forked from Mirrors/oauth2
google: update formatting and code structure
Change-Id: I9c45c7848de4ab2e3a685f761c85b1c9405b6eb1
This commit is contained in:
parent
801e5611ae
commit
5908a54aa5
|
@ -10,7 +10,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -33,35 +32,41 @@ func ExchangeToken(ctx context.Context, endpoint string, request *STSTokenExchan
|
||||||
fmt.Errorf("oauth2/google: failed to marshal additional options: %v", err)
|
fmt.Errorf("oauth2/google: failed to marshal additional options: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
data.Set("options", string(opts))
|
|
||||||
|
|
||||||
|
data.Set("options", string(opts))
|
||||||
authentication.InjectAuthentication(data, headers)
|
authentication.InjectAuthentication(data, headers)
|
||||||
encodedData := data.Encode()
|
encodedData := data.Encode()
|
||||||
|
|
||||||
req, err := http.NewRequestWithContext(ctx, "POST", endpoint, strings.NewReader(encodedData))
|
req, err := http.NewRequestWithContext(ctx, "POST", endpoint, strings.NewReader(encodedData))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Errorf("oauth2/google: failed to properly build http request: %v", err)
|
fmt.Errorf("oauth2/google: failed to properly build http request: %v", err)
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
for key, _ := range headers {
|
for key, list := range headers {
|
||||||
for _, val := range headers.Values(key) {
|
for _, val := range list {
|
||||||
req.Header.Add(key, val)
|
req.Header.Add(key, val)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
req.Header.Add("Content-Length", strconv.Itoa(len(encodedData)))
|
req.Header.Add("Content-Length", strconv.Itoa(len(encodedData)))
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Errorf("oauth2/google: invalid response from Secure Token Server: %v", err)
|
fmt.Errorf("oauth2/google: invalid response from Secure Token Server: %v", err)
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
body, err := ioutil.ReadAll(io.LimitReader(resp.Body, 1<<20))
|
|
||||||
if err != nil {
|
bodyJson := json.NewDecoder(io.LimitReader(resp.Body, 1<<20))
|
||||||
fmt.Errorf("oauth2/google: invalid body in Secure Token Server response: %v", err)
|
|
||||||
}
|
|
||||||
var stsResp STSTokenExchangeResponse
|
var stsResp STSTokenExchangeResponse
|
||||||
err = json.Unmarshal(body, &stsResp)
|
for bodyJson.More() {
|
||||||
|
err = bodyJson.Decode(&stsResp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Errorf("oauth2/google: failed to unmarshal response body from Secure Token Server: %v", err)
|
fmt.Errorf("oauth2/google: failed to unmarshal response body from Secure Token Server: %v", err)
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return &stsResp, nil
|
return &stsResp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,7 +34,7 @@ var tokenRequest = STSTokenExchangeRequest{
|
||||||
SubjectTokenType: "urn:ietf:params:oauth:token-type:jwt",
|
SubjectTokenType: "urn:ietf:params:oauth:token-type:jwt",
|
||||||
}
|
}
|
||||||
|
|
||||||
var requestbody = "audience=32555940559.apps.googleusercontent.com&grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Atoken-exchange&requested_token_type=urn%3Aietf%3Aparams%3Aoauth%3Atoken-type%3Aaccess_token&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdevstorage.full_control&subject_token=Sample.Subject.Token&subject_token_type=urn%3Aietf%3Aparams%3Aoauth%3Atoken-type%3Ajwt"
|
var requestbody = "audience=32555940559.apps.googleusercontent.com&grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Atoken-exchange&options=null&requested_token_type=urn%3Aietf%3Aparams%3Aoauth%3Atoken-type%3Aaccess_token&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdevstorage.full_control&subject_token=Sample.Subject.Token&subject_token_type=urn%3Aietf%3Aparams%3Aoauth%3Atoken-type%3Ajwt"
|
||||||
var responseBody = `{"access_token":"Sample.Access.Token","issued_token_type":"urn:ietf:params:oauth:token-type:access_token","token_type":"Bearer","expires_in":3600,"scope":"https://www.googleapis.com/auth/cloud-platform"}`
|
var responseBody = `{"access_token":"Sample.Access.Token","issued_token_type":"urn:ietf:params:oauth:token-type:access_token","token_type":"Bearer","expires_in":3600,"scope":"https://www.googleapis.com/auth/cloud-platform"}`
|
||||||
var expectedToken = STSTokenExchangeResponse{
|
var expectedToken = STSTokenExchangeResponse{
|
||||||
AccessToken: "Sample.Access.Token",
|
AccessToken: "Sample.Access.Token",
|
||||||
|
@ -48,34 +48,37 @@ var expectedToken = STSTokenExchangeResponse{
|
||||||
func TestExchangeToken(t *testing.T) {
|
func TestExchangeToken(t *testing.T) {
|
||||||
|
|
||||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != "POST" {
|
||||||
|
t.Errorf("Unexpected request method, %v is found", r.Method)
|
||||||
|
}
|
||||||
if r.URL.String() != "/" {
|
if r.URL.String() != "/" {
|
||||||
t.Errorf("Unexpected request URL, %v is found.", r.URL)
|
t.Errorf("Unexpected request URL, %v is found", r.URL)
|
||||||
}
|
}
|
||||||
headerAuth := r.Header.Get("Authorization")
|
headerAuth := r.Header.Get("Authorization")
|
||||||
if headerAuth != "Basic cmJyZ25vZ25yaG9uZ28zYmk0Z2I5Z2hnOWc6bm90c29zZWNyZXQ=" {
|
if headerAuth != "Basic cmJyZ25vZ25yaG9uZ28zYmk0Z2I5Z2hnOWc6bm90c29zZWNyZXQ=" {
|
||||||
t.Errorf("Unexpected autohrization header, %v is found.", headerAuth)
|
t.Errorf("Unexpected autohrization header, %v is found", headerAuth)
|
||||||
}
|
}
|
||||||
headerContentType := r.Header.Get("Content-Type")
|
headerContentType := r.Header.Get("Content-Type")
|
||||||
if headerContentType != "application/x-www-form-urlencoded" {
|
if headerContentType != "application/x-www-form-urlencoded" {
|
||||||
t.Errorf("Unexpected Content-Type header, %v is found.", headerContentType)
|
t.Errorf("Unexpected Content-Type header, %v is found", headerContentType)
|
||||||
}
|
}
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
body, err := ioutil.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed reading request body: %s.", err)
|
t.Errorf("Failed reading request body: %v.", err)
|
||||||
}
|
}
|
||||||
if string(body) != requestbody {
|
if string(body) != requestbody {
|
||||||
t.Errorf("Unexpected exchange payload, %v is found.", string(body))
|
t.Errorf("Unexpected exchange payload, %v is found", string(body))
|
||||||
}
|
}
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.Write([]byte(responseBody))
|
w.Write([]byte(responseBody))
|
||||||
}))
|
}))
|
||||||
|
|
||||||
headers := make(map[string][]string)
|
headers := http.Header{}
|
||||||
headers["Content-Type"] = []string{"application/x-www-form-urlencoded"}
|
headers.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
|
||||||
resp, err := ExchangeToken(context.Background(), ts.URL, &tokenRequest, auth, headers, nil)
|
resp, err := ExchangeToken(context.Background(), ts.URL, &tokenRequest, auth, headers, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("ExchangeToken failed with error: %s", err)
|
t.Fatalf("ExchangeToken failed with error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if diff := cmp.Diff(expectedToken, *resp); diff != "" {
|
if diff := cmp.Diff(expectedToken, *resp); diff != "" {
|
||||||
|
@ -90,8 +93,8 @@ func TestExchangeToken_Err(t *testing.T) {
|
||||||
w.Write([]byte("what's wrong with this response?"))
|
w.Write([]byte("what's wrong with this response?"))
|
||||||
}))
|
}))
|
||||||
|
|
||||||
headers := make(map[string][]string)
|
headers := http.Header{}
|
||||||
headers["Content-Type"] = []string{"application/x-www-form-urlencoded"}
|
headers.Add("Content-Type", "application/x-www-form-urlencoded")
|
||||||
_, err := ExchangeToken(context.Background(), ts.URL, &tokenRequest, auth, headers, nil)
|
_, err := ExchangeToken(context.Background(), ts.URL, &tokenRequest, auth, headers, nil)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("Expected handled error; instead got nil.")
|
t.Errorf("Expected handled error; instead got nil.")
|
||||||
|
|
Loading…
Reference in New Issue