-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.go
161 lines (133 loc) · 3.54 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
package passage
import (
"context"
"errors"
"fmt"
"github.com/golang-jwt/jwt"
gojwt "github.com/golang-jwt/jwt"
"github.com/lestrrat-go/jwx/v2/jwk"
)
type MagicLinkOptions struct {
Language string
MagicLinkPath string
RedirectURL string
TTL int
}
type auth struct {
appID string
client *ClientWithResponses
jwksCacheSet jwk.Set
}
func newAuth(appID string, client *ClientWithResponses) (*auth, error) {
ctx := context.Background()
url := fmt.Sprintf("https://auth.passage.id/v1/apps/%v/.well-known/jwks.json", appID)
cache := jwk.NewCache(ctx)
if err := cache.Register(url); err != nil {
return nil, err
}
if _, err := cache.Refresh(ctx, url); err != nil {
return nil, fmt.Errorf("failed to fetch JWKS: %w", err)
}
return &auth{
appID: appID,
client: client,
jwksCacheSet: jwk.NewCachedSet(cache, url),
}, nil
}
// CreateMagicLink creates a Magic Link for your app using an email address.
func (a *auth) CreateMagicLinkWithEmail(
email string,
magicLinkType MagicLinkType,
send bool,
opts *MagicLinkOptions,
) (*MagicLink, error) {
args := CreateMagicLinkBody{
Email: email,
Channel: EmailChannel,
Type: magicLinkType,
Send: send,
}
return a.createMagicLink(args, opts)
}
// CreateMagicLink creates a Magic Link for your app using an E164-formatted phone number.
func (a *auth) CreateMagicLinkWithPhone(
phone string,
magicLinkType MagicLinkType,
send bool,
opts *MagicLinkOptions,
) (*MagicLink, error) {
args := CreateMagicLinkBody{
Phone: phone,
Channel: PhoneChannel,
Type: magicLinkType,
Send: send,
}
return a.createMagicLink(args, opts)
}
// CreateMagicLink creates a Magic Link for your app using a Passage user ID.
func (a *auth) CreateMagicLinkWithUser(
userID string,
channel ChannelType,
magicLinkType MagicLinkType,
send bool,
opts *MagicLinkOptions,
) (*MagicLink, error) {
args := CreateMagicLinkBody{
UserID: userID,
Channel: channel,
Type: magicLinkType,
Send: send,
}
return a.createMagicLink(args, opts)
}
// ValidateJWT validates the JWT and returns the user ID.
func (a *auth) ValidateJWT(jwt string) (string, error) {
if jwt == "" {
return "", errors.New("jwt is required.")
}
parsedToken, err := gojwt.Parse(jwt, a.getPublicKey)
if err != nil {
return "", err
}
claims, ok := parsedToken.Claims.(gojwt.MapClaims)
if !ok {
return "", errors.New("failed to extract claims from JWT")
}
userID, ok := claims["sub"].(string)
if !ok {
return "", errors.New("failed to find sub claim in JWT")
}
if !claims.VerifyAudience(a.appID, true) {
return "", errors.New("failed audience verification for JWT")
}
return userID, nil
}
func (a *auth) createMagicLink(args CreateMagicLinkBody, opts *MagicLinkOptions) (*MagicLink, error) {
if opts != nil {
args.Language = opts.Language
args.MagicLinkPath = opts.MagicLinkPath
args.RedirectURL = opts.RedirectURL
args.TTL = opts.TTL
}
res, err := a.client.CreateMagicLinkWithResponse(context.Background(), a.appID, args)
if err != nil {
return nil, err
}
if res.JSON201 != nil {
return &res.JSON201.MagicLink, nil
}
return nil, errorFromResponse(res.Body, res.StatusCode())
}
func (a *auth) getPublicKey(token *jwt.Token) (interface{}, error) {
keyID, ok := token.Header["kid"].(string)
if !ok {
return nil, errors.New("failed to find kid in JWT header")
}
key, ok := a.jwksCacheSet.LookupKeyID(keyID)
if !ok {
return nil, fmt.Errorf("failed to find key %q in JWKS", keyID)
}
var pubKey interface{}
err := key.Raw(&pubKey)
return pubKey, err
}