Skip to content

Commit d7b67f1

Browse files
authored
Add new methods (#12)
1 parent 159d560 commit d7b67f1

File tree

4 files changed

+251
-2
lines changed

4 files changed

+251
-2
lines changed

client/challenges.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package client
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/url"
7+
"strings"
8+
)
9+
10+
func (c Client) Challenge(req ChallengeRequest) (ChallengeResponse, error) {
11+
var resp ChallengeResponse
12+
body, err := json.Marshal(req)
13+
if err != nil {
14+
return resp, err
15+
}
16+
data, err := c.post("/challenge", strings.NewReader(string(body)))
17+
if err != nil {
18+
return resp, err
19+
}
20+
if err := json.Unmarshal(data, &resp); err != nil {
21+
return resp, err
22+
}
23+
return resp, nil
24+
}
25+
26+
func (c Client) Verify(req VerifyRequest) (VerifyResponse, error) {
27+
var resp VerifyResponse
28+
body, err := json.Marshal(req)
29+
if err != nil {
30+
return resp, err
31+
}
32+
data, err := c.post("/verify", strings.NewReader(string(body)))
33+
if err != nil {
34+
return resp, err
35+
}
36+
if err := json.Unmarshal(data, &resp); err != nil {
37+
return resp, err
38+
}
39+
return resp, nil
40+
}
41+
42+
func (c Client) ClaimChallenge(req ClaimChallengeRequest) (ClaimChallengeResponse, error) {
43+
var resp ClaimChallengeResponse
44+
body, err := json.Marshal(req)
45+
if err != nil {
46+
return resp, err
47+
}
48+
data, err := c.post("/claim", strings.NewReader(string(body)))
49+
if err != nil {
50+
return resp, err
51+
}
52+
if err := json.Unmarshal(data, &resp); err != nil {
53+
return resp, err
54+
}
55+
return resp, nil
56+
}
57+
58+
func (c Client) GetChallenge(req GetChallengeRequest) (GetChallengeResponse, error) {
59+
var resp GetChallengeResponse
60+
// Build query string
61+
u, err := url.Parse(fmt.Sprintf("%s/challenges", c.ApiUrl))
62+
if err != nil {
63+
return resp, err
64+
}
65+
q := u.Query()
66+
if req.ChallengeId != "" {
67+
q.Set("challengeId", req.ChallengeId)
68+
}
69+
if req.UserId != "" {
70+
q.Set("userId", req.UserId)
71+
}
72+
if req.Action != "" {
73+
q.Set("action", req.Action)
74+
}
75+
if req.VerificationMethod != "" {
76+
q.Set("verificationMethod", string(req.VerificationMethod))
77+
}
78+
u.RawQuery = q.Encode()
79+
80+
data, err := c.get(u.Path + "?" + u.RawQuery)
81+
if err != nil {
82+
return resp, err
83+
}
84+
if err := json.Unmarshal(data, &resp); err != nil {
85+
return resp, err
86+
}
87+
return resp, nil
88+
}

client/sessions.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package client
2+
3+
import (
4+
"encoding/json"
5+
"strings"
6+
)
7+
8+
func (c Client) CreateSession(req CreateSessionRequest) (CreateSessionResponse, error) {
9+
var resp CreateSessionResponse
10+
body, err := json.Marshal(req)
11+
if err != nil {
12+
return resp, err
13+
}
14+
data, err := c.post("/sessions", strings.NewReader(string(body)))
15+
if err != nil {
16+
return resp, err
17+
}
18+
if err := json.Unmarshal(data, &resp); err != nil {
19+
return resp, err
20+
}
21+
return resp, nil
22+
}
23+
24+
func (c Client) ValidateSession(req ValidateSessionRequest) (ValidateSessionResponse, error) {
25+
var resp ValidateSessionResponse
26+
body, err := json.Marshal(req)
27+
if err != nil {
28+
return resp, err
29+
}
30+
data, err := c.post("/sessions/validate", strings.NewReader(string(body)))
31+
if err != nil {
32+
return resp, err
33+
}
34+
if err := json.Unmarshal(data, &resp); err != nil {
35+
return resp, err
36+
}
37+
return resp, nil
38+
}
39+
40+
func (c Client) RefreshSession(req RefreshSessionRequest) (RefreshSessionResponse, error) {
41+
var resp RefreshSessionResponse
42+
body, err := json.Marshal(req)
43+
if err != nil {
44+
return resp, err
45+
}
46+
data, err := c.post("/sessions/refresh", strings.NewReader(string(body)))
47+
if err != nil {
48+
return resp, err
49+
}
50+
if err := json.Unmarshal(data, &resp); err != nil {
51+
return resp, err
52+
}
53+
return resp, nil
54+
}
55+
56+
func (c Client) RevokeSession(req RevokeSessionRequest) error {
57+
body, err := json.Marshal(req)
58+
if err != nil {
59+
return err
60+
}
61+
_, err = c.post("/sessions/revoke", strings.NewReader(string(body)))
62+
return err
63+
}
64+
65+
func (c Client) RevokeUserSessions(req RevokeUserSessionsRequest) error {
66+
body, err := json.Marshal(req)
67+
if err != nil {
68+
return err
69+
}
70+
_, err = c.post("/sessions/user/revoke", strings.NewReader(string(body)))
71+
return err
72+
}

client/types.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,94 @@ type ValidateChallengeResponse struct {
232232
IdempotencyKey string `json:"idempotencyKey,omitempty"`
233233
VerificationMethod string `json:"verificationMethod,omitempty"`
234234
}
235+
236+
type ChallengeRequest struct {
237+
VerificationMethod string `json:"verificationMethod"`
238+
Action string `json:"action"`
239+
Email string `json:"email,omitempty"`
240+
PhoneNumber string `json:"phoneNumber,omitempty"`
241+
SmsChannel string `json:"smsChannel,omitempty"`
242+
}
243+
244+
type ChallengeResponse struct {
245+
ChallengeId string `json:"challengeId"`
246+
}
247+
248+
type VerifyRequest struct {
249+
ChallengeId string `json:"challengeId"`
250+
VerificationCode string `json:"verificationCode"`
251+
}
252+
253+
type VerifyResponse struct {
254+
IsVerified bool `json:"isVerified"`
255+
Email string `json:"email,omitempty"`
256+
PhoneNumber string `json:"phoneNumber,omitempty"`
257+
VerificationMethod string `json:"verificationMethod,omitempty"`
258+
}
259+
260+
type ClaimChallengeRequest struct {
261+
ChallengeId string `json:"challengeId"`
262+
UserId string `json:"userId"`
263+
}
264+
265+
type ClaimChallengeResponse struct {
266+
Token string `json:"token"`
267+
VerificationMethod string `json:"verificationMethod"`
268+
}
269+
270+
type GetChallengeRequest struct {
271+
ChallengeId string `json:"challengeId,omitempty"`
272+
UserId string `json:"userId,omitempty"`
273+
Action string `json:"action,omitempty"`
274+
VerificationMethod string `json:"verificationMethod,omitempty"`
275+
}
276+
277+
type GetChallengeResponse struct {
278+
ChallengeId string `json:"challengeId,omitempty"`
279+
ExpiresAt int64 `json:"expiresAt,omitempty"`
280+
VerificationMethod string `json:"verificationMethod,omitempty"`
281+
SmsChannel string `json:"smsChannel,omitempty"`
282+
PhoneNumber string `json:"phoneNumber,omitempty"`
283+
Email string `json:"email,omitempty"`
284+
Action string `json:"action,omitempty"`
285+
}
286+
287+
type CreateSessionRequest struct {
288+
ClientId string `json:"clientId"`
289+
Token string `json:"token"`
290+
}
291+
292+
type CreateSessionResponse struct {
293+
AccessToken string `json:"accessToken"`
294+
RefreshToken string `json:"refreshToken"`
295+
}
296+
297+
type ValidateSessionRequest struct {
298+
AccessToken string `json:"accessToken"`
299+
ClientIds []string `json:"clientIds,omitempty"`
300+
}
301+
302+
type ValidateSessionResponse struct {
303+
User struct {
304+
UserId string `json:"userId"`
305+
UserAttributes
306+
} `json:"user"`
307+
ExpiresAt int64 `json:"expiresAt"`
308+
}
309+
310+
type RefreshSessionRequest struct {
311+
RefreshToken string `json:"refreshToken"`
312+
}
313+
314+
type RefreshSessionResponse struct {
315+
AccessToken string `json:"accessToken"`
316+
RefreshToken string `json:"refreshToken"`
317+
}
318+
319+
type RevokeSessionRequest struct {
320+
AccessToken string `json:"accessToken"`
321+
}
322+
323+
type RevokeUserSessionsRequest struct {
324+
UserId string `json:"userId"`
325+
}

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
github.com/golang-jwt/jwt/v5 v5.0.0 h1:1n1XNM9hk7O9mnQoNBGolZvzebBQ7p93ULHRc28XJUE=
2-
github.com/golang-jwt/jwt/v5 v5.0.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=

0 commit comments

Comments
 (0)