use claims map instead of struct

Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
This commit is contained in:
Jörn Friedrich Dreyer
2021-07-22 09:20:36 +00:00
parent b148faada6
commit 1f3e963c29
18 changed files with 177 additions and 105 deletions
+15 -9
View File
@@ -98,16 +98,22 @@ func NewStaticSelector(cfg *config.StaticSelectorConf) Selector {
func NewMigrationSelector(cfg *config.MigrationSelectorConf, ss accounts.AccountsService) Selector {
var acc = ss
return func(ctx context.Context, r *http.Request) (s string, err error) {
var userID string
if claims := oidc.FromContext(r.Context()); claims != nil {
userID = claims.PreferredUsername
if _, err := acc.GetAccount(ctx, &accounts.GetAccountRequest{Id: userID}); err != nil {
return cfg.AccNotFoundPolicy, nil
}
return cfg.AccFoundPolicy, nil
var claims map[string]interface{}
if claims = oidc.FromContext(r.Context()); claims == nil {
return cfg.UnauthenticatedPolicy, nil
}
return cfg.UnauthenticatedPolicy, nil
var userID string
var ok bool
if userID, ok = claims[oidc.PreferredUsername].(string); !ok {
// TODO clarify: what if the user just has no username ...
return cfg.AccNotFoundPolicy, nil
}
if _, err := acc.GetAccount(ctx, &accounts.GetAccountRequest{Id: userID}); err != nil {
return cfg.AccNotFoundPolicy, nil
}
return cfg.AccFoundPolicy, nil
}
}
+4 -3
View File
@@ -69,7 +69,7 @@ func TestStaticSelector(t *testing.T) {
type testCase struct {
AccSvcShouldReturnError bool
Claims *oidc.StandardClaims
Claims map[string]interface{}
Expected string
}
@@ -80,8 +80,9 @@ func TestMigrationSelector(t *testing.T) {
UnauthenticatedPolicy: "unauth",
}
var tests = []testCase{
{true, &oidc.StandardClaims{PreferredUsername: "Hans"}, "not_found"},
{false, &oidc.StandardClaims{PreferredUsername: "Hans"}, "found"},
{true, map[string]interface{}{oidc.PreferredUsername: "Hans"}, "not_found"},
{true, map[string]interface{}{oidc.Email: "hans@example.test"}, "not_found"},
{false, map[string]interface{}{oidc.PreferredUsername: "Hans"}, "found"},
{false, nil, "unauth"},
}