Skip to content

Commit

Permalink
fix: unmarshal is_private_email correctly (supabase#1402)
Browse files Browse the repository at this point in the history
## What kind of change does this PR introduce?
* Apple returns either a boolean or string value in `is_private_email`
which is why we need to implement our own custom unmarshaler to deal
with it
(https://developer.apple.com/documentation/sign_in_with_apple/sign_in_with_apple_rest_api/authenticating_users_with_sign_in_with_apple)
  • Loading branch information
kangmingtay authored Feb 6, 2024
1 parent 8eedb95 commit 47df151
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
29 changes: 28 additions & 1 deletion internal/api/provider/apple.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"net/url"
"strconv"
"strings"

"github.com/coreos/go-oidc/v3/oidc"
Expand All @@ -17,10 +18,36 @@ const IssuerApple = "https://appleid.apple.com"
// AppleProvider stores the custom config for apple provider
type AppleProvider struct {
*oauth2.Config

oidc *oidc.Provider
}

type IsPrivateEmail bool

// Apple returns an is_private_email field that could be a string or boolean value so we need to implement a custom unmarshaler
// https://developer.apple.com/documentation/sign_in_with_apple/sign_in_with_apple_rest_api/authenticating_users_with_sign_in_with_apple
func (b *IsPrivateEmail) UnmarshalJSON(data []byte) error {
var boolVal bool
if err := json.Unmarshal(data, &boolVal); err == nil {
*b = IsPrivateEmail(boolVal)
return nil
}

// ignore the error and try to unmarshal as a string
var strVal string
if err := json.Unmarshal(data, &strVal); err != nil {
return err
}

var err error
boolVal, err = strconv.ParseBool(strVal)
if err != nil {
return err
}

*b = IsPrivateEmail(boolVal)
return nil
}

type appleName struct {
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
Expand Down
4 changes: 2 additions & 2 deletions internal/api/provider/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ type AppleIDTokenClaims struct {

Email string `json:"email"`

AuthTime *float64 `json:"auth_time"`
IsPrivateEmail *bool `json:"is_private_email,string"`
AuthTime *float64 `json:"auth_time"`
IsPrivateEmail *IsPrivateEmail `json:"is_private_email"`
}

func parseAppleIDToken(token *oidc.IDToken) (*oidc.IDToken, *UserProvidedData, error) {
Expand Down

0 comments on commit 47df151

Please sign in to comment.