Skip to content

Commit

Permalink
Added oauth logging
Browse files Browse the repository at this point in the history
  • Loading branch information
aurc committed Aug 16, 2022
1 parent 31b46cf commit fd69a97
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
12 changes: 12 additions & 0 deletions internal/gcp/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func doOAuthAsync(clientId, clientSecret string) {
tcp := listener.Addr().(*net.TCPAddr)

redirectUri := fmt.Sprintf("http://%s:%d/", "127.0.0.1", tcp.Port)
util.Log().WithField("code", redirectUri).Info("Preparing redirect url")

scopes := []string{
"openid",
Expand All @@ -92,6 +93,8 @@ func doOAuthAsync(clientId, clientSecret string) {
code: make(chan string, 1),
}

util.Log().WithField("code", authorizationRequest).Info("Assembled authorisation request.")

go func() {
err = util.OpenBrowser(authorizationRequest)
if err != nil {
Expand All @@ -101,13 +104,15 @@ func doOAuthAsync(clientId, clientSecret string) {
}()

go func() {
util.Log().Infof("Start redirect listener at port %d", tcp.Port)
err = http.Serve(listener, c)
if err != nil {
util.Log().Fatal(err)
}
}()

code := <-c.code
util.Log().WithField("code", code).Info("Received Auth code.")
a := exchangeCodeForTokensAsync(code, codeVerifier.String(), redirectUri, clientId, clientSecret)
a.Save()
}
Expand All @@ -126,6 +131,8 @@ func exchangeCodeForTokensAsync(code, codeVerifier, redirectUri, clientId, clien
data.Set("grant_type", "authorization_code")
encodedData := data.Encode()

util.Log().WithField("code", tokenRequestUri).WithField("data", encodedData).Info("Requesting token exchange.")

req, err := http.NewRequest(http.MethodPost, tokenRequestUri, strings.NewReader(encodedData))
if err != nil {
util.Log().Fatal(err)
Expand All @@ -143,6 +150,9 @@ func exchangeCodeForTokensAsync(code, codeVerifier, redirectUri, clientId, clien
if err != nil {
util.Log().Fatal(err)
}

util.Log().WithField("code", body.String()).Info("Received token response.")

m := make(map[string]string)
_ = json.Unmarshal(body.Bytes(), &m)
return &Auth{
Expand All @@ -162,6 +172,8 @@ func (c *callbackHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request)
time.Sleep(time.Second)
vals := req.URL.Query()

util.Log().WithField("code", req.URL.RawQuery).Info("Received request from browser")

if strings.Contains(req.RequestURI, "favicon") {
resp.WriteHeader(404)
return
Expand Down
2 changes: 2 additions & 0 deletions internal/util/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ package util
import (
"os/exec"
"runtime"
"strings"
)

func OpenBrowser(url string) error {
Expand All @@ -41,5 +42,6 @@ func OpenBrowser(url string) error {
cmd = "xdg-open"
}
args = append(args, url)
Log().WithField("code", cmd+" "+strings.Join(args, " ")).Info("Issue browser command.")
return exec.Command(cmd, args...).Start()
}

0 comments on commit fd69a97

Please sign in to comment.