-
Notifications
You must be signed in to change notification settings - Fork 6
/
auth.go
182 lines (155 loc) · 4.56 KB
/
auth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package apple
import (
"context"
"crypto/rsa"
"encoding/base64"
"encoding/json"
"errors"
"github.com/golang-jwt/jwt/v5"
"github.com/smartwalle/apple/internal/auth"
"github.com/smartwalle/dbc"
"github.com/smartwalle/ngx"
"github.com/smartwalle/nsync/singleflight"
"net/http"
"strings"
"time"
)
const (
kFetchAuthKeys = "https://appleid.apple.com/auth/keys"
kIssuer = "https://appleid.apple.com"
)
var (
ErrInvalidToken = errors.New("invalid token")
ErrInvalidIssuer = errors.New("invalid issuer")
ErrInvalidBundleId = errors.New("invalid bundle id")
ErrTokenExpired = errors.New("token is expired")
)
type AuthClientOption func(c *AuthClient)
// WithKeyExpiration 用于设置从 https://appleid.apple.com/auth/keys 获取的公钥在本地的缓存时间,单位为秒
func WithKeyExpiration(expiration int64) AuthClientOption {
return func(c *AuthClient) {
c.expiration = expiration
}
}
// WithBundleId 用于设置 VerifyToken() 方法需要的 BundleId 信息
func WithBundleId(bundleId string) AuthClientOption {
return func(c *AuthClient) {
c.bundleId = bundleId
}
}
// AuthClient 苹果登录验证
type AuthClient struct {
Client *http.Client
keys dbc.Cache[string, *rsa.PublicKey]
group singleflight.Group[string, interface{}]
expiration int64
bundleId string
}
func NewAuthClient(opts ...AuthClientOption) *AuthClient {
var nClient = &AuthClient{}
nClient.Client = http.DefaultClient
nClient.keys = dbc.New[*rsa.PublicKey]()
nClient.group = singleflight.New[interface{}]()
for _, opt := range opts {
if opt != nil {
opt(nClient)
}
}
if nClient.expiration <= 0 {
nClient.expiration = 300 // 默认 300 秒
}
return nClient
}
// DecodeToken 解析 Token
//
// 只对 Token 进行解析,不会验证合法性
func (c *AuthClient) DecodeToken(token string) (*User, error) {
var payloads = strings.Split(token, ".")
if len(payloads) < 3 {
return nil, ErrInvalidToken
}
headerBytes, err := base64.RawStdEncoding.DecodeString(payloads[0])
if err != nil {
return nil, err
}
var header *auth.Header
if err = json.Unmarshal(headerBytes, &header); err != nil {
return nil, err
}
var key = c.GetAuthKey(header.Kid)
if key == nil {
return nil, ErrInvalidToken
}
var claims = &auth.Claims{}
jwt.ParseWithClaims(token, claims, func(_ *jwt.Token) (interface{}, error) {
return key, nil
})
var user = &User{}
user.Id = claims.Subject
user.Issuer = claims.Issuer
user.BundleId = strings.Join(claims.Audience, ";")
user.Email = claims.Email
user.EmailVerified = bool(claims.EmailVerified)
user.IsPrivateEmail = bool(claims.IsPrivateEmail)
user.RealUserStatus = claims.RealUserStatus
user.TransferSub = claims.TransferSub
user.Nonce = claims.Nonce
user.AuthTime = int64(claims.AuthTime)
user.IssuedAt = claims.IssuedAt.Unix()
user.ExpiresAt = claims.ExpiresAt.Unix()
return user, nil
}
// VerifyToken 解析并验证 Token https://developer.apple.com/documentation/sign_in_with_apple/sign_in_with_apple_rest_api/verifying_a_user#3383769
//
// 会对 Token 的合法性进行验证,主要判断 BundleId 和 Issuer 是否正确以及 Token 是否在有效期内
func (c *AuthClient) VerifyToken(token string) (*User, error) {
var user, err = c.DecodeToken(token)
if err != nil {
return nil, err
}
if user.BundleId != c.bundleId {
return nil, ErrInvalidBundleId
}
if user.Issuer != kIssuer {
return nil, ErrInvalidIssuer
}
if user.ExpiresAt <= time.Now().Unix() {
return nil, ErrTokenExpired
}
return user, nil
}
func (c *AuthClient) GetAuthKey(kid string) *rsa.PublicKey {
// 从本地缓存中查询 key 信息,存在则直接返回
if key, _ := c.keys.Get(kid); key != nil {
return key
}
c.group.Do(kFetchAuthKeys, func(_ string) (interface{}, error) {
// 从苹果服务器请求 key 数据
var nKeys, _ = c.requestAuthKeys()
for _, key := range nKeys {
var nKey, _ = auth.DecodePublicKey(key.N, key.E)
if nKey != nil {
c.keys.SetEx(key.Kid, nKey, c.expiration)
}
}
return nil, nil
})
key, _ := c.keys.Get(kid)
return key
}
// requestAuthKeys https://developer.apple.com/documentation/sign_in_with_apple/fetch_apple_s_public_key_for_verifying_token_signature
func (c *AuthClient) requestAuthKeys() ([]auth.Key, error) {
var req = ngx.NewRequest(http.MethodGet, kFetchAuthKeys, ngx.WithClient(c.Client))
var rsp, err = req.Do(context.Background())
if err != nil {
return nil, err
}
defer rsp.Body.Close()
var aux = struct {
Keys []auth.Key `json:"keys"`
}{}
if err = json.NewDecoder(rsp.Body).Decode(&aux); err != nil {
return nil, err
}
return aux.Keys, nil
}