Skip to content

Commit

Permalink
General updates
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg committed Apr 24, 2022
1 parent 63b9c81 commit 7ffe6c2
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 21 deletions.
10 changes: 10 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2
updates:
- package-ecosystem: gomod
directory: "/"
schedule:
interval: daily
- package-ecosystem: github-actions
directory: "/"
schedule:
interval: daily
1 change: 1 addition & 0 deletions GUIDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Guide for oauth2
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[![pkg-img]][pkg-url]
[![reportcard-img]][reportcard-url]
[![coverage-img]][coverage-url]
[![version-img]][version-url]

OAuth2 client in Go.

Expand All @@ -13,9 +14,11 @@ OAuth2 client in Go.
* Tiny codebase.
* Dependency-free.

See [GUIDE.md](https://github.com/cristalhq/oauth2/blob/main/GUIDE.md) for more details.

## Install

Go version 1.13
Go version 1.17

```
go get github.com/cristalhq/oauth2
Expand Down Expand Up @@ -75,3 +78,5 @@ See [these docs][pkg-url].
[reportcard-url]: https://goreportcard.com/report/cristalhq/oauth2
[coverage-img]: https://codecov.io/gh/cristalhq/oauth2/branch/master/graph/badge.svg
[coverage-url]: https://codecov.io/gh/cristalhq/oauth2
[version-img]: https://img.shields.io/github/v/release/cristalhq/oauth2
[version-url]: https://github.com/cristalhq/oauth2/releases
22 changes: 8 additions & 14 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@ func NewClient(client *http.Client, config Config) *Client {
// the state query parameter on your redirect callback.
//
// See http://tools.ietf.org/html/rfc6749#section-10.12 for more info.
//
func (c *Client) AuthCodeURL(state string) string {
return c.AuthCodeURLWithParams(state, nil)
}

// AuthCodeURLWithParams same as AuthCodeURL but allows to pass additional URL parameters.
//
func (c *Client) AuthCodeURLWithParams(state string, vals url.Values) string {
// TODO(cristaloleg): can be set once (except state).
v := cloneURLValues(vals)
Expand Down Expand Up @@ -72,13 +70,11 @@ func (c *Client) AuthCodeURLWithParams(state string, vals url.Values) string {
}

// Exchange converts an authorization code into an OAuth2 token.
//
func (c *Client) Exchange(ctx context.Context, code string) (*Token, error) {
return c.ExchangeWithParams(ctx, code, nil)
}

// ExchangeWithParams converts an authorization code into an OAuth2 token.
//
func (c *Client) ExchangeWithParams(ctx context.Context, code string, params url.Values) (*Token, error) {
vals := cloneURLValues(params)
vals.Add("grant_type", "authorization_code")
Expand All @@ -91,7 +87,6 @@ func (c *Client) ExchangeWithParams(ctx context.Context, code string, params url
}

// CredentialsToken retrieves a token for given username and password.
//
func (c *Client) CredentialsToken(ctx context.Context, username, password string) (*Token, error) {
vals := url.Values{
"grant_type": []string{"password"},
Expand All @@ -106,7 +101,6 @@ 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")
Expand All @@ -116,7 +110,6 @@ func (c *Client) Token(ctx context.Context, refreshToken string) (*Token, error)
"grant_type": []string{"refresh_token"},
"refresh_token": []string{refreshToken},
}

return c.retrieveToken(ctx, vals)
}

Expand All @@ -128,29 +121,30 @@ func (c *Client) retrieveToken(ctx context.Context, vals url.Values) (*Token, er
mode = InHeaderMode
}

token, err := c.makeRequest(ctx, mode, vals)
token, err := c.doRequest(ctx, mode, vals)
if err == nil {
c.config.Mode = mode
return token, nil
}
if !shouldGuessAuthMode {
return nil, err
}
mode = InParamsMode

token, err = c.makeRequest(ctx, mode, vals)
token, err = c.doRequest(ctx, mode, vals)
if err != nil {
return nil, err
}
c.config.Mode = mode
return token, nil
}

func (c *Client) makeRequest(ctx context.Context, mode Mode, vals url.Values) (*Token, error) {
req, err := c.newTokenRequest(mode, vals)
func (c *Client) doRequest(ctx context.Context, mode Mode, vals url.Values) (*Token, error) {
req, err := c.newTokenRequest(ctx, mode, vals)
if err != nil {
return nil, err
}
resp, err := c.client.Do(req.WithContext(ctx))
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
Expand All @@ -161,7 +155,7 @@ func (c *Client) makeRequest(ctx context.Context, mode Mode, vals url.Values) (*
return token, nil
}

func (c *Client) newTokenRequest(mode Mode, v url.Values) (*http.Request, error) {
func (c *Client) newTokenRequest(ctx context.Context, mode Mode, v url.Values) (*http.Request, error) {
clientID, clientSecret := c.config.ClientID, c.config.ClientSecret

if mode == InParamsMode {
Expand All @@ -174,7 +168,7 @@ func (c *Client) newTokenRequest(mode Mode, v url.Values) (*http.Request, error)
}
}

req, err := http.NewRequest("POST", c.config.TokenURL, strings.NewReader(v.Encode()))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.config.TokenURL, strings.NewReader(v.Encode()))
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ func TestExchangeRequest_JSONResponse_Expiry(t *testing.T) {
true, false,
)
f(
fmt.Sprintf(`"expires_in": null`),
`"expires_in": null`,
true, true,
)
f(
Expand Down
4 changes: 2 additions & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ func ExampleClientAndConfig() {
config := oauth2.Config{
ClientID: "YOUR_CLIENT_ID",
ClientSecret: "YOUR_CLIENT_SECRET",
AuthURL: "https://provider.com/o/oauth2/auth",
TokenURL: "https://provider.com/o/oauth2/token",
AuthURL: "https://example.com/o/oauth2/auth",
TokenURL: "https://example.com/o/oauth2/token",
Scopes: []string{"email", "avatar"},
}

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.13
go 1.17
3 changes: 1 addition & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"math"
"mime"
"net/http"
Expand All @@ -27,7 +26,7 @@ func cloneURLValues(vals url.Values) url.Values {
}

func parseResponse(resp *http.Response) (*Token, error) {
body, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20))
body, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
resp.Body.Close()

if err != nil {
Expand Down

0 comments on commit 7ffe6c2

Please sign in to comment.