@@ -103,6 +103,10 @@ var invalidHTTPClientConfigs = []struct {
103103 httpClientConfigFile : "testdata/http.conf.auth-creds-no-basic.bad.yaml" ,
104104 errMsg : `authorization type cannot be set to "basic", use "basic_auth" instead` ,
105105 },
106+ {
107+ httpClientConfigFile : "testdata/http.conf.oauth2-secret-and-file-set.bad.yml" ,
108+ errMsg : "at most one of oauth2 client_secret & client_secret_file must be configured" ,
109+ },
106110}
107111
108112func newTestServer (handler func (w http.ResponseWriter , r * http.Request )) (* httptest.Server , error ) {
@@ -1136,7 +1140,7 @@ endpoint_params:
11361140 t .Fatalf ("Got unmarshalled config %q, expected %q" , unmarshalledConfig , expectedConfig )
11371141 }
11381142
1139- rt := expectedConfig . NewOAuth2RoundTripper (context . Background () , http .DefaultTransport )
1143+ rt := NewOAuth2RoundTripper (& expectedConfig , http .DefaultTransport )
11401144
11411145 client := http.Client {
11421146 Transport : rt ,
@@ -1148,3 +1152,115 @@ endpoint_params:
11481152 t .Fatalf ("Expected authorization header to be 'Bearer 12345', got '%s'" , authorization )
11491153 }
11501154}
1155+
1156+ func TestOAuth2WithFile (t * testing.T ) {
1157+ var expectedAuth * string
1158+ var previousAuth string
1159+ tokenTS := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
1160+ auth := r .Header .Get ("Authorization" )
1161+ if auth != * expectedAuth {
1162+ t .Fatalf ("bad auth, expected %s, got %s" , * expectedAuth , auth )
1163+ }
1164+ if auth == previousAuth {
1165+ t .Fatal ("token endpoint called twice" )
1166+ }
1167+ previousAuth = auth
1168+ res , _ := json .Marshal (testServerResponse {
1169+ AccessToken : "12345" ,
1170+ TokenType : "Bearer" ,
1171+ })
1172+ w .Header ().Add ("Content-Type" , "application/json" )
1173+ _ , _ = w .Write (res )
1174+ }))
1175+ defer tokenTS .Close ()
1176+ ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
1177+ auth := r .Header .Get ("Authorization" )
1178+ if auth != "Bearer 12345" {
1179+ t .Fatalf ("bad auth, expected %s, got %s" , "Bearer 12345" , auth )
1180+ }
1181+ fmt .Fprintln (w , "Hello, client" )
1182+ }))
1183+ defer ts .Close ()
1184+
1185+ secretFile , err := ioutil .TempFile ("" , "oauth2_secret" )
1186+ if err != nil {
1187+ t .Fatal (err )
1188+ }
1189+ defer os .Remove (secretFile .Name ())
1190+
1191+ var yamlConfig = fmt .Sprintf (`
1192+ client_id: 1
1193+ client_secret_file: %s
1194+ scopes:
1195+ - A
1196+ - B
1197+ token_url: %s
1198+ endpoint_params:
1199+ hi: hello
1200+ ` , secretFile .Name (), tokenTS .URL )
1201+ expectedConfig := OAuth2 {
1202+ ClientID : "1" ,
1203+ ClientSecretFile : secretFile .Name (),
1204+ Scopes : []string {"A" , "B" },
1205+ EndpointParams : map [string ]string {"hi" : "hello" },
1206+ TokenURL : tokenTS .URL ,
1207+ }
1208+
1209+ var unmarshalledConfig OAuth2
1210+ err = yaml .Unmarshal ([]byte (yamlConfig ), & unmarshalledConfig )
1211+ if err != nil {
1212+ t .Fatalf ("Expected no error unmarshalling yaml, got %v" , err )
1213+ }
1214+ if ! reflect .DeepEqual (unmarshalledConfig , expectedConfig ) {
1215+ t .Fatalf ("Got unmarshalled config %q, expected %q" , unmarshalledConfig , expectedConfig )
1216+ }
1217+
1218+ rt := NewOAuth2RoundTripper (& expectedConfig , http .DefaultTransport )
1219+
1220+ client := http.Client {
1221+ Transport : rt ,
1222+ }
1223+
1224+ tk := "Basic MToxMjM0NTY="
1225+ expectedAuth = & tk
1226+ if _ , err := secretFile .Write ([]byte ("123456" )); err != nil {
1227+ t .Fatal (err )
1228+ }
1229+ resp , err := client .Get (ts .URL )
1230+ if err != nil {
1231+ t .Fatal (err )
1232+ }
1233+
1234+ authorization := resp .Request .Header .Get ("Authorization" )
1235+ if authorization != "Bearer 12345" {
1236+ t .Fatalf ("Expected authorization header to be 'Bearer 12345', got '%s'" , authorization )
1237+ }
1238+
1239+ // Making a second request with the same file content should not re-call the token API.
1240+ resp , err = client .Get (ts .URL )
1241+ if err != nil {
1242+ t .Fatal (err )
1243+ }
1244+
1245+ tk = "Basic MToxMjM0NTY3"
1246+ expectedAuth = & tk
1247+ if _ , err := secretFile .Write ([]byte ("7" )); err != nil {
1248+ t .Fatal (err )
1249+ }
1250+
1251+ _ , err = client .Get (ts .URL )
1252+ if err != nil {
1253+ t .Fatal (err )
1254+ }
1255+
1256+ // Making a second request with the same file content should not re-call the token API.
1257+ _ , err = client .Get (ts .URL )
1258+ if err != nil {
1259+ t .Fatal (err )
1260+ }
1261+
1262+ authorization = resp .Request .Header .Get ("Authorization" )
1263+ if authorization != "Bearer 12345" {
1264+ t .Fatalf ("Expected authorization header to be 'Bearer 12345', got '%s'" , authorization )
1265+ }
1266+ }
0 commit comments