Skip to content

Commit

Permalink
Merge pull request #303 from dstapleton92/add-email-apple-token
Browse files Browse the repository at this point in the history
Add email and is_private_email to Apple id_token
  • Loading branch information
bentranter authored Jan 16, 2020
2 parents 39cbb11 + 7cc1735 commit 9eac473
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
3 changes: 3 additions & 0 deletions providers/apple/apple.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ func (Provider) UnmarshalSession(data string) (goth.Session, error) {
// as the only identifying attribute.
// A full name and email can be obtained from the form post response
// to the redirect page following authentication, if the name are email scopes are requested.
// Additionally, if the response type is form_post and the email scope is requested, the email
// will be encoded into the ID token in the email claim.
func (p Provider) FetchUser(session goth.Session) (goth.User, error) {
s := session.(*Session)
if s.AccessToken == "" {
Expand All @@ -125,6 +127,7 @@ func (p Provider) FetchUser(session goth.Session) (goth.User, error) {
return goth.User{
Provider: p.Name(),
UserID: s.ID.Sub,
Email: s.ID.Email,
AccessToken: s.AccessToken,
RefreshToken: s.RefreshToken,
ExpiresAt: s.ExpiresAt,
Expand Down
10 changes: 8 additions & 2 deletions providers/apple/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ const (
)

type ID struct {
Sub string `json:"sub"`
Sub string `json:"sub"`
Email string `json:"email"`
IsPrivateEmail bool `json:"is_private_email"`
}

type Session struct {
Expand All @@ -47,6 +49,8 @@ type IDTokenClaims struct {
jwt.StandardClaims
AccessTokenHash string `json:"at_hash"`
AuthTime int `json:"auth_time"`
Email string `json:"email"`
IsPrivateEmail bool `json:"is_private_email,string"`
}

func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) {
Expand Down Expand Up @@ -112,7 +116,9 @@ func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string,
return "", err
}
s.ID = ID{
Sub: idToken.Claims.(*IDTokenClaims).Subject,
Sub: idToken.Claims.(*IDTokenClaims).Subject,
Email: idToken.Claims.(*IDTokenClaims).Email,
IsPrivateEmail: idToken.Claims.(*IDTokenClaims).IsPrivateEmail,
}
}

Expand Down
2 changes: 1 addition & 1 deletion providers/apple/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func Test_ToJSON(t *testing.T) {
s := &Session{}

data := s.Marshal()
a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z","sub":""}`)
a.Equal(data, `{"AuthURL":"","AccessToken":"","RefreshToken":"","ExpiresAt":"0001-01-01T00:00:00Z","sub":"","email":"","is_private_email":false}`)
}

func Test_String(t *testing.T) {
Expand Down

0 comments on commit 9eac473

Please sign in to comment.