-
Notifications
You must be signed in to change notification settings - Fork 38
fix(sdk): fixes Manifests JSONs with OIDC #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
ddb8b9d
0cb12fc
542b78d
ab49c71
7fe50ed
247cea9
9bf2aec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,13 @@ | ||
| package sdk | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "github.com/opentdf/opentdf-v2-poc/internal/crypto" | ||
| "io" | ||
| "net/http" | ||
| "net/url" | ||
| "strings" | ||
| ) | ||
|
|
||
| type AuthConfig struct { | ||
|
|
@@ -30,3 +35,45 @@ func NewAuthConfig() (*AuthConfig, error) { | |
|
|
||
| return &AuthConfig{signingPublicKey: publicKey, signingPrivateKey: privateKey}, nil | ||
| } | ||
|
|
||
| func NewOidcAuthConfig(host, realm, clientId, clientSecret, subjectToken string) (*AuthConfig, error) { | ||
| authConfig, err := NewAuthConfig() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("Failed to create auth config:%w", err) | ||
|
imdominicreed marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| authConfig.authToken, err = authConfig.fetchOIDCAccessToken(host, realm, clientId, clientSecret, subjectToken) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("Failed to fetch acces token:%w", err) | ||
| } | ||
| return authConfig, nil | ||
| } | ||
| func (a *AuthConfig) fetchOIDCAccessToken(host, realm, clientId, clientSecret, subjectToken string) (string, error) { | ||
| data := url.Values{"grant_type": {"urn:ietf:params:oauth:grant-type:token-exchange"}, "client_id": {clientId}, "client_secret": {clientSecret}, "subject_token": {subjectToken}, "requested_token_type": {"urn:ietf:params:oauth:token-type:access_token"}} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. did you run go fmt on this?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did... i wasnt sure if i shouldve done it manually, as it slightly annoyed me, but i left it as is. |
||
|
|
||
| body := strings.NewReader(data.Encode()) | ||
| kcURL := fmt.Sprintf("%s/auth/realms/%s/protocol/openid-connect/token", host, realm) | ||
|
|
||
| req, err := http.NewRequest(http.MethodPost, kcURL, body) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
|
|
||
| certB64 := crypto.Base64Encode([]byte(a.signingPublicKey)) | ||
| req.Header.Set("X-VirtruPubKey", string(certB64)) | ||
|
|
||
| client := &http.Client{} | ||
| resp, err := client.Do(req) | ||
| type keycloakResponsePayload struct { | ||
| AccessToken string `json:"access_token"` | ||
| TokenType string `json:"token_type"` | ||
| } | ||
| keyResp := keycloakResponsePayload{} | ||
| respBody, _ := io.ReadAll(resp.Body) | ||
| err = json.Unmarshal(respBody, &keyResp) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return "Bearer " + keyResp.AccessToken, nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| package sdk | ||
|
|
||
| import ( | ||
| "io" | ||
| "net" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "net/url" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestNewOidcAuthConfig(t *testing.T) { | ||
| expectedAccessToken := "Bearer fail" | ||
| clientId := "idk" | ||
| clientSecret := "secret password" | ||
| subjectToken := "token" | ||
| realm := "tdf" | ||
| urlVals := url.Values{"grant_type": {"urn:ietf:params:oauth:grant-type:token-exchange"}, "client_id": {clientId}, "client_secret": {clientSecret}, "subject_token": {subjectToken}, "requested_token_type": {"urn:ietf:params:oauth:token-type:access_token"}} | ||
| expectedBody := urlVals.Encode() | ||
|
|
||
| s := httptest.NewServer( | ||
| http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||
| body, err := io.ReadAll(req.Body) | ||
| if "" == req.Header.Get("X-VirtruPubKey") || err != nil || string(body) != expectedBody { | ||
| w.WriteHeader(400) | ||
| return | ||
| } | ||
|
|
||
| _, _ = w.Write([]byte("{\"access_token\": \"fail\", \"token_type\": \"ok\"}")) | ||
| w.WriteHeader(200) | ||
| }), | ||
| ) | ||
| defer s.Close() | ||
| u, _ := url.Parse(s.URL) | ||
| host, port, _ := net.SplitHostPort(u.Host) | ||
|
|
||
| authConfig, err := NewOidcAuthConfig("http://"+host+":"+port, realm, clientId, clientSecret, subjectToken) | ||
|
|
||
| if err != nil { | ||
| t.Fatalf("authconfig failed: %v", err) | ||
| } | ||
|
|
||
| if authConfig.authToken != expectedAccessToken { | ||
| t.Fatalf("Auth token expected %s recived %s", expectedAccessToken, authConfig.authToken) | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.