Skip to content

Commit 232fabf

Browse files
committed
OAuth2 doc
1 parent e5af1bf commit 232fabf

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

client_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ func TestSetClientCredentialNotAllowedTarget(t *testing.T) {
693693
}
694694

695695
func TestOauth2AccessTokenReqs(t *testing.T) {
696-
accesToken := "yourAccessToken"
696+
accessToken := "yourAccessToken"
697697
refreshToken := "yourRefreshToken"
698698
authSrv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
699699
assert.NoError(t, r.ParseForm())
@@ -708,7 +708,7 @@ func TestOauth2AccessTokenReqs(t *testing.T) {
708708
assert.True(t, strings.HasPrefix(r.Header.Get("Authorization"), "Basic"))
709709
}
710710
w.Header().Set("Content-type", "application/json")
711-
w.Write([]byte(`{"access_token" : "` + accesToken + `", "expires_in": 60, "refresh_Token": "` + refreshToken + `"}`))
711+
w.Write([]byte(`{"access_token" : "` + accessToken + `", "expires_in": 60, "refresh_Token": "` + refreshToken + `"}`))
712712
}))
713713
defer authSrv.Close()
714714
ctx := context.Background()
@@ -718,15 +718,15 @@ func TestOauth2AccessTokenReqs(t *testing.T) {
718718
client := NewClient().HTTPS(&HTTPSConfig{AllowedHTTPHosts: []string{"127.0.0.1"}}).SetOauth2Conf(oauth2.Config{Endpoint: oauth2.Endpoint{TokenURL: authSrv.URL}}, nil, "garbage")
719719
err := client.setOauth2Auth(ctx, req)
720720
assert.NoError(t, err)
721-
assert.Equal(t, client.oauth2.token.AccessToken, accesToken)
721+
assert.Equal(t, client.oauth2.token.AccessToken, accessToken)
722722

723723
// Test client with password credentials grant
724724
client = NewClient().HTTPS(&HTTPSConfig{AllowedHTTPHosts: []string{"127.0.0.1"}}).SetOauth2Conf(oauth2.Config{Endpoint: oauth2.Endpoint{TokenURL: authSrv.URL}}, nil, GrantPasswordCredentials)
725725
assert.Equal(t, len(client.oauth2.config.Scopes), 0)
726726
client.SetBasicAuth("user", "pass")
727727
err = client.setOauth2Auth(ctx, req)
728728
assert.NoError(t, err)
729-
assert.Equal(t, client.oauth2.token.AccessToken, accesToken)
729+
assert.Equal(t, client.oauth2.token.AccessToken, accessToken)
730730
assert.Equal(t, client.oauth2.token.RefreshToken, refreshToken)
731731
req.Header.Del("Authorization")
732732

@@ -737,21 +737,21 @@ func TestOauth2AccessTokenReqs(t *testing.T) {
737737
assert.Equal(t, client.oauth2.token.RefreshToken, "")
738738
err = client.setOauth2Auth(ctx, req) // First try with password credentials grant without refresh_token
739739
assert.NoError(t, err)
740-
assert.Equal(t, client.oauth2.token.AccessToken, accesToken)
740+
assert.Equal(t, client.oauth2.token.AccessToken, accessToken)
741741
assert.Equal(t, client.oauth2.token.RefreshToken, refreshToken)
742742
req.Header.Del("Authorization")
743743
client.oauth2.token.AccessToken = "" // Let's make the access token invalid before attempting a second request
744744
err = client.setOauth2Auth(ctx, req) // Second try with refresh_token grant with refresh_token included
745745
assert.NoError(t, err)
746-
assert.Equal(t, client.oauth2.token.AccessToken, accesToken)
746+
assert.Equal(t, client.oauth2.token.AccessToken, accessToken)
747747
req.Header.Del("Authorization")
748748

749749
// Test client with default client credentials grant
750750
client = NewClient().HTTPS(&HTTPSConfig{AllowedHTTPHosts: []string{"127.0.0.1"}}).SetOauth2Conf(oauth2.Config{ClientID: "id", ClientSecret: "secret", Endpoint: oauth2.Endpoint{TokenURL: authSrv.URL}, Scopes: []string{"openid", "profile"}}, nil)
751751
assert.Equal(t, len(client.oauth2.config.Scopes), 2)
752752
err = client.setOauth2Auth(ctx, req)
753753
assert.NoError(t, err)
754-
assert.Equal(t, client.oauth2.token.AccessToken, accesToken)
754+
assert.Equal(t, client.oauth2.token.AccessToken, accessToken)
755755

756756
// Test h2 OAuth2 client
757757
client = NewClient().SetOauth2Conf(oauth2.Config{ClientID: "id", ClientSecret: "secret", Endpoint: oauth2.Endpoint{TokenURL: authSrv.URL}}, nil).SetOauth2H2()

doc/client.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,21 @@ client.TLSOwnCerts("/etc/own_tls")
104104
Any update (e.g., cert-manager.io) will not affect that client.
105105
You may restart your app, e.g. issue `kubectl rollout restart deploy xxx` when using K8s.
106106

107+
### OAuth2
108+
109+
Restful client uses [Golang's oauth2 package](https://github.com/golang/oauth2). That creates its own client to obtain the necessary access token for the request you want to make.
110+
111+
```go
112+
client := restful.NewClient().SetOauth2Conf(oauth2.Config{...}, nil /*optional client*/)
113+
_, err := client.Post(ctx, "https://api.example.com", &reqData, &respData)
114+
```
115+
116+
Client Credentials grant example:
117+
118+
```go
119+
client := restful.NewClient().SetOauth2Conf(oauth2.Config{ClientID: "id", ClientSecret: "secret", Endpoint: oauth2.Endpoint{TokenURL: "https://as.example.com/auth"}}, nil)
120+
```
121+
107122
## MessagePack
108123

109124
MessagePack is substantially cheaper to parse compared to JSON.

0 commit comments

Comments
 (0)