@@ -2,30 +2,41 @@ package passage
2
2
3
3
import (
4
4
"context"
5
+ "errors"
5
6
"fmt"
6
7
7
8
"github.com/lestrrat-go/jwx/v2/jwk"
8
9
)
9
10
10
11
const jwksUrl = "https://auth.passage.id/v1/apps/%v/.well-known/jwks.json"
11
12
13
+ type Passage = App
14
+
15
+ // Config holds the configuration for the Passage SDK.
16
+ //
17
+ // Deprecated: will be removed in v2.
12
18
type Config struct {
13
19
APIKey string
14
20
HeaderAuth bool
15
21
}
16
22
17
- // Deprecated: will be renamed to `Passage` in v2
23
+ // App is the main struct for the Passage SDK.
24
+ //
25
+ // Deprecated: will be renamed to `Passage` in v2.
18
26
type App struct {
27
+ // Deprecated: will be removed in v2.
19
28
ID string
20
- // Deprecated
21
- Config * Config
22
- User * appUser
23
- client * ClientWithResponses
24
- jwksCacheSet jwk. Set
29
+ // Deprecated: will be removed in v2.
30
+ Config * Config
31
+ Auth * auth
32
+ User * user
33
+ client * ClientWithResponses
25
34
}
26
35
27
- // Deprecated: Will be replaced with a different signature in v2
28
- func New (appID string , config * Config ) (* App , error ) {
36
+ // New creates a new Passage instance.
37
+ //
38
+ // Deprecated: Will be replaced with a different signature in v2 -- `New(appID string, apiKey string) (*Passage, error)`.
39
+ func New (appID string , config * Config ) (* Passage , error ) {
29
40
if config == nil {
30
41
config = & Config {}
31
42
}
@@ -39,12 +50,6 @@ func New(appID string, config *Config) (*App, error) {
39
50
return nil , err
40
51
}
41
52
42
- app := App {
43
- ID : appID ,
44
- Config : config ,
45
- client : client ,
46
- }
47
-
48
53
url := fmt .Sprintf (jwksUrl , appID )
49
54
cache := jwk .NewCache (context .Background ())
50
55
if err := cache .Register (url ); err != nil {
@@ -55,17 +60,24 @@ func New(appID string, config *Config) (*App, error) {
55
60
return nil , Error {Message : "failed to fetch jwks" }
56
61
}
57
62
58
- app .jwksCacheSet = jwk .NewCachedSet (cache , url )
59
-
60
- app .User = newAppUser (app )
63
+ auth , err := newAuth (appID , client )
64
+ if err != nil {
65
+ return nil , err
66
+ }
67
+ user := newUser (appID , client )
61
68
62
- return & app , nil
69
+ return & App {
70
+ ID : appID ,
71
+ Config : config ,
72
+ Auth : auth ,
73
+ User : user ,
74
+ client : client ,
75
+ }, nil
63
76
}
64
77
65
- // GetApp gets information about an app
66
- // returns App on success, error on failure
78
+ // GetApp fetches the Passage app info.
67
79
//
68
- // Deprecated: GetApp - this method will not be replaced
80
+ // Deprecated: will be removed in v2.
69
81
func (a * App ) GetApp () (* AppInfo , error ) {
70
82
res , err := a .client .GetAppWithResponse (context .Background (), a .ID )
71
83
if err != nil {
@@ -99,40 +111,22 @@ func (a *App) GetApp() (*AppInfo, error) {
99
111
}
100
112
}
101
113
102
- // CreateMagicLink receives a CreateMagicLinkBody struct, creating a magic link with provided values
103
- // returns MagicLink on success, error on failure
114
+ // CreateMagicLink creates a Magic Link for your app.
115
+ //
116
+ // Deprecated: use `Passage.Auth.CreateMagicLink` instead.
104
117
func (a * App ) CreateMagicLink (createMagicLinkBody CreateMagicLinkBody ) (* MagicLink , error ) {
105
- res , err := a .client . CreateMagicLinkWithResponse ( context . Background (), a . ID , createMagicLinkBody )
118
+ magicLink , err := a .Auth . CreateMagicLink ( createMagicLinkBody )
106
119
if err != nil {
107
- return nil , Error {Message : "network error: failed to create Passage Magic Link" }
120
+ var passageError PassageError
121
+ if errors .As (err , & passageError ) {
122
+ return magicLink , Error {
123
+ ErrorText : passageError .Message ,
124
+ ErrorCode : passageError .ErrorCode ,
125
+ Message : passageError .Message ,
126
+ StatusCode : passageError .StatusCode ,
127
+ }
128
+ }
108
129
}
109
130
110
- if res .JSON201 != nil {
111
- return & res .JSON201 .MagicLink , nil
112
- }
113
-
114
- var errorText string
115
- var errorCode string
116
- switch {
117
- case res .JSON400 != nil :
118
- errorText = res .JSON400 .Error
119
- errorCode = string (res .JSON400 .Code )
120
- case res .JSON401 != nil :
121
- errorText = res .JSON401 .Error
122
- errorCode = string (res .JSON401 .Code )
123
- case res .JSON404 != nil :
124
- errorText = res .JSON404 .Error
125
- errorCode = string (res .JSON404 .Code )
126
- case res .JSON500 != nil :
127
- errorText = res .JSON500 .Error
128
- errorCode = string (res .JSON500 .Code )
129
- }
130
-
131
- return nil , Error {
132
- Message : "failed to create Passage Magic Link" ,
133
- StatusCode : res .StatusCode (),
134
- StatusText : res .Status (),
135
- ErrorText : errorText ,
136
- ErrorCode : errorCode ,
137
- }
131
+ return magicLink , err
138
132
}
0 commit comments