Skip to content

Commit

Permalink
Small updates
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg committed Nov 10, 2022
1 parent 07ad86c commit 83be859
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 11 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![coverage-img]][coverage-url]
[![version-img]][version-url]

OAuth2 client in Go.
OAuth2 client for Go.

## Features

Expand Down Expand Up @@ -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
Expand All @@ -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].
Expand Down
6 changes: 3 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/cristalhq/oauth2

go 1.17
go 1.19
3 changes: 0 additions & 3 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand Down Expand Up @@ -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()
}
Expand All @@ -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
Expand Down
1 change: 0 additions & 1 deletion wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 83be859

Please sign in to comment.