Skip to content

Commit c7f4076

Browse files
committed
added tests for proxy auth
1 parent d967f26 commit c7f4076

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

model/authentication.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,11 @@ type BearerAuthenticationPolicy struct {
185185
Use string `json:"use,omitempty" validate:"required_without=Token"`
186186
}
187187

188+
// ProxyBearerAuthenticationPolicy supports either an inline token or a secret reference (use).
189+
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Proxy-Authorization
188190
type ProxyBearerAuthenticationPolicy struct {
189191
Token string `json:"token,omitempty" validate:"required_without=Use,proxy_bearer_policy"`
190-
Use string `json:"use,omitempty" validate:"required"`
192+
Use string `json:"use,omitempty" validate:"required_without=Token"`
191193
}
192194

193195
// DigestAuthenticationPolicy supports either inline properties (username/password) or a secret reference (use).

model/authentication_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ func TestAuthenticationPolicy(t *testing.T) {
4949
expected: `{"digest":{"username":"digestUser","password":"digestPass"}}`,
5050
expectsErr: false,
5151
},
52+
{
53+
name: "Valid Proxy Bearer Authentication Inline",
54+
input: `{
55+
"proxy_bearer": {
56+
"token": "proxyToken123"
57+
}
58+
}`,
59+
expected: `{"proxy_bearer":{"token":"proxyToken123"}}`,
60+
expectsErr: false,
61+
},
5262
}
5363

5464
for _, tc := range testCases {
@@ -64,6 +74,9 @@ func TestAuthenticationPolicy(t *testing.T) {
6474
if authPolicy.Bearer != nil {
6575
err = validate.Struct(authPolicy.Bearer)
6676
}
77+
if authPolicy.ProxyBearer != nil {
78+
err = validate.Struct(authPolicy.ProxyBearer)
79+
}
6780
if authPolicy.Digest != nil {
6881
err = validate.Struct(authPolicy.Digest)
6982
}

model/validator.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func init() {
4343

4444
registerValidator("basic_policy", validateBasicPolicy)
4545
registerValidator("bearer_policy", validateBearerPolicy)
46+
registerValidator("proxy_bearer_policy", validateProxyBearerPolicy)
4647
registerValidator("digest_policy", validateDigestPolicy)
4748
registerValidator("oauth2_policy", validateOAuth2Policy)
4849
registerValidator("client_auth_type", validateOptionalOAuthClientAuthentication)
@@ -186,6 +187,18 @@ func validateBearerPolicy(fl validator.FieldLevel) bool {
186187
return true
187188
}
188189

190+
// validateProxyBearerPolicy ensures ProxyBearerAuthenticationPolicy has mutually exclusive fields set.
191+
func validateProxyBearerPolicy(fl validator.FieldLevel) bool {
192+
policy, ok := fl.Parent().Interface().(ProxyBearerAuthenticationPolicy)
193+
if !ok {
194+
return false
195+
}
196+
if policy.Token != "" && policy.Use != "" {
197+
return false
198+
}
199+
return true
200+
}
201+
189202
// validateDigestPolicy ensures DigestAuthenticationPolicy has mutually exclusive fields set.
190203
func validateDigestPolicy(fl validator.FieldLevel) bool {
191204
policy, ok := fl.Parent().Interface().(DigestAuthenticationPolicy)

0 commit comments

Comments
 (0)