Skip to content

Commit 4e3de93

Browse files
authored
Support custom authorization header for access token extraction (#63)
* add: Support custom authorization header for access token extraction Signed-off-by: fsul7o <[email protected]> * fix go.mod Signed-off-by: fsul7o <[email protected]> --------- Signed-off-by: fsul7o <[email protected]>
1 parent 4283744 commit 4e3de93

File tree

7 files changed

+77
-58
lines changed

7 files changed

+77
-58
lines changed

config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ type AccessToken struct {
279279
// CertOffsetDuration represents the certificate issue time offset when comparing with the issue time of the access token. (for usecase: new cert + old token)
280280
CertOffsetDuration string `yaml:"certOffsetDuration"`
281281

282-
// AccessTokenAuthHeader represents the request header key for extracting the access token. (gRPC only, Not supported in HTTP)
282+
// AccessTokenAuthHeader represents the request header key for extracting the access token.
283283
AccessTokenAuthHeader string `yaml:"accessTokenAuthHeader"`
284284
}
285285

config/config_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,9 @@ func TestNew(t *testing.T) {
213213
"common_name1": {"client_id1", "client_id2"},
214214
"common_name2": {"client_id1", "client_id2"},
215215
},
216-
CertBackdateDuration: "1h",
217-
CertOffsetDuration: "1h",
216+
CertBackdateDuration: "1h",
217+
CertOffsetDuration: "1h",
218+
AccessTokenAuthHeader: "Authorization",
218219
},
219220
RoleToken: RoleToken{
220221
Enable: true,

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.25.3
44

55
replace (
66
cloud.google.com/go => cloud.google.com/go v0.123.0
7-
github.com/AthenZ/athenz-authorizer/v5 => github.com/AthenZ/athenz-authorizer/v5 v5.7.0
7+
github.com/AthenZ/athenz-authorizer/v5 => github.com/AthenZ/athenz-authorizer/v5 v5.8.0
88
github.com/kpango/gache/v2 => github.com/kpango/gache/v2 v2.1.1
99
github.com/kpango/glg => github.com/kpango/glg v1.6.15
1010
github.com/mwitkow/grpc-proxy => github.com/mwitkow/grpc-proxy v0.0.0-20250813121105-2866842de9a5
@@ -53,7 +53,6 @@ require (
5353
github.com/json-iterator/go v1.1.12 // indirect
5454
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
5555
github.com/kpango/fastime v1.1.10 // indirect
56-
github.com/kr/text v0.2.0 // indirect
5756
github.com/lestrrat-go/blackmagic v1.0.4 // indirect
5857
github.com/lestrrat-go/dsig v1.0.0 // indirect
5958
github.com/lestrrat-go/dsig-secp256k1 v1.0.0 // indirect
@@ -82,7 +81,6 @@ require (
8281
go.opentelemetry.io/otel/trace v1.38.0 // indirect
8382
go.opentelemetry.io/proto/otlp v1.8.0 // indirect
8483
go.yaml.in/yaml/v2 v2.4.3 // indirect
85-
golang.org/x/crypto v0.43.0 // indirect
8684
golang.org/x/net v0.46.0 // indirect
8785
golang.org/x/sys v0.37.0 // indirect
8886
golang.org/x/text v0.30.0 // indirect

go.sum

Lines changed: 12 additions & 52 deletions
Large diffs are not rendered by default.

test/data/example_config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ authorization:
121121
- client_id2
122122
certBackdateDuration: 1h
123123
certOffsetDuration: 1h
124+
accessTokenAuthHeader: Authorization
124125
roleToken:
125126
enable: true
126127
roleAuthHeader: Athenz-Role-Auth

usecase/authz_proxyd.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ func newAuthzD(cfg config.Config) (service.Authorizationd, error) {
271271
var atOpts []authorizerd.Option
272272
var jwkOpts []authorizerd.Option
273273
if authzCfg.AccessToken.Enable {
274+
if authzCfg.AccessToken.AccessTokenAuthHeader == "" {
275+
authzCfg.AccessToken.AccessTokenAuthHeader = "Authorization"
276+
}
274277
atOpts = []authorizerd.Option{
275278
authorizerd.WithAccessTokenParam(
276279
authorizerd.NewAccessTokenParam(
@@ -280,6 +283,7 @@ func newAuthzD(cfg config.Config) (service.Authorizationd, error) {
280283
authzCfg.AccessToken.CertOffsetDuration,
281284
authzCfg.AccessToken.VerifyClientID,
282285
authzCfg.AccessToken.AuthorizedClientIDs,
286+
authzCfg.AccessToken.AccessTokenAuthHeader,
283287
),
284288
),
285289
}
@@ -300,6 +304,7 @@ func newAuthzD(cfg config.Config) (service.Authorizationd, error) {
300304
"0h",
301305
false,
302306
nil,
307+
"",
303308
),
304309
),
305310
}

usecase/authz_proxyd_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,60 @@ func Test_newAuthzD(t *testing.T) {
10541054
},
10551055
want: true,
10561056
},
1057+
{
1058+
name: "test success access token enable with custom AccessTokenAuthHeader",
1059+
args: args{
1060+
cfg: config.Config{
1061+
Authorization: config.Authorization{
1062+
PublicKey: config.PublicKey{
1063+
SysAuthDomain: "10s",
1064+
ETagExpiry: "10s",
1065+
ETagPurgePeriod: "10s",
1066+
},
1067+
Policy: config.Policy{
1068+
ExpiryMargin: "10s",
1069+
RefreshPeriod: "10s",
1070+
PurgePeriod: "10s",
1071+
},
1072+
AccessToken: config.AccessToken{
1073+
Enable: true,
1074+
AccessTokenAuthHeader: "X-Auth-Token",
1075+
},
1076+
},
1077+
Athenz: config.Athenz{
1078+
URL: "dummy-athenz-url",
1079+
},
1080+
},
1081+
},
1082+
want: true,
1083+
},
1084+
{
1085+
name: "test success access token enable with empty AccessTokenAuthHeader defaults to Authorization",
1086+
args: args{
1087+
cfg: config.Config{
1088+
Authorization: config.Authorization{
1089+
PublicKey: config.PublicKey{
1090+
SysAuthDomain: "10s",
1091+
ETagExpiry: "10s",
1092+
ETagPurgePeriod: "10s",
1093+
},
1094+
Policy: config.Policy{
1095+
ExpiryMargin: "10s",
1096+
RefreshPeriod: "10s",
1097+
PurgePeriod: "10s",
1098+
},
1099+
AccessToken: config.AccessToken{
1100+
Enable: true,
1101+
AccessTokenAuthHeader: "",
1102+
},
1103+
},
1104+
Athenz: config.Athenz{
1105+
URL: "dummy-athenz-url",
1106+
},
1107+
},
1108+
},
1109+
want: true,
1110+
},
10571111
{
10581112
name: "test success policy disable",
10591113
args: args{

0 commit comments

Comments
 (0)