diff --git a/README.md b/README.md index 530f0c7..ef8d703 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ [![coverage-img]][coverage-url] [![version-img]][version-url] -OAuth2 client in Go. +OAuth2 client for Go. ## Features @@ -40,13 +40,13 @@ client := oauth2.NewClient(http.DefaultClient, config) // url to fetch the code url := client.AuthCodeURL("state") -fmt.Printf("Visit the URL for the auth dialog: %v", url) +fmt.Printf("Visit the URL with the auth dialog: %v", url) // Use the authorization code that is pushed to the redirect URL. // Exchange will do the handshake to retrieve the initial access token. var code string if _, err := fmt.Scan(&code); err != nil { - log.Fatal(err) + panic(err) } // get a token @@ -62,6 +62,8 @@ var _ time.Time = token.Expiry // token expiration time var _ bool = token.IsExpired() // have token expired? ``` +Also see examples: [example_test.go](https://github.com/cristalhq/oauth2/blob/main/example_test.go). + ## Documentation See [these docs][pkg-url]. diff --git a/client.go b/client.go index a1fcdef..094dbd1 100644 --- a/client.go +++ b/client.go @@ -10,14 +10,12 @@ import ( ) // Client represents an OAuth2 HTTP client. -// type Client struct { client *http.Client config Config } // NewClient instantiates a new client with a given config. -// func NewClient(client *http.Client, config Config) *Client { c := &Client{ client: client, @@ -103,7 +101,7 @@ func (c *Client) CredentialsToken(ctx context.Context, username, password string // Token renews a token based on previous token. func (c *Client) Token(ctx context.Context, refreshToken string) (*Token, error) { if refreshToken == "" { - return nil, errors.New("oauth2: refresh token is not set") + return nil, errors.New("refresh token is not set") } params := url.Values{ @@ -144,10 +142,12 @@ func (c *Client) doRequest(ctx context.Context, mode Mode, params url.Values) (* if err != nil { return nil, err } + resp, err := c.client.Do(req) if err != nil { return nil, err } + token, err := parseResponse(resp) if err != nil { return nil, err diff --git a/go.mod b/go.mod index 84fc43f..20f6dec 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/cristalhq/oauth2 -go 1.17 +go 1.19 diff --git a/token.go b/token.go index 0ccd690..6478294 100644 --- a/token.go +++ b/token.go @@ -9,7 +9,6 @@ import ( // Token represents the credentials used to authorize the requests to access // protected resources on the OAuth 2.0 provider's backend. -// type Token struct { AccessToken string `json:"access_token"` // AccessToken is the token that authorizes and authenticates the requests. TokenType string `json:"token_type,omitempty"` // TokenType is the type of token. The Type method returns either this or "Bearer". @@ -68,7 +67,6 @@ func (t *Token) Extra(key string) interface{} { } // Valid reports whether t is non-nil, has an AccessToken, and is not expired. -// func (t *Token) Valid() bool { return t != nil && t.AccessToken != "" && !t.IsExpired() } @@ -82,7 +80,6 @@ var timeNow = time.Now const expiryDelta = 10 * time.Second // IsExpired reports whether the token is expired. -// func (t *Token) IsExpired() bool { if t.Expiry.IsZero() { return false diff --git a/wrapper.go b/wrapper.go index 846fbad..3e89974 100644 --- a/wrapper.go +++ b/wrapper.go @@ -7,7 +7,6 @@ import ( // Wrap adds an additional header to the given http.Client. // The header will be `Authorization`. // All the params cannot be empty or nil. -// func Wrap(header, value string, c *http.Client) (*http.Client, error) { transport := http.DefaultTransport if c.Transport != nil {