Skip to content

Commit

Permalink
User info roles field name (#42)
Browse files Browse the repository at this point in the history
* Sign Corporate CLA

* Allow defining the UserInfoRoles field name

* Add new field to validation list

---------

Co-authored-by: Ottenhus, Thomas <ottenhus@uni-mainz.de>
  • Loading branch information
moschlar and Ottenhus, Thomas committed Sep 4, 2023
1 parent 3b45bc2 commit 20ba1ef
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
8 changes: 7 additions & 1 deletion assets/cla/consent.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
- name: Lukas Vogel
email: vogel@anapaya.net
company: Anapaya Systems AG
- name: Thomas Ottenhus
email: ottenhus@uni-mainz.de
company: Johannes Gutenberg University Mainz
- name: Moritz Schlarb
email: schlarbm@uni-mainz.de
company: Johannes Gutenberg University Mainz

# By adding your name and email below, you I hereby consent to the Individual
# CLA provided in assets/cla/individual_cla.md.
Expand All @@ -30,4 +36,4 @@
- name: Sergiu Cozma
email: sergiucozma1994@gmail.com
- name: Riccardo Piola
email: riccardopiola@live.it
email: riccardopiola@live.it
1 change: 1 addition & 0 deletions pkg/idp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func (cfg *IdentityProviderConfig) Validate() error {
"email_claim_check_disabled",
"login_icon",
"user_info_fields",
"user_info_roles_field_name",
}
case "saml":
requiredFields = []string{
Expand Down
1 change: 1 addition & 0 deletions pkg/idp/oauth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ type Config struct {
LoginIcon *icons.LoginIcon `json:"login_icon,omitempty" xml:"login_icon,omitempty" yaml:"login_icon,omitempty"`

UserInfoFields []string `json:"user_info_fields,omitempty" xml:"user_info_fields,omitempty" yaml:"user_info_fields,omitempty"`
UserInfoRolesFieldName string `json:"user_info_roles_field_name,omitempty" xml:"user_info_roles_field_name,omitempty" yaml:"user_info_roles_field_name,omitempty"`

// The name of the cookie storing id_token from OAuth provider.
IdentityTokenCookieName string `json:"identity_token_cookie_name,omitempty" xml:"identity_token_cookie_name,omitempty" yaml:"identity_token_cookie_name,omitempty"`
Expand Down
7 changes: 7 additions & 0 deletions pkg/idp/oauth/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type IdentityProvider struct {
requiredTokenFields map[string]interface{}
scopeMap map[string]interface{}
userInfoFields map[string]interface{}
userInfoRolesFieldName string
// state stores cached state IDs
state *stateManager
logger *zap.Logger
Expand Down Expand Up @@ -227,6 +228,12 @@ func (b *IdentityProvider) Configure() error {
b.userInfoFields[fieldName] = true
}

if b.config.UserInfoRolesFieldName != "" {
b.userInfoRolesFieldName = b.config.UserInfoRolesFieldName
} else {
b.userInfoRolesFieldName = "roles"
}

// Configure user group filters, if any.
for _, pattern := range b.config.UserGroupFilters {
b.userGroupFilters = append(b.userGroupFilters, regexp.MustCompile(pattern))
Expand Down
8 changes: 4 additions & 4 deletions pkg/idp/oauth/user_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (b *IdentityProvider) fetchUserInfo(tokenData, userData map[string]interfac

var roles []string
if _, exists := b.userInfoFields["all"]; exists {
roles = extractUserInfoRoles(userinfo)
roles = extractUserInfoRoles(userinfo, b.userInfoRolesFieldName)
if len(userinfo) > 0 {
userData["userinfo"] = userinfo
}
Expand All @@ -96,7 +96,7 @@ func (b *IdentityProvider) fetchUserInfo(tokenData, userData map[string]interfac
}
}
if len(userinfo) > 0 {
roles = extractUserInfoRoles(userinfo)
roles = extractUserInfoRoles(userinfo, b.userInfoRolesFieldName)
if len(userinfo) > 0 {
userData["userinfo"] = userinfo
}
Expand All @@ -109,11 +109,11 @@ func (b *IdentityProvider) fetchUserInfo(tokenData, userData map[string]interfac
return nil
}

func extractUserInfoRoles(m map[string]interface{}) []string {
func extractUserInfoRoles(m map[string]interface{}, rolesFieldName string) []string {
entries := make(map[string]interface{})
var roles []string
for k, v := range m {
if !strings.HasSuffix(k, "roles") && !strings.HasSuffix(k, "groups") {
if !strings.HasSuffix(k, rolesFieldName) && !strings.HasSuffix(k, "groups") {
continue
}
switch values := v.(type) {
Expand Down

0 comments on commit 20ba1ef

Please sign in to comment.