-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.go
224 lines (177 loc) · 5.12 KB
/
user.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package passage
import (
"context"
"errors"
"net/http"
"strings"
)
type user struct {
appID string
client *ClientWithResponses
}
func newUser(appID string, client *ClientWithResponses) *user {
return &user{
appID: appID,
client: client,
}
}
// Get retrieves a user's object using their user ID.
func (u *user) Get(userID string) (*PassageUser, error) {
if userID == "" {
return nil, errors.New("userID is required.")
}
res, err := u.client.GetUserWithResponse(context.Background(), u.appID, userID)
if err != nil {
return nil, err
}
if res.JSON200 != nil {
return &res.JSON200.PassageUser, nil
}
return nil, errorFromResponse(res.Body, res.StatusCode())
}
// GetByIdentifier retrieves a user's object using their user identifier.
func (u *user) GetByIdentifier(identifier string) (*PassageUser, error) {
if identifier == "" {
return nil, errors.New("identifier is required.")
}
limit := 1
lowerIdentifier := strings.ToLower(identifier)
res, err := u.client.ListPaginatedUsersWithResponse(
context.Background(),
u.appID,
&ListPaginatedUsersParams{
Limit: &limit,
Identifier: &lowerIdentifier,
},
)
if err != nil {
return nil, err
}
if res.JSON200 != nil {
users := res.JSON200.Users
if len(users) == 0 {
return nil, PassageError{
Message: "Could not find user with that identifier.",
ErrorCode: "user_not_found",
StatusCode: http.StatusNotFound,
}
}
return u.Get(users[0].ID)
}
return nil, errorFromResponse(res.Body, res.StatusCode())
}
// Activate activates a user using their user ID.
func (u *user) Activate(userID string) (*PassageUser, error) {
if userID == "" {
return nil, errors.New("userID is required.")
}
res, err := u.client.ActivateUserWithResponse(context.Background(), u.appID, userID)
if err != nil {
return nil, err
}
if res.JSON200 != nil {
return &res.JSON200.PassageUser, nil
}
return nil, errorFromResponse(res.Body, res.StatusCode())
}
// Deactivate deactivates a user using their user ID.
func (u *user) Deactivate(userID string) (*PassageUser, error) {
if userID == "" {
return nil, errors.New("userID is required.")
}
res, err := u.client.DeactivateUserWithResponse(context.Background(), u.appID, userID)
if err != nil {
return nil, err
}
if res.JSON200 != nil {
return &res.JSON200.PassageUser, nil
}
return nil, errorFromResponse(res.Body, res.StatusCode())
}
// Update updates a user.
func (u *user) Update(userID string, options UpdateUserOptions) (*PassageUser, error) {
if userID == "" {
return nil, errors.New("userID is required.")
}
res, err := u.client.UpdateUserWithResponse(context.Background(), u.appID, userID, options)
if err != nil {
return nil, err
}
if res.JSON200 != nil {
return &res.JSON200.PassageUser, nil
}
return nil, errorFromResponse(res.Body, res.StatusCode())
}
// Create creates a user.
func (u *user) Create(args CreateUserArgs) (*PassageUser, error) {
if args.Email == "" && args.Phone == "" {
return nil, errors.New("At least one of args.Email or args.Phone is required.")
}
res, err := u.client.CreateUserWithResponse(context.Background(), u.appID, args)
if err != nil {
return nil, err
}
if res.JSON201 != nil {
return &res.JSON201.PassageUser, nil
}
return nil, errorFromResponse(res.Body, res.StatusCode())
}
// Delete deletes a user using their user ID.
func (u *user) Delete(userID string) error {
if userID == "" {
return errors.New("userID is required.")
}
res, err := u.client.DeleteUserWithResponse(context.Background(), u.appID, userID)
if err != nil {
return err
}
if res.StatusCode() >= 200 && res.StatusCode() < 300 {
return nil
}
return errorFromResponse(res.Body, res.StatusCode())
}
// ListDevices retrieves a user's webauthn devices using their user ID.
func (u *user) ListDevices(userID string) ([]WebAuthnDevices, error) {
if userID == "" {
return nil, errors.New("userID is required.")
}
res, err := u.client.ListUserDevicesWithResponse(context.Background(), u.appID, userID)
if err != nil {
return nil, err
}
if res.JSON200 != nil {
return res.JSON200.Devices, nil
}
return nil, errorFromResponse(res.Body, res.StatusCode())
}
// RevokeDevice revokes user's webauthn device using their user ID and the device ID.
func (u *user) RevokeDevice(userID string, deviceID string) error {
if userID == "" {
return errors.New("userID is required.")
}
if deviceID == "" {
return errors.New("deviceID is required.")
}
res, err := u.client.DeleteUserDevicesWithResponse(context.Background(), u.appID, userID, deviceID)
if err != nil {
return err
}
if res.StatusCode() >= 200 && res.StatusCode() < 300 {
return nil
}
return errorFromResponse(res.Body, res.StatusCode())
}
// RevokeRefreshTokens revokes all of a user's Refresh Tokens using their User ID.
func (u *user) RevokeRefreshTokens(userID string) error {
if userID == "" {
return errors.New("userID is required.")
}
res, err := u.client.RevokeUserRefreshTokensWithResponse(context.Background(), u.appID, userID)
if err != nil {
return err
}
if res.StatusCode() >= 200 && res.StatusCode() < 300 {
return nil
}
return errorFromResponse(res.Body, res.StatusCode())
}