-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
work on the claim verification in the auth package
- Loading branch information
1 parent
ae0885f
commit e9830ba
Showing
7 changed files
with
125 additions
and
5 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
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 was deleted.
Oops, something went wrong.
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,47 @@ | ||
package auth | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func (auth *Authenticator) VerifyClaims(gc *gin.Context, token string) (name string, role string, err error) { | ||
verifier := auth.GetTokenVerifier() | ||
accessToken, err := verifier.Verify(gc, token) | ||
if err != nil { | ||
return "", "", errors.New(fmt.Sprintf("could not obtain token from cookie: %w", err)) | ||
} | ||
var claims map[string]any | ||
if err := accessToken.Claims(&claims); err != nil { | ||
return "", "", errors.New(fmt.Sprintf("could not map clains: %w", err)) | ||
} | ||
if _, ok := claims["iss"]; !ok { | ||
return "", "", errors.New("no issues in claim") | ||
} | ||
|
||
return "", "", nil | ||
} | ||
|
||
func (auth *Authenticator) mapClaimsToUser(claims map[string]any) (*User, error) { | ||
user := &User{} | ||
|
||
if username, ok := claims[auth.userclaimConfig.OIDCClaimUsernameField].(string); ok { | ||
user.Username = username | ||
} | ||
if email, ok := claims[auth.userclaimConfig.OIDCClaimEmailField].(string); ok { | ||
user.Email = email | ||
} | ||
if name, ok := claims[auth.userclaimConfig.OIDCClaimNameField].(string); ok { | ||
user.Name = name | ||
} | ||
if groups, ok := claims[auth.userclaimConfig.OIDCClaimGroupsField].([]interface{}); ok { | ||
user.Groups = make([]string, len(groups)) | ||
for i, g := range groups { | ||
user.Groups[i] = g.(string) | ||
} | ||
} | ||
|
||
return user, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package auth | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMapClaimsToUser_AllFieldsMappedCorrectly(t *testing.T) { | ||
UserClaimsConfig := UserClaimsConfig{ | ||
OIDCClaimUsernameField: "preferred_username", | ||
OIDCClaimEmailField: "email", | ||
OIDCClaimNameField: "name", | ||
OIDCClaimGroupsField: "groups", | ||
} | ||
claims := map[string]interface{}{ | ||
"preferred_username": "johndoe", | ||
"email": "[email protected]", | ||
"name": "John Doe", | ||
"groups": []interface{}{"users", "admins"}, | ||
} | ||
expectedUser := &User{ | ||
Username: "johndoe", | ||
Email: "[email protected]", | ||
Name: "John Doe", | ||
Groups: []string{"users", "admins"}, | ||
} | ||
|
||
auth := &Authenticator{ | ||
userclaimConfig: &UserClaimsConfig, | ||
} | ||
|
||
user, err := auth.mapClaimsToUser(claims) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, expectedUser, user) | ||
} |
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