mirror of
https://github.com/juanfont/headscale.git
synced 2025-09-06 17:54:31 +02:00
Fix: Improve OIDC username fallback and email parsing
This commit is contained in:
parent
243112bc6e
commit
c4062c425d
@ -326,15 +326,25 @@ func (u *User) FromClaim(claims *OIDCClaims) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !assignedName && claims.Email != "" {
|
if !assignedName && claims.Email != "" {
|
||||||
emailPrefix := strings.Split(claims.Email, "@")[0]
|
// Attempt to parse the email to ensure it's well-formed before extracting the prefix.
|
||||||
if emailPrefix != "" {
|
// This also helps to ensure claims.Email is a simple address without display name,
|
||||||
err := util.ValidateUsername(emailPrefix)
|
// making the subsequent split more reliable.
|
||||||
if err == nil {
|
_, parseErr := mail.ParseAddress(claims.Email)
|
||||||
// Ensure uniqueness of the extracted email prefix if it's to be used as a primary username.
|
if parseErr == nil {
|
||||||
u.Name = emailPrefix
|
// If email is parsable, extract the local part (before '@').
|
||||||
} else {
|
parts := strings.Split(claims.Email, "@")
|
||||||
log.Debug().Err(err).Msgf("Extracted email prefix %s is not a valid username", emailPrefix)
|
if len(parts) > 0 && parts[0] != "" {
|
||||||
|
emailPrefix := parts[0]
|
||||||
|
valErr := util.ValidateUsername(emailPrefix)
|
||||||
|
if valErr == nil {
|
||||||
|
// For OIDC users, Name uniqueness is often scoped with ProviderIdentifier.
|
||||||
|
u.Name = emailPrefix
|
||||||
|
} else {
|
||||||
|
log.Debug().Err(valErr).Msgf("Extracted email prefix '%s' from '%s' is not a valid username", emailPrefix, claims.Email)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
log.Debug().Err(parseErr).Msgf("Could not parse claims.Email '%s' to extract prefix for username", claims.Email)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user