Skip to content

Commit

Permalink
fix user group parsing for OAuth login (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
h44z committed Jan 21, 2025
1 parent 7a0a211 commit a04eaa4
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
6 changes: 5 additions & 1 deletion internal/app/auth/oauth_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ func getOauthFieldMapping(f config.OauthFields) config.OauthFields {
Phone: "phone",
Department: "department",
},
IsAdmin: "admin_flag",
IsAdmin: "admin_flag",
UserGroups: "", // by default, do not use user groups
}
if f.UserIdentifier != "" {
defaultMap.UserIdentifier = f.UserIdentifier
Expand All @@ -83,6 +84,9 @@ func getOauthFieldMapping(f config.OauthFields) config.OauthFields {
if f.IsAdmin != "" {
defaultMap.IsAdmin = f.IsAdmin
}
if f.UserGroups != "" {
defaultMap.UserGroups = f.UserGroups
}

return defaultMap
}
57 changes: 57 additions & 0 deletions internal/app/auth/oauth_common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package auth

import (
"encoding/json"
"testing"

"github.com/h44z/wg-portal/internal/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_parseOauthUserInfo(t *testing.T) {
userInfoStr := `
{
"at_hash": "REDACTED",
"aud": "REDACTED",
"c_hash": "REDACTED",
"email": "[email protected]",
"email_verified": true,
"exp": 1737404259,
"groups": [
"[email protected]",
"[email protected]",
"[email protected]"
],
"iat": 1737317859,
"iss": "https://dex.mydomain.net",
"name": "Test User",
"nonce": "REDACTED",
"sub": "REDACTED"
}
`

userInfo := map[string]interface{}{}
err := json.Unmarshal([]byte(userInfoStr), &userInfo)
require.NoError(t, err)

fieldMapping := getOauthFieldMapping(config.OauthFields{
BaseFields: config.BaseFields{
UserIdentifier: "email",
Email: "email",
Firstname: "name",
Lastname: "family_name",
},
UserGroups: "groups",
})
adminMapping := &config.OauthAdminMapping{
AdminGroupRegex: "^[email protected]$",
}

info, err := parseOauthUserInfo(fieldMapping, adminMapping, userInfo)
assert.NoError(t, err)
assert.True(t, info.IsAdmin)
assert.Equal(t, info.Firstname, "Test User")
assert.Equal(t, info.Lastname, "")
assert.Equal(t, info.Email, "[email protected]")
}
11 changes: 11 additions & 0 deletions internal/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ func MapDefaultStringSlice(m map[string]interface{}, key string, dflt []string)
return dflt
} else {
switch v := tmp.(type) {
case []any:
result := make([]string, 0, len(v))
for _, elem := range v {
switch vElem := elem.(type) {
case string:
result = append(result, vElem)
default:
result = append(result, fmt.Sprintf("%v", vElem))
}
}
return result
case []string:
return v
case string:
Expand Down

0 comments on commit a04eaa4

Please sign in to comment.