Skip to content

Commit 615ec9f

Browse files
authored
[BACKEND-440] Raw token support for validator repository (#69)
1 parent a5f2271 commit 615ec9f

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

service/validator_repository.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ import (
1010
"github.com/pkg/errors"
1111
)
1212

13-
// JwtValidatorRepository contains a set of JWT validators and can return the appropriate one for a given request
14-
// The request must contain a valid JWT in the Authorization header ("Authorization: Bearer <token>")
13+
// JwtValidatorRepository contains a set of JWT validators and can return the appropriate one for a given request or raw JWT
14+
//
15+
// The request must contain a valid JWT in the Authorization header ("Authorization: Bearer <token>")
1516
// The validator is selected based on the "iss" claim in the JWT
1617
type JwtValidatorRepository interface {
1718
GetJwtValidatorForRequest(r *http.Request) (Validator, error)
19+
GetJwtValidatorForRawToken(rawJwt string) (Validator, error)
1820
}
1921

2022
// DefaultJwtValidatorRepository ...
@@ -36,14 +38,19 @@ func (vr *DefaultJwtValidatorRepository) GetJwtValidatorForRequest(r *http.Reque
3638
return nil, errors.New("failed to read JWT from header")
3739
}
3840

39-
iss, err := vr.getIssuerFromRawJWT(rawJwt[1])
41+
return vr.GetJwtValidatorForRawToken(rawJwt[1])
42+
}
43+
44+
// GetJwtValidatorForRawToken ...
45+
func (vr *DefaultJwtValidatorRepository) GetJwtValidatorForRawToken(rawJwt string) (Validator, error) {
46+
iss, err := vr.getIssuerFromRawJWT(rawJwt)
4047
if err != nil {
4148
return nil, errors.Wrap(err, "failed to get issuer form the JWT")
4249
}
4350

4451
validator := vr.JwtValidators[iss]
4552
if validator == nil {
46-
return nil, errors.New("there is no JWT validator for this issuer")
53+
return nil, fmt.Errorf("there is no JWT validator for issuer: %s", iss)
4754
}
4855

4956
return validator, nil

service/validator_repository_test.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,25 @@ const (
1515
tokenIssuerServiceIssuer = "https://token-issuer.bitrise.io/auth/realms/bitrise-services"
1616
)
1717

18-
func Test_GivenMatchingValidatorExists_ReturnsValidator(t *testing.T) {
18+
func Test_GetJwtValidatorForRawToken_GivenMatchingValidatorExists_ReturnsValidator(t *testing.T) {
19+
authServiceValidator := NewValidator(
20+
config.NewAudienceConfig("bitrise-api", "bitrise"),
21+
WithRealm("bitrise-services"))
22+
tokenIssuerServiceValidator := NewValidator(
23+
config.NewAudienceConfig("bitrise-api", "bitrise"),
24+
WithRealm("bitrise-services"))
25+
vr := NewJwtValidatorRepository(map[string]Validator{
26+
authServiceIssuer: authServiceValidator,
27+
tokenIssuerServiceIssuer: tokenIssuerServiceValidator,
28+
})
29+
30+
v, err := vr.GetJwtValidatorForRawToken(mocks.RawMockToken)
31+
assert.NoError(t, err)
32+
33+
assert.Equal(t, tokenIssuerServiceValidator, v)
34+
}
35+
36+
func Test_GetJwtValidatorForRequest_GivenMatchingValidatorExists_ReturnsValidator(t *testing.T) {
1937
authServiceValidator := NewValidator(
2038
config.NewAudienceConfig("bitrise-api", "bitrise"),
2139
WithRealm("bitrise-services"))
@@ -37,7 +55,7 @@ func Test_GivenMatchingValidatorExists_ReturnsValidator(t *testing.T) {
3755
assert.Equal(t, tokenIssuerServiceValidator, v)
3856
}
3957

40-
func Test_GivenNoMatchingValidatorExists_ReturnsError(t *testing.T) {
58+
func Test_GetJwtValidatorForRequest_GivenNoMatchingValidatorExists_ReturnsError(t *testing.T) {
4159
authServiceValidator := NewValidator(
4260
config.NewAudienceConfig("bitrise-api", "bitrise"),
4361
WithRealm("bitrise-services"))
@@ -50,10 +68,10 @@ func Test_GivenNoMatchingValidatorExists_ReturnsError(t *testing.T) {
5068
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", mocks.RawMockToken))
5169

5270
_, err = vr.GetJwtValidatorForRequest(request)
53-
assert.EqualError(t, err, "there is no JWT validator for this issuer")
71+
assert.EqualError(t, err, "there is no JWT validator for issuer: https://token-issuer.bitrise.io/auth/realms/bitrise-services")
5472
}
5573

56-
func Test_GivenInvalidAuthorizationHeader_ReturnsError(t *testing.T) {
74+
func Test_GetJwtValidatorForRequest_GivenInvalidAuthorizationHeader_ReturnsError(t *testing.T) {
5775
authServiceValidator := NewValidator(
5876
config.NewAudienceConfig("bitrise-api", "bitrise"),
5977
WithRealm("bitrise-services"))

0 commit comments

Comments
 (0)