Skip to content

Commit a266313

Browse files
committed
Get user full name from Apple Sign-In
1 parent 15b24f5 commit a266313

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

providers/apple/apple.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,12 @@ func (Provider) UnmarshalSession(data string) (goth.Session, error) {
121121
return s, err
122122
}
123123

124-
// Apple doesn't seem to provide a user profile endpoint like all the other providers do.
125-
// Therefore this will return a User with the unique identifier obtained through authorization
126-
// as the only identifying attribute.
127124
// A full name and email can be obtained from the form post response (parameter 'user')
128125
// to the redirect page following authentication, if the name and email scopes are requested.
129126
// Additionally, if the response type is form_post and the email scope is requested, the email
130127
// will be encoded into the ID token in the email claim.
128+
// Note that the full name is only provided on the very first authentication of a user, and
129+
// subsequent authentications will not include the full name, even if requested.
131130
func (p Provider) FetchUser(session goth.Session) (goth.User, error) {
132131
s := session.(*Session)
133132
if s.AccessToken == "" {
@@ -140,6 +139,8 @@ func (p Provider) FetchUser(session goth.Session) (goth.User, error) {
140139
AccessToken: s.AccessToken,
141140
RefreshToken: s.RefreshToken,
142141
ExpiresAt: s.ExpiresAt,
142+
FirstName: s.FirstName,
143+
LastName: s.LastName,
143144
}, nil
144145
}
145146

providers/apple/apple_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func TestAuthorize(t *testing.T) {
9393

9494
_, err := session.Authorize(p, url.Values{
9595
"code": []string{"<authorization code from successful authentication>"},
96+
"user": []string{"{\"first_name\":\"John\",\"last_name\":\"Doe\"}"},
9697
})
9798
if err != nil {
9899
errStr := err.Error()

providers/apple/session.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ type Session struct {
3232
AccessToken string
3333
RefreshToken string
3434
ExpiresAt time.Time
35+
FirstName string
36+
LastName string
3537
ID
3638
}
3739

@@ -119,6 +121,21 @@ func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string,
119121
IsPrivateEmail: idToken.Claims.(*IDTokenClaims).IsPrivateEmail.Value(),
120122
EmailVerified: idToken.Claims.(*IDTokenClaims).EmailVerified.Value(),
121123
}
124+
125+
// fetch the user first and last name from the params if available.
126+
if user := params.Get("user"); user != "" {
127+
var userData struct {
128+
Name struct {
129+
FirstName string `json:"firstName"`
130+
LastName string `json:"lastName"`
131+
} `json:"name"`
132+
Email string `json:"email"`
133+
}
134+
if err := json.Unmarshal([]byte(user), &userData); err == nil {
135+
s.FirstName = userData.Name.FirstName
136+
s.LastName = userData.Name.LastName
137+
}
138+
}
122139
}
123140

124141
return token.AccessToken, err

providers/apple/session_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func Test_ToJSON(t *testing.T) {
3737
s := &Session{}
3838

3939
data := s.Marshal()
40-
a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z","sub":"","email":"","is_private_email":false,"email_verified":false}`)
40+
a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z","FirstName":"","LastName":"","sub":"","email":"","is_private_email":false,"email_verified":false}`)
4141
}
4242

4343
func Test_String(t *testing.T) {

0 commit comments

Comments
 (0)