Skip to content

Commit 9fd6049

Browse files
gIthurielcodyoss
authored andcommitted
google: add utilities supporting upcoming oauth2 functionality
These are used to support some extended utilities to help with STS requests. Change-Id: Iafc145b06ca42374cfc2ac6572762a50bcf560f2 GitHub-Last-Rev: 3085fe5 GitHub-Pull-Request: #439 Reviewed-on: https://go-review.googlesource.com/c/oauth2/+/259777 Trust: Cody Oss <[email protected]> Run-TryBot: Cody Oss <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Tyler Bui-Palsulich <[email protected]>
1 parent 5d25da1 commit 9fd6049

File tree

4 files changed

+191
-0
lines changed

4 files changed

+191
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2020 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package externalaccount
6+
7+
import (
8+
"encoding/base64"
9+
"golang.org/x/oauth2"
10+
"net/http"
11+
"net/url"
12+
)
13+
14+
// ClientAuthentication represents an OAuth client ID and secret and the mechanism for passing these credentials as stated in rfc6749#2.3.1.
15+
type ClientAuthentication struct {
16+
// AuthStyle can be either basic or request-body
17+
AuthStyle oauth2.AuthStyle
18+
ClientID string
19+
ClientSecret string
20+
}
21+
22+
func (c *ClientAuthentication) InjectAuthentication(values url.Values, headers http.Header) {
23+
if c.ClientID == "" || c.ClientSecret == "" || values == nil || headers == nil {
24+
return
25+
}
26+
27+
switch c.AuthStyle {
28+
case oauth2.AuthStyleInHeader: // AuthStyleInHeader corresponds to basic authentication as defined in rfc7617#2
29+
plainHeader := c.ClientID + ":" + c.ClientSecret
30+
headers.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(plainHeader)))
31+
case oauth2.AuthStyleInParams: // AuthStyleInParams corresponds to request-body authentication with ClientID and ClientSecret in the message body.
32+
values.Set("client_id", c.ClientID)
33+
values.Set("client_secret", c.ClientSecret)
34+
case oauth2.AuthStyleAutoDetect:
35+
values.Set("client_id", c.ClientID)
36+
values.Set("client_secret", c.ClientSecret)
37+
default:
38+
values.Set("client_id", c.ClientID)
39+
values.Set("client_secret", c.ClientSecret)
40+
}
41+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Copyright 2020 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package externalaccount
6+
7+
import (
8+
"golang.org/x/oauth2"
9+
"net/http"
10+
"net/url"
11+
"reflect"
12+
"testing"
13+
)
14+
15+
var clientID = "rbrgnognrhongo3bi4gb9ghg9g"
16+
var clientSecret = "notsosecret"
17+
18+
var audience = []string{"32555940559.apps.googleusercontent.com"}
19+
var grantType = []string{"urn:ietf:params:oauth:grant-type:token-exchange"}
20+
var requestedTokenType = []string{"urn:ietf:params:oauth:token-type:access_token"}
21+
var subjectTokenType = []string{"urn:ietf:params:oauth:token-type:jwt"}
22+
var subjectToken = []string{"eyJhbGciOiJSUzI1NiIsImtpZCI6IjJjNmZhNmY1OTUwYTdjZTQ2NWZjZjI0N2FhMGIwOTQ4MjhhYzk1MmMiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLCJhenAiOiIzMjU1NTk0MDU1OS5hcHBzLmdvb2dsZXVzZXJjb250ZW50LmNvbSIsImF1ZCI6IjMyNTU1OTQwNTU5LmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwic3ViIjoiMTEzMzE4NTQxMDA5MDU3Mzc4MzI4IiwiaGQiOiJnb29nbGUuY29tIiwiZW1haWwiOiJpdGh1cmllbEBnb29nbGUuY29tIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImF0X2hhc2giOiI5OVJVYVFrRHJsVDFZOUV0SzdiYXJnIiwiaWF0IjoxNjAxNTgxMzQ5LCJleHAiOjE2MDE1ODQ5NDl9.SZ-4DyDcogDh_CDUKHqPCiT8AKLg4zLMpPhGQzmcmHQ6cJiV0WRVMf5Lq911qsvuekgxfQpIdKNXlD6yk3FqvC2rjBbuEztMF-OD_2B8CEIYFlMLGuTQimJlUQksLKM-3B2ITRDCxnyEdaZik0OVssiy1CBTsllS5MgTFqic7w8w0Cd6diqNkfPFZRWyRYsrRDRlHHbH5_TUnv2wnLVHBHlNvU4wU2yyjDIoqOvTRp8jtXdq7K31CDhXd47-hXsVFQn2ZgzuUEAkH2Q6NIXACcVyZOrjBcZiOQI9IRWz-g03LzbzPSecO7I8dDrhqUSqMrdNUz_f8Kr8JFhuVMfVug"}
23+
var scope = []string{"https://www.googleapis.com/auth/devstorage.full_control"}
24+
25+
var ContentType = []string{"application/x-www-form-urlencoded"}
26+
27+
func TestClientAuthentication_InjectHeaderAuthentication(t *testing.T) {
28+
valuesH := url.Values{
29+
"audience": audience,
30+
"grant_type": grantType,
31+
"requested_token_type": requestedTokenType,
32+
"subject_token_type": subjectTokenType,
33+
"subject_token": subjectToken,
34+
"scope": scope,
35+
}
36+
headerH := http.Header{
37+
"Content-Type": ContentType,
38+
}
39+
40+
headerAuthentication := ClientAuthentication{
41+
AuthStyle: oauth2.AuthStyleInHeader,
42+
ClientID: clientID,
43+
ClientSecret: clientSecret,
44+
}
45+
headerAuthentication.InjectAuthentication(valuesH, headerH)
46+
47+
if got, want := valuesH["audience"], audience; !reflect.DeepEqual(got, want) {
48+
t.Errorf("audience = %q, want %q", got, want)
49+
}
50+
if got, want := valuesH["grant_type"], grantType; !reflect.DeepEqual(got, want) {
51+
t.Errorf("grant_type = %q, want %q", got, want)
52+
}
53+
if got, want := valuesH["requested_token_type"], requestedTokenType; !reflect.DeepEqual(got, want) {
54+
t.Errorf("requested_token_type = %q, want %q", got, want)
55+
}
56+
if got, want := valuesH["subject_token_type"], subjectTokenType; !reflect.DeepEqual(got, want) {
57+
t.Errorf("subject_token_type = %q, want %q", got, want)
58+
}
59+
if got, want := valuesH["subject_token"], subjectToken; !reflect.DeepEqual(got, want) {
60+
t.Errorf("subject_token = %q, want %q", got, want)
61+
}
62+
if got, want := valuesH["scope"], scope; !reflect.DeepEqual(got, want) {
63+
t.Errorf("scope = %q, want %q", got, want)
64+
}
65+
if got, want := headerH["Authorization"], []string{"Basic cmJyZ25vZ25yaG9uZ28zYmk0Z2I5Z2hnOWc6bm90c29zZWNyZXQ="}; !reflect.DeepEqual(got, want) {
66+
t.Errorf("Authorization in header = %q, want %q", got, want)
67+
}
68+
}
69+
70+
func TestClientAuthentication_ParamsAuthentication(t *testing.T) {
71+
valuesP := url.Values{
72+
"audience": audience,
73+
"grant_type": grantType,
74+
"requested_token_type": requestedTokenType,
75+
"subject_token_type": subjectTokenType,
76+
"subject_token": subjectToken,
77+
"scope": scope,
78+
}
79+
headerP := http.Header{
80+
"Content-Type": ContentType,
81+
}
82+
paramsAuthentication := ClientAuthentication{
83+
AuthStyle: oauth2.AuthStyleInParams,
84+
ClientID: clientID,
85+
ClientSecret: clientSecret,
86+
}
87+
paramsAuthentication.InjectAuthentication(valuesP, headerP)
88+
89+
if got, want := valuesP["audience"], audience; !reflect.DeepEqual(got, want) {
90+
t.Errorf("audience = %q, want %q", got, want)
91+
}
92+
if got, want := valuesP["grant_type"], grantType; !reflect.DeepEqual(got, want) {
93+
t.Errorf("grant_type = %q, want %q", got, want)
94+
}
95+
if got, want := valuesP["requested_token_type"], requestedTokenType; !reflect.DeepEqual(got, want) {
96+
t.Errorf("requested_token_type = %q, want %q", got, want)
97+
}
98+
if got, want := valuesP["subject_token_type"], subjectTokenType; !reflect.DeepEqual(got, want) {
99+
t.Errorf("subject_token_type = %q, want %q", got, want)
100+
}
101+
if got, want := valuesP["subject_token"], subjectToken; !reflect.DeepEqual(got, want) {
102+
t.Errorf("subject_token = %q, want %q", got, want)
103+
}
104+
if got, want := valuesP["scope"], scope; !reflect.DeepEqual(got, want) {
105+
t.Errorf("scope = %q, want %q", got, want)
106+
}
107+
if got, want := valuesP["client_id"], []string{clientID}; !reflect.DeepEqual(got, want) {
108+
t.Errorf("client_id = %q, want %q", got, want)
109+
}
110+
if got, want := valuesP["client_secret"], []string{clientSecret}; !reflect.DeepEqual(got, want) {
111+
t.Errorf("client_secret = %q, want %q", got, want)
112+
}
113+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2020 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package externalaccount
6+
7+
import "fmt"
8+
9+
// Error for handling OAuth related error responses as stated in rfc6749#5.2.
10+
type Error struct {
11+
Code string
12+
URI string
13+
Description string
14+
}
15+
16+
func (err *Error) Error() string {
17+
return fmt.Sprintf("got error code %s from %s: %s", err.Code, err.URI, err.Description)
18+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2020 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package externalaccount
6+
7+
import "testing"
8+
9+
func TestError(t *testing.T) {
10+
e := Error{
11+
"42",
12+
"http:thisIsAPlaceholder",
13+
"The Answer!",
14+
}
15+
want := "got error code 42 from http:thisIsAPlaceholder: The Answer!"
16+
if got := e.Error(); got != want {
17+
t.Errorf("Got error message %q; want %q", got, want)
18+
}
19+
}

0 commit comments

Comments
 (0)