@@ -2,6 +2,8 @@ package types
2
2
3
3
import (
4
4
"cmp"
5
+ "database/sql"
6
+ "net/mail"
5
7
"strconv"
6
8
7
9
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
@@ -34,7 +36,7 @@ type User struct {
34
36
// Unique identifier of the user from OIDC,
35
37
// comes from `sub` claim in the OIDC token
36
38
// and is used to lookup the user.
37
- ProviderIdentifier string `gorm:"index"`
39
+ ProviderIdentifier sql. NullString `gorm:"index"`
38
40
39
41
// Provider is the origin of the user account,
40
42
// same as RegistrationMethod, without authkey.
@@ -51,7 +53,7 @@ type User struct {
51
53
// should be used throughout headscale, in information returned to the
52
54
// user and the Policy engine.
53
55
func (u * User ) Username () string {
54
- return cmp .Or (u .Email , u .Name , u .ProviderIdentifier , strconv .FormatUint (uint64 (u .ID ), 10 ))
56
+ return cmp .Or (u .Email , u .Name , u .ProviderIdentifier . String , strconv .FormatUint (uint64 (u .ID ), 10 ))
55
57
}
56
58
57
59
// DisplayNameOrUsername returns the DisplayName if it exists, otherwise
@@ -107,7 +109,7 @@ func (u *User) Proto() *v1.User {
107
109
CreatedAt : timestamppb .New (u .CreatedAt ),
108
110
DisplayName : u .DisplayName ,
109
111
Email : u .Email ,
110
- ProviderId : u .ProviderIdentifier ,
112
+ ProviderId : u .ProviderIdentifier . String ,
111
113
Provider : u .Provider ,
112
114
ProfilePicUrl : u .ProfilePicURL ,
113
115
}
@@ -129,10 +131,20 @@ type OIDCClaims struct {
129
131
// FromClaim overrides a User from OIDC claims.
130
132
// All fields will be updated, except for the ID.
131
133
func (u * User ) FromClaim (claims * OIDCClaims ) {
132
- u .ProviderIdentifier = claims .Sub
134
+ err := util .CheckForFQDNRules (claims .Username )
135
+ if err == nil {
136
+ u .Name = claims .Username
137
+ }
138
+
139
+ if claims .EmailVerified {
140
+ _ , err = mail .ParseAddress (claims .Email )
141
+ if err == nil {
142
+ u .Email = claims .Email
143
+ }
144
+ }
145
+
146
+ u .ProviderIdentifier .String = claims .Sub
133
147
u .DisplayName = claims .Name
134
- u .Email = claims .Email
135
- u .Name = claims .Username
136
148
u .ProfilePicURL = claims .ProfilePictureURL
137
149
u .Provider = util .RegisterMethodOIDC
138
150
}
0 commit comments