Skip to content

Commit

Permalink
Add debug log feature (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
int128 authored Jul 12, 2020
1 parent 0466ce0 commit e6ee980
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
1 change: 1 addition & 0 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ Then set the following options:`)
LocalServerReadyChan: ready,
LocalServerCertFile: o.localServerCert,
LocalServerKeyFile: o.localServerKey,
Logf: log.Printf,
}

ctx := context.Background()
Expand Down
17 changes: 12 additions & 5 deletions oauth2cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ type Config struct {
// A channel to send its URL when the local server is ready. Default to none.
LocalServerReadyChan chan<- string

// Logger function for debug.
Logf func(format string, args ...interface{})

// DEPRECATED: this will be removed in the future release.
// Use LocalServerBindAddress instead.
// Address which the local server binds to.
Expand Down Expand Up @@ -123,6 +126,9 @@ func (c *Config) validateAndSetDefaults() error {
if c.LocalServerSuccessHTML == "" {
c.LocalServerSuccessHTML = DefaultLocalServerSuccessHTML
}
if c.Logf == nil {
c.Logf = func(string, ...interface{}) {}
}
return nil
}

Expand Down Expand Up @@ -150,16 +156,17 @@ func (c *Config) populateDeprecatedFields() {
// 5. Exchange the code and a token.
// 6. Return the code.
//
func GetToken(ctx context.Context, config Config) (*oauth2.Token, error) {
if err := config.validateAndSetDefaults(); err != nil {
func GetToken(ctx context.Context, c Config) (*oauth2.Token, error) {
if err := c.validateAndSetDefaults(); err != nil {
return nil, fmt.Errorf("invalid config: %w", err)
}
config.populateDeprecatedFields()
code, err := receiveCodeViaLocalServer(ctx, &config)
c.populateDeprecatedFields()
code, err := receiveCodeViaLocalServer(ctx, &c)
if err != nil {
return nil, fmt.Errorf("authorization error: %w", err)
}
token, err := config.OAuth2Config.Exchange(ctx, code, config.TokenRequestOptions...)
c.Logf("oauth2cli: exchanging the code and token")
token, err := c.OAuth2Config.Exchange(ctx, code, c.TokenRequestOptions...)
if err != nil {
return nil, fmt.Errorf("could not exchange the code and token: %w", err)
}
Expand Down
15 changes: 12 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,32 +33,40 @@ func receiveCodeViaLocalServer(ctx context.Context, c *Config) (string, error) {
select {
case received, ok := <-respCh:
if !ok {
c.Logf("oauth2cli: response channel has been closed")
return nil // channel is closed (after the server is stopped)
}
if resp == nil {
resp = received // pick only the first response
}
c.Logf("oauth2cli: shutting down the server at %s", l.Addr())
if err := server.Shutdown(ctx); err != nil {
return fmt.Errorf("could not shutdown the local server: %w", err)
}
case <-ctx.Done():
c.Logf("oauth2cli: context cancelled: %s", ctx.Err())
c.Logf("oauth2cli: shutting down the server at %s", l.Addr())
if err := server.Shutdown(ctx); err != nil {
return fmt.Errorf("could not shutdown the local server: %w", err)
}
return fmt.Errorf("context done while waiting for authorization response: %w", ctx.Err())
return fmt.Errorf("context cancelled while waiting for authorization response: %w", ctx.Err())
}
}
})
eg.Go(func() error {
defer close(respCh)
if c.LocalServerCertFile != "" && c.LocalServerKeyFile != "" {
c.Logf("oauth2cli: starting HTTPS server at %s", l.Addr())
if err := server.ServeTLS(l, c.LocalServerCertFile, c.LocalServerKeyFile); err != nil && err != http.ErrServerClosed {
return fmt.Errorf("could not start a local TLS server: %w", err)
return fmt.Errorf("could not start HTTPS server: %w", err)
}
c.Logf("oauth2cli: stopped HTTPS server at %s", l.Addr())
} else {
c.Logf("oauth2cli: starting HTTP server at %s", l.Addr())
if err := server.Serve(l); err != nil && err != http.ErrServerClosed {
return fmt.Errorf("could not start a local server: %w", err)
return fmt.Errorf("could not start HTTP server: %w", err)
}
c.Logf("oauth2cli: stopped HTTP server at %s", l.Addr())
}
return nil
})
Expand Down Expand Up @@ -109,6 +117,7 @@ func (h *localServerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

func (h *localServerHandler) handleIndex(w http.ResponseWriter, r *http.Request) {
authCodeURL := h.config.OAuth2Config.AuthCodeURL(h.config.State, h.config.AuthCodeOptions...)
h.config.Logf("oauth2cli: sending redirect to %s", authCodeURL)
http.Redirect(w, r, authCodeURL, 302)
}

Expand Down

0 comments on commit e6ee980

Please sign in to comment.