Skip to content
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

Customise Authorization header #726

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions clientcredentials/clientcredentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ type Config struct {
// authStyleCache caches which auth style to use when Endpoint.AuthStyle is
// the zero value (AuthStyleAutoDetect).
authStyleCache internal.LazyAuthStyleCache

// CustomTokenHeaderKey - If set, Instead of `Authorization` this will be used as header key
CustomTokenHeaderKey string

// CustomTokenPrefix - If set, Instead of `Bearer ` prefix / token type retrieved, this will be used
CustomTokenPrefix string
}

// Token uses client credentials to retrieve a token.
Expand Down Expand Up @@ -120,5 +126,9 @@ func (c *tokenSource) Token() (*oauth2.Token, error) {
RefreshToken: tk.RefreshToken,
Expiry: tk.Expiry,
}
if c.conf != nil {
t.CustomTokenHeaderKey = c.conf.CustomTokenHeaderKey
t.CustomTokenPrefix = c.conf.CustomTokenPrefix
}
return t.WithExtra(tk.Raw), nil
}
10 changes: 10 additions & 0 deletions jwt/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ type Config struct {
// UseIDToken optionally specifies whether ID token should be used instead
// of access token when the server returns both.
UseIDToken bool

// CustomTokenHeaderKey - If set, Instead of `Authorization` this will be used as header key
CustomTokenHeaderKey string

// CustomTokenPrefix - If set, Instead of `Bearer ` prefix / token type retrieved, this will be used
CustomTokenPrefix string
}

// TokenSource returns a JWT TokenSource using the configuration
Expand Down Expand Up @@ -181,5 +187,9 @@ func (js jwtSource) Token() (*oauth2.Token, error) {
}
token.AccessToken = tokenRes.IDToken
}
if js.conf != nil {
token.CustomTokenHeaderKey = js.conf.CustomTokenHeaderKey
token.CustomTokenPrefix = js.conf.CustomTokenPrefix
}
return token, nil
}
19 changes: 18 additions & 1 deletion token.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,19 @@ type Token struct {
// expired, by subtracting from Expiry. If zero, defaultExpiryDelta
// is used.
expiryDelta time.Duration

// CustomTokenHeaderKey - If set, Instead of `Authorization` this will be used as header key
CustomTokenHeaderKey string

// CustomTokenPrefix - If set, Instead of `Bearer ` prefix / token type retrieved, this will be used
CustomTokenPrefix string
}

// Type returns t.TokenType if non-empty, else "Bearer".
func (t *Token) Type() string {
if t.CustomTokenPrefix != "" {
return ""
}
if strings.EqualFold(t.TokenType, "bearer") {
return "Bearer"
}
Expand All @@ -82,7 +91,15 @@ func (t *Token) Type() string {
// This method is unnecessary when using Transport or an HTTP Client
// returned by this package.
func (t *Token) SetAuthHeader(r *http.Request) {
r.Header.Set("Authorization", t.Type()+" "+t.AccessToken)
headerKey := "Authorization"
if strings.TrimSpace(t.CustomTokenHeaderKey) != "" {
headerKey = t.CustomTokenHeaderKey
}
headerValue := strings.TrimSpace(strings.Join([]string{t.Type(), t.AccessToken}, " "))
if t.CustomTokenPrefix != "" {
headerValue = strings.TrimSpace(strings.Join([]string{t.CustomTokenPrefix, headerValue}, ""))
}
r.Header.Set(headerKey, headerValue)
}

// WithExtra returns a new Token that's a clone of t, but using the
Expand Down
2 changes: 2 additions & 0 deletions token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ func TestTokenTypeMethod(t *testing.T) {
{name: "mac", tok: &Token{TokenType: "mac"}, want: "MAC"},
{name: "mac-caps", tok: &Token{TokenType: "MAC"}, want: "MAC"},
{name: "mac-mixed_case", tok: &Token{TokenType: "mAc"}, want: "MAC"},
{name: "clear type", tok: &Token{TokenType: "Bearer", CustomTokenPrefix: " "}, want: ""},
{name: "custom type", tok: &Token{TokenType: "mAC", CustomTokenPrefix: "something"}, want: ""},
}
for _, tc := range cases {
if got, want := tc.tok.Type(), tc.want; got != want {
Expand Down