-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow for multiple keysets configured (#277)
- Loading branch information
Showing
7 changed files
with
415 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package jwtauth | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/hashicorp/cap/jwt" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func Test_jwtAuthBackend_jwtValidator(t *testing.T) { | ||
type args struct { | ||
config *jwtConfig | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *jwt.Validator | ||
expectedErr string | ||
}{ | ||
{ | ||
name: "invalid ca pem", | ||
args: args{ | ||
config: &jwtConfig{ | ||
JWKSPairs: []interface{}{ | ||
map[string]any{ | ||
"jwks_url": "https://www.foobar.com/something", | ||
"jwks_ca_pem": "defg", | ||
}, | ||
map[string]any{ | ||
"jwks_url": "https://www.barbaz.com/something", | ||
"jwks_ca_pem": "", | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedErr: "could not parse CA PEM value successfully", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
b := &jwtAuthBackend{} | ||
b.providerCtx = context.TODO() | ||
|
||
got, err := b.jwtValidator(tt.args.config) | ||
if tt.expectedErr != "" { | ||
require.ErrorContains(t, err, tt.expectedErr) | ||
return | ||
} | ||
assert.Equalf(t, tt.want, got, "jwtValidator(%v)", tt.args.config) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package jwtauth | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/mitchellh/mapstructure" | ||
) | ||
|
||
type JWKSPair struct { | ||
JWKSUrl string `mapstructure:"jwks_url"` | ||
JWKSCAPEM string `mapstructure:"jwks_ca_pem"` | ||
} | ||
|
||
func NewJWKSPairsConfig(jc *jwtConfig) ([]*JWKSPair, error) { | ||
if len(jc.JWKSPairs) <= 0 { | ||
return nil, nil | ||
} | ||
|
||
pairs := make([]*JWKSPair, 0, len(jc.JWKSPairs)) | ||
for i := 0; i < len(jc.JWKSPairs); i++ { | ||
pairsMap, ok := jc.JWKSPairs[i].(map[string]interface{}) | ||
if !ok { | ||
return nil, fmt.Errorf("jwks_pairs must be provided as a list of json objects with the fields jwks_url and jwks_ca_pem") | ||
} | ||
jp, err := Initialize(pairsMap) | ||
if err != nil { | ||
return nil, err | ||
} | ||
pairs = append(pairs, jp) | ||
} | ||
|
||
return pairs, nil | ||
} | ||
|
||
func Initialize(jp map[string]interface{}) (*JWKSPair, error) { | ||
var newJp JWKSPair | ||
if err := mapstructure.Decode(jp, &newJp); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &newJp, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.