golang.org/x/oauth2@v0.18.0/google/internal/stsexchange/clientauth_test.go (about) 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 stsexchange 6 7 import ( 8 "net/http" 9 "net/url" 10 "reflect" 11 "testing" 12 13 "golang.org/x/oauth2" 14 ) 15 16 var clientID = "rbrgnognrhongo3bi4gb9ghg9g" 17 var clientSecret = "notsosecret" 18 19 var audience = []string{"32555940559.apps.googleusercontent.com"} 20 var grantType = []string{"urn:ietf:params:oauth:grant-type:token-exchange"} 21 var requestedTokenType = []string{"urn:ietf:params:oauth:token-type:access_token"} 22 var subjectTokenType = []string{"urn:ietf:params:oauth:token-type:jwt"} 23 var subjectToken = []string{"eyJhbGciOiJSUzI1NiIsImtpZCI6IjJjNmZhNmY1OTUwYTdjZTQ2NWZjZjI0N2FhMGIwOTQ4MjhhYzk1MmMiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLCJhenAiOiIzMjU1NTk0MDU1OS5hcHBzLmdvb2dsZXVzZXJjb250ZW50LmNvbSIsImF1ZCI6IjMyNTU1OTQwNTU5LmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwic3ViIjoiMTEzMzE4NTQxMDA5MDU3Mzc4MzI4IiwiaGQiOiJnb29nbGUuY29tIiwiZW1haWwiOiJpdGh1cmllbEBnb29nbGUuY29tIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImF0X2hhc2giOiI5OVJVYVFrRHJsVDFZOUV0SzdiYXJnIiwiaWF0IjoxNjAxNTgxMzQ5LCJleHAiOjE2MDE1ODQ5NDl9.SZ-4DyDcogDh_CDUKHqPCiT8AKLg4zLMpPhGQzmcmHQ6cJiV0WRVMf5Lq911qsvuekgxfQpIdKNXlD6yk3FqvC2rjBbuEztMF-OD_2B8CEIYFlMLGuTQimJlUQksLKM-3B2ITRDCxnyEdaZik0OVssiy1CBTsllS5MgTFqic7w8w0Cd6diqNkfPFZRWyRYsrRDRlHHbH5_TUnv2wnLVHBHlNvU4wU2yyjDIoqOvTRp8jtXdq7K31CDhXd47-hXsVFQn2ZgzuUEAkH2Q6NIXACcVyZOrjBcZiOQI9IRWz-g03LzbzPSecO7I8dDrhqUSqMrdNUz_f8Kr8JFhuVMfVug"} 24 var scope = []string{"https://www.googleapis.com/auth/devstorage.full_control"} 25 26 var ContentType = []string{"application/x-www-form-urlencoded"} 27 28 func TestClientAuthentication_InjectHeaderAuthentication(t *testing.T) { 29 valuesH := url.Values{ 30 "audience": audience, 31 "grant_type": grantType, 32 "requested_token_type": requestedTokenType, 33 "subject_token_type": subjectTokenType, 34 "subject_token": subjectToken, 35 "scope": scope, 36 } 37 headerH := http.Header{ 38 "Content-Type": ContentType, 39 } 40 41 headerAuthentication := ClientAuthentication{ 42 AuthStyle: oauth2.AuthStyleInHeader, 43 ClientID: clientID, 44 ClientSecret: clientSecret, 45 } 46 headerAuthentication.InjectAuthentication(valuesH, headerH) 47 48 if got, want := valuesH["audience"], audience; !reflect.DeepEqual(got, want) { 49 t.Errorf("audience = %q, want %q", got, want) 50 } 51 if got, want := valuesH["grant_type"], grantType; !reflect.DeepEqual(got, want) { 52 t.Errorf("grant_type = %q, want %q", got, want) 53 } 54 if got, want := valuesH["requested_token_type"], requestedTokenType; !reflect.DeepEqual(got, want) { 55 t.Errorf("requested_token_type = %q, want %q", got, want) 56 } 57 if got, want := valuesH["subject_token_type"], subjectTokenType; !reflect.DeepEqual(got, want) { 58 t.Errorf("subject_token_type = %q, want %q", got, want) 59 } 60 if got, want := valuesH["subject_token"], subjectToken; !reflect.DeepEqual(got, want) { 61 t.Errorf("subject_token = %q, want %q", got, want) 62 } 63 if got, want := valuesH["scope"], scope; !reflect.DeepEqual(got, want) { 64 t.Errorf("scope = %q, want %q", got, want) 65 } 66 if got, want := headerH["Authorization"], []string{"Basic cmJyZ25vZ25yaG9uZ28zYmk0Z2I5Z2hnOWc6bm90c29zZWNyZXQ="}; !reflect.DeepEqual(got, want) { 67 t.Errorf("Authorization in header = %q, want %q", got, want) 68 } 69 } 70 71 func TestClientAuthentication_ParamsAuthentication(t *testing.T) { 72 valuesP := url.Values{ 73 "audience": audience, 74 "grant_type": grantType, 75 "requested_token_type": requestedTokenType, 76 "subject_token_type": subjectTokenType, 77 "subject_token": subjectToken, 78 "scope": scope, 79 } 80 headerP := http.Header{ 81 "Content-Type": ContentType, 82 } 83 paramsAuthentication := ClientAuthentication{ 84 AuthStyle: oauth2.AuthStyleInParams, 85 ClientID: clientID, 86 ClientSecret: clientSecret, 87 } 88 paramsAuthentication.InjectAuthentication(valuesP, headerP) 89 90 if got, want := valuesP["audience"], audience; !reflect.DeepEqual(got, want) { 91 t.Errorf("audience = %q, want %q", got, want) 92 } 93 if got, want := valuesP["grant_type"], grantType; !reflect.DeepEqual(got, want) { 94 t.Errorf("grant_type = %q, want %q", got, want) 95 } 96 if got, want := valuesP["requested_token_type"], requestedTokenType; !reflect.DeepEqual(got, want) { 97 t.Errorf("requested_token_type = %q, want %q", got, want) 98 } 99 if got, want := valuesP["subject_token_type"], subjectTokenType; !reflect.DeepEqual(got, want) { 100 t.Errorf("subject_token_type = %q, want %q", got, want) 101 } 102 if got, want := valuesP["subject_token"], subjectToken; !reflect.DeepEqual(got, want) { 103 t.Errorf("subject_token = %q, want %q", got, want) 104 } 105 if got, want := valuesP["scope"], scope; !reflect.DeepEqual(got, want) { 106 t.Errorf("scope = %q, want %q", got, want) 107 } 108 if got, want := valuesP["client_id"], []string{clientID}; !reflect.DeepEqual(got, want) { 109 t.Errorf("client_id = %q, want %q", got, want) 110 } 111 if got, want := valuesP["client_secret"], []string{clientSecret}; !reflect.DeepEqual(got, want) { 112 t.Errorf("client_secret = %q, want %q", got, want) 113 } 114 }