mirror of
https://github.com/coder/coder.git
synced 2025-07-18 14:17:22 +00:00
feat(coderd): set full name from IDP name claim (#13468)
* Updates OIDC and GitHub OAuth login to fetch set name from relevant claim fields * Adds CODER_OIDC_NAME_FIELD as configurable source of user name claim * Adds httpapi function to normalize a username such that it will pass validation * Adds firstName / lastName fields to dev OIDC setup
This commit is contained in:
@ -91,3 +91,14 @@ func UserRealNameValid(str string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// NormalizeUserRealName normalizes a user name such that it will pass
|
||||
// validation by UserRealNameValid. This is done to avoid blocking
|
||||
// little Bobby Whitespace from using Coder.
|
||||
func NormalizeRealUsername(str string) string {
|
||||
s := strings.TrimSpace(str)
|
||||
if len(s) > 128 {
|
||||
s = s[:128]
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
@ -1,9 +1,11 @@
|
||||
package httpapi_test
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/moby/moby/pkg/namesgenerator"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/coder/coder/v2/coderd/httpapi"
|
||||
@ -217,6 +219,10 @@ func TestUserRealNameValid(t *testing.T) {
|
||||
Name string
|
||||
Valid bool
|
||||
}{
|
||||
{"", true},
|
||||
{" a", false},
|
||||
{"a ", false},
|
||||
{" a ", false},
|
||||
{"1", true},
|
||||
{"A", true},
|
||||
{"A1", true},
|
||||
@ -229,17 +235,22 @@ func TestUserRealNameValid(t *testing.T) {
|
||||
{"Małgorzata Kalinowska-Iszkowska", true},
|
||||
{"成龍", true},
|
||||
{". .", true},
|
||||
|
||||
{"Lord Voldemort ", false},
|
||||
{" Bellatrix Lestrange", false},
|
||||
{" ", false},
|
||||
{strings.Repeat("a", 128), true},
|
||||
{strings.Repeat("a", 129), false},
|
||||
}
|
||||
for _, testCase := range testCases {
|
||||
testCase := testCase
|
||||
t.Run(testCase.Name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
valid := httpapi.UserRealNameValid(testCase.Name)
|
||||
require.Equal(t, testCase.Valid, valid == nil)
|
||||
err := httpapi.UserRealNameValid(testCase.Name)
|
||||
norm := httpapi.NormalizeRealUsername(testCase.Name)
|
||||
normErr := httpapi.UserRealNameValid(norm)
|
||||
assert.NoError(t, normErr)
|
||||
assert.Equal(t, testCase.Valid, err == nil)
|
||||
assert.Equal(t, testCase.Valid, norm == testCase.Name, "invalid name should be different after normalization")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user