1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-03-19 22:19:23 +02:00
pocketbase/tools/auth/apple.go

167 lines
4.5 KiB
Go
Raw Normal View History

2023-03-01 23:29:45 +02:00
package auth
import (
"context"
"encoding/json"
"errors"
"strings"
2025-01-05 11:05:26 +02:00
"github.com/golang-jwt/jwt/v5"
"github.com/pocketbase/pocketbase/tools/types"
2023-03-01 23:29:45 +02:00
"github.com/spf13/cast"
"golang.org/x/oauth2"
)
2024-09-29 19:23:19 +03:00
func init() {
Providers[NameApple] = wrapFactory(NewAppleProvider)
}
2023-03-01 23:29:45 +02:00
var _ Provider = (*Apple)(nil)
// NameApple is the unique name of the Apple provider.
const NameApple string = "apple"
// Apple allows authentication via Apple OAuth2.
//
// [OIDC differences]: https://bitbucket.org/openid/connect/src/master/How-Sign-in-with-Apple-differs-from-OpenID-Connect.md
type Apple struct {
2024-09-29 19:23:19 +03:00
BaseProvider
2023-03-01 23:29:45 +02:00
2024-09-29 19:23:19 +03:00
jwksURL string
2023-03-01 23:29:45 +02:00
}
// NewAppleProvider creates a new Apple provider instance with some defaults.
func NewAppleProvider() *Apple {
return &Apple{
2024-09-29 19:23:19 +03:00
BaseProvider: BaseProvider{
ctx: context.Background(),
displayName: "Apple",
pkce: true,
2024-06-14 11:41:19 +03:00
scopes: []string{"name", "email"},
2024-09-29 19:23:19 +03:00
authURL: "https://appleid.apple.com/auth/authorize",
tokenURL: "https://appleid.apple.com/auth/token",
2023-03-01 23:29:45 +02:00
},
2024-09-29 19:23:19 +03:00
jwksURL: "https://appleid.apple.com/auth/keys",
2023-03-01 23:29:45 +02:00
}
}
// FetchAuthUser returns an AuthUser instance based on the provided token.
//
// API reference: https://developer.apple.com/documentation/sign_in_with_apple/tokenresponse.
func (p *Apple) FetchAuthUser(token *oauth2.Token) (*AuthUser, error) {
2024-09-29 19:23:19 +03:00
data, err := p.FetchRawUserInfo(token)
2023-03-01 23:29:45 +02:00
if err != nil {
return nil, err
}
rawUser := map[string]any{}
if err := json.Unmarshal(data, &rawUser); err != nil {
return nil, err
}
extracted := struct {
Id string `json:"sub"`
2024-06-14 11:41:19 +03:00
Name string `json:"name"`
2023-03-01 23:29:45 +02:00
Email string `json:"email"`
EmailVerified any `json:"email_verified"` // could be string or bool
User struct {
Name struct {
FirstName string `json:"firstName"`
LastName string `json:"lastName"`
} `json:"name"`
} `json:"user"`
2023-03-01 23:29:45 +02:00
}{}
if err := json.Unmarshal(data, &extracted); err != nil {
return nil, err
}
user := &AuthUser{
Id: extracted.Id,
2024-06-14 11:41:19 +03:00
Name: extracted.Name,
2023-03-01 23:29:45 +02:00
RawUser: rawUser,
AccessToken: token.AccessToken,
RefreshToken: token.RefreshToken,
}
user.Expiry, _ = types.ParseDateTime(token.Expiry)
2023-03-01 23:29:45 +02:00
if cast.ToBool(extracted.EmailVerified) {
user.Email = extracted.Email
}
if user.Name == "" {
user.Name = strings.TrimSpace(extracted.User.Name.FirstName + " " + extracted.User.Name.LastName)
}
2023-03-01 23:29:45 +02:00
return user, nil
}
2024-09-29 19:23:19 +03:00
// FetchRawUserInfo implements Provider.FetchRawUserInfo interface.
2023-03-01 23:29:45 +02:00
//
// Apple doesn't have a UserInfo endpoint and claims about users
// are instead included in the "id_token" (https://openid.net/specs/openid-connect-core-1_0.html#id_tokenExample)
2024-09-29 19:23:19 +03:00
func (p *Apple) FetchRawUserInfo(token *oauth2.Token) ([]byte, error) {
2023-03-01 23:29:45 +02:00
idToken, _ := token.Extra("id_token").(string)
claims, err := p.parseAndVerifyIdToken(idToken)
if err != nil {
return nil, err
}
// Apple only returns the user object the first time the user authorizes the app
// https://developer.apple.com/documentation/sign_in_with_apple/sign_in_with_apple_js/configuring_your_webpage_for_sign_in_with_apple#3331292
rawUser, _ := token.Extra("user").(string)
if rawUser != "" {
user := map[string]any{}
err = json.Unmarshal([]byte(rawUser), &user)
if err != nil {
return nil, err
}
claims["user"] = user
}
2023-03-01 23:29:45 +02:00
return json.Marshal(claims)
}
func (p *Apple) parseAndVerifyIdToken(idToken string) (jwt.MapClaims, error) {
if idToken == "" {
return nil, errors.New("empty id_token")
}
// extract the token header params and claims
// ---
claims := jwt.MapClaims{}
t, _, err := jwt.NewParser().ParseUnverified(idToken, claims)
if err != nil {
return nil, err
}
// validate common claims per https://developer.apple.com/documentation/sign_in_with_apple/sign_in_with_apple_rest_api/verifying_a_user#3383769
// ---
2025-01-05 11:05:26 +02:00
jwtValidator := jwt.NewValidator(
jwt.WithExpirationRequired(),
jwt.WithIssuedAt(),
jwt.WithLeeway(idTokenLeeway),
2025-01-05 11:05:26 +02:00
jwt.WithIssuer("https://appleid.apple.com"),
jwt.WithAudience(p.clientId),
)
err = jwtValidator.Validate(claims)
if err != nil {
return nil, err
}
// validate id_token signature
//
// note: this step could be technically considered optional because we trust
// the token which is a result of direct TLS communication with the provider
// (see also https://openid.net/specs/openid-connect-core-1_0.html#IDTokenValidation)
2023-03-01 23:29:45 +02:00
// ---
kid, _ := t.Header["kid"].(string)
err = validateIdTokenSignature(p.ctx, idToken, p.jwksURL, kid)
2023-03-01 23:29:45 +02:00
if err != nil {
return nil, err
}
return claims, nil
2023-03-01 23:29:45 +02:00
}