Skip to content

Commit

Permalink
fix: handle token hook auth config
Browse files Browse the repository at this point in the history
  • Loading branch information
hperl committed Dec 15, 2023
1 parent fe260d1 commit 89806f6
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 27 deletions.
10 changes: 7 additions & 3 deletions driver/config/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package config

import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -482,8 +481,13 @@ func (p *DefaultProvider) AccessTokenStrategy(ctx context.Context, additionalSou

type (
Auth struct {
Type string `json:"type"`
Config json.RawMessage `json:"config"`
Type string `json:"type"`
Config AuthConfig `json:"config"`
}
AuthConfig struct {
In string `json:"in"`
Name string `json:"name"`
Value string `json:"value"`
}
HookConfig struct {
URL string `json:"url"`
Expand Down
23 changes: 15 additions & 8 deletions driver/config/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,18 +443,25 @@ func TestHookConfigs(t *testing.T) {
require.NotNil(t, hc)
assert.EqualValues(t, "http://localhost:8080/hook", hc.URL)

c.MustSet(ctx, key, map[string]any{
"url": "http://localhost:8080/hook2",
"auth": map[string]any{
"type": "api_key",
"config": json.RawMessage(`{"in":"header","name":"my-header","value":"my-value"}`),
},
})
c.MustSet(ctx, key, `
{
"url": "http://localhost:8080/hook2",
"auth": {
"type": "api_key",
"config": {
"in": "header",
"name": "my-header",
"value": "my-value"
}
}
}`)
hc = getFunc(ctx)
require.NotNil(t, hc)
assert.EqualValues(t, "http://localhost:8080/hook2", hc.URL)
assert.EqualValues(t, "api_key", hc.Auth.Type)
assert.JSONEq(t, `{"in":"header","name":"my-header","value":"my-value"}`, string(hc.Auth.Config))
rawConfig, err := json.Marshal(hc.Auth.Config)
require.NoError(t, err)
assert.JSONEq(t, `{"in":"header","name":"my-header","value":"my-value"}`, string(rawConfig))
}
}

Expand Down
8 changes: 6 additions & 2 deletions oauth2/oauth2_auth_code_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1006,8 +1006,12 @@ func TestAuthCodeWithDefaultStrategy(t *testing.T) {
reg.Config().MustSet(ctx, config.KeyTokenHook, &config.HookConfig{
URL: hs.URL,
Auth: &config.Auth{
Type: "api_key",
Config: json.RawMessage(`{"in": "header", "name": "Authorization", "value": "Bearer secret value"}`),
Type: "api_key",
Config: config.AuthConfig{
In: "header",
Name: "Authorization",
Value: "Bearer secret value",
},
},
})

Expand Down
8 changes: 6 additions & 2 deletions oauth2/oauth2_client_credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,12 @@ func TestClientCredentials(t *testing.T) {
reg.Config().MustSet(ctx, config.KeyTokenHook, &config.HookConfig{
URL: hs.URL,
Auth: &config.Auth{
Type: "api_key",
Config: json.RawMessage(`{"in": "header", "name": "Authorization", "value": "Bearer secret value"}`),
Type: "api_key",
Config: config.AuthConfig{
In: "header",
Name: "Authorization",
Value: "Bearer secret value",
},
},
})

Expand Down
15 changes: 3 additions & 12 deletions oauth2/token_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,11 @@ func applyAuth(req *retryablehttp.Request, auth *config.Auth) error {

switch auth.Type {
case "api_key":
c := struct {
In string `json:"in"`
Name string `json:"name"`
Value string `json:"value"`
}{}
if err := json.Unmarshal(auth.Config, &c); err != nil {
return err
}

switch c.In {
switch auth.Config.In {
case "header":
req.Header.Set(c.Name, c.Value)
req.Header.Set(auth.Config.Name, auth.Config.Value)
case "cookie":
req.AddCookie(&http.Cookie{Name: c.Name, Value: c.Value})
req.AddCookie(&http.Cookie{Name: auth.Config.Name, Value: auth.Config.Value})
}
default:
return errors.Errorf("unsupported auth type %q", auth.Type)
Expand Down

0 comments on commit 89806f6

Please sign in to comment.