Skip to content

Commit 1e1fbf6

Browse files
committed
added custom header
1 parent 5fd4241 commit 1e1fbf6

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

Diff for: clientcredentials/clientcredentials.go

+10
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ type Config struct {
5151
// authStyleCache caches which auth style to use when Endpoint.AuthStyle is
5252
// the zero value (AuthStyleAutoDetect).
5353
authStyleCache internal.LazyAuthStyleCache
54+
55+
// CustomTokenHeaderKey - If set, Instead of `Authorization` this will be used as header key
56+
CustomTokenHeaderKey string
57+
58+
// CustomTokenPrefix - If set, Instead of `Bearer ` prefix / token type retrieved, this will be used
59+
CustomTokenPrefix string
5460
}
5561

5662
// Token uses client credentials to retrieve a token.
@@ -120,5 +126,9 @@ func (c *tokenSource) Token() (*oauth2.Token, error) {
120126
RefreshToken: tk.RefreshToken,
121127
Expiry: tk.Expiry,
122128
}
129+
if c.conf != nil {
130+
t.CustomTokenHeaderKey = c.conf.CustomTokenHeaderKey
131+
t.CustomTokenPrefix = c.conf.CustomTokenPrefix
132+
}
123133
return t.WithExtra(tk.Raw), nil
124134
}

Diff for: jwt/jwt.go

+10
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ type Config struct {
7474
// UseIDToken optionally specifies whether ID token should be used instead
7575
// of access token when the server returns both.
7676
UseIDToken bool
77+
78+
// CustomTokenHeaderKey - If set, Instead of `Authorization` this will be used as header key
79+
CustomTokenHeaderKey string
80+
81+
// CustomTokenPrefix - If set, Instead of `Bearer ` prefix / token type retrieved, this will be used
82+
CustomTokenPrefix string
7783
}
7884

7985
// TokenSource returns a JWT TokenSource using the configuration
@@ -181,5 +187,9 @@ func (js jwtSource) Token() (*oauth2.Token, error) {
181187
}
182188
token.AccessToken = tokenRes.IDToken
183189
}
190+
if js.conf != nil {
191+
token.CustomTokenHeaderKey = js.conf.CustomTokenHeaderKey
192+
token.CustomTokenPrefix = js.conf.CustomTokenPrefix
193+
}
184194
return token, nil
185195
}

Diff for: token.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,19 @@ type Token struct {
5757
// expired, by subtracting from Expiry. If zero, defaultExpiryDelta
5858
// is used.
5959
expiryDelta time.Duration
60+
61+
// CustomTokenHeaderKey - If set, Instead of `Authorization` this will be used as header key
62+
CustomTokenHeaderKey string
63+
64+
// CustomTokenPrefix - If set, Instead of `Bearer ` prefix / token type retrieved, this will be used
65+
CustomTokenPrefix string
6066
}
6167

6268
// Type returns t.TokenType if non-empty, else "Bearer".
6369
func (t *Token) Type() string {
70+
if t.CustomTokenPrefix != "" {
71+
return ""
72+
}
6473
if strings.EqualFold(t.TokenType, "bearer") {
6574
return "Bearer"
6675
}
@@ -82,7 +91,15 @@ func (t *Token) Type() string {
8291
// This method is unnecessary when using Transport or an HTTP Client
8392
// returned by this package.
8493
func (t *Token) SetAuthHeader(r *http.Request) {
85-
r.Header.Set("Authorization", t.Type()+" "+t.AccessToken)
94+
headerKey := "Authorization"
95+
if strings.TrimSpace(t.CustomTokenHeaderKey) != "" {
96+
headerKey = t.CustomTokenHeaderKey
97+
}
98+
headerValue := strings.TrimSpace(strings.Join([]string{t.Type(), t.AccessToken}, " "))
99+
if t.CustomTokenPrefix != "" {
100+
headerValue = strings.TrimSpace(strings.Join([]string{t.CustomTokenPrefix, headerValue}, ""))
101+
}
102+
r.Header.Set(headerKey, headerValue)
86103
}
87104

88105
// WithExtra returns a new Token that's a clone of t, but using the

Diff for: token_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ func TestTokenTypeMethod(t *testing.T) {
7171
{name: "mac", tok: &Token{TokenType: "mac"}, want: "MAC"},
7272
{name: "mac-caps", tok: &Token{TokenType: "MAC"}, want: "MAC"},
7373
{name: "mac-mixed_case", tok: &Token{TokenType: "mAc"}, want: "MAC"},
74+
{name: "clear type", tok: &Token{TokenType: "Bearer", CustomTokenPrefix: " "}, want: ""},
75+
{name: "custom type", tok: &Token{TokenType: "mAC", CustomTokenPrefix: "something"}, want: ""},
7476
}
7577
for _, tc := range cases {
7678
if got, want := tc.tok.Type(), tc.want; got != want {

0 commit comments

Comments
 (0)