-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
oauth config, template function, unit and apitest, added bounce-query… (
#52) * oauth config, template function, unit and apitest, added bounce-query to support it * Fixed typo Co-authored-by: Martin Rode <martin.rode@programmfabrik.de>
- Loading branch information
1 parent
cfdb289
commit 2805b3b
Showing
13 changed files
with
210 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
apitest: | ||
oauth_client: | ||
my_client: | ||
endpoint: | ||
token_url: "http://localhost:9999/bounce-query?access_token=mytoken" | ||
secret: "foobar" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package util | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"time" | ||
|
||
"golang.org/x/oauth2" | ||
) | ||
|
||
// OAuthClientsConfig is our config for multiple oAuth clients | ||
type OAuthClientsConfig map[string]OAuthClientConfig | ||
|
||
// OAuthClientConfig is our config for a single oAuth client | ||
type OAuthClientConfig struct { | ||
Key string | ||
Endpoint OAuthEndpointConfig `mapstructure:"endpoint"` | ||
Secret string `mapstructure:"secret"` | ||
} | ||
|
||
// OAuthEndpointConfig is our config for an oAuth endpoint | ||
type OAuthEndpointConfig struct { | ||
TokenURL string `mapstructure:"token_url"` | ||
} | ||
|
||
// GetAuthToken sends request to oAuth token endpoint | ||
// to get a token on behalf of a user | ||
func (c OAuthClientConfig) GetAuthToken(username string, password string) (*oauth2.Token, error) { | ||
cfg := oauth2.Config{ | ||
ClientID: c.Key, | ||
ClientSecret: c.Secret, | ||
Endpoint: oauth2.Endpoint{ | ||
TokenURL: c.Endpoint.TokenURL, | ||
}, | ||
} | ||
httpClient := &http.Client{Timeout: 5 * time.Second} | ||
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, httpClient) | ||
return cfg.PasswordCredentialsToken(ctx, username, password) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package util | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestGetToken(t *testing.T) { | ||
theToken := "thetoken" | ||
theClient := "my_client" | ||
theSecret := "foobar" | ||
basicAuth := theClient + ":" + theSecret | ||
basicAuthHeader := "Basic " + base64.StdEncoding.EncodeToString([]byte(basicAuth)) | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
authHeader := r.Header.Get("Authorization") | ||
if authHeader != basicAuthHeader { | ||
w.WriteHeader(403) | ||
return | ||
} | ||
fmt.Fprintf(w, `access_token=%s`, theToken) | ||
})) | ||
defer ts.Close() | ||
|
||
cfg := OAuthClientConfig{ | ||
Key: "nobody", | ||
Endpoint: OAuthEndpointConfig{ | ||
TokenURL: ts.URL, | ||
}, | ||
Secret: "whatever", | ||
} | ||
_, err := cfg.GetAuthToken("hey", "yo") | ||
if err == nil { | ||
t.Fatal("Expected error") | ||
} | ||
|
||
cfg = OAuthClientConfig{ | ||
Key: theClient, | ||
Endpoint: OAuthEndpointConfig{ | ||
TokenURL: ts.URL, | ||
}, | ||
Secret: "whatever", | ||
} | ||
_, err = cfg.GetAuthToken("hey", "yo") | ||
if err == nil { | ||
t.Fatal("Expected error") | ||
} | ||
|
||
cfg = OAuthClientConfig{ | ||
Key: theClient, | ||
Endpoint: OAuthEndpointConfig{ | ||
TokenURL: ts.URL, | ||
}, | ||
Secret: theSecret, | ||
} | ||
token, err := cfg.GetAuthToken("hey", "yo") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if token == nil { | ||
t.Fatal("No token nor error returned") | ||
} | ||
if token.AccessToken != theToken { | ||
t.Fatalf("Received token: %s , expected: %s", token.AccessToken, theToken) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "Bounce token", | ||
"request": { | ||
"server_url": "http://localhost:9999", | ||
"endpoint": "bounce-json", | ||
"method": "POST", | ||
"body": { | ||
"token": {{ datastore "access_token" | marshal }} | ||
} | ||
}, | ||
"response": { | ||
"statuscode": 200, | ||
"body": { | ||
"body": { | ||
"token": "mytoken" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"http_server": { | ||
"addr": ":9999", | ||
"dir": "./", | ||
"testmode": false, | ||
"proxy": { | ||
"oauth2": { | ||
"mode": "passthru" | ||
} | ||
} | ||
}, | ||
"name": "oauth2", | ||
"tests": [ | ||
"@store_token.json", | ||
"@bounce_token.json" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "Store oauth2 access token", | ||
"store": { | ||
"access_token": {{ oauth2_token "my_client" "user" "password" | marshal | qjson "access_token" }} | ||
} | ||
} |