This repository was archived by the owner on Jul 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccount.go
More file actions
92 lines (77 loc) · 2.13 KB
/
Copy pathaccount.go
File metadata and controls
92 lines (77 loc) · 2.13 KB
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
package bulwark
import (
"context"
"fmt"
"net/http"
)
// Account is used for account bulwark-auth tasks, but it's preferable to use it via the Guard struct.
type Account struct {
client *http.Client
baseURL string
}
const (
createUrl = "api/accounts"
verifyUrl = "api/accounts/verify"
changePasswordUrl = "api/accounts/password"
)
// NewAccountClient creates a client for account tasks
func NewAccountClient(baseURL string, client *http.Client) *Account {
return &Account{
baseURL: baseURL,
client: client,
}
}
// Create will create a user account and send a verification email
func (a Account) Create(ctx context.Context, tenantID, email, password string) error {
payload := struct {
TenantID string `json:"tenantId"`
Email string `json:"email"`
Password string `json:"password"`
}{
TenantID: tenantID,
Email: email,
Password: password,
}
err := doPost(ctx, fmt.Sprintf("%s/%s", a.baseURL, createUrl), payload, nil, a.client)
if err != nil {
return err
}
return nil
}
// Verify will verify a account with a verification token supplied via email
func (a Account) Verify(ctx context.Context, tenantID, email, verificationToken string) error {
payload := struct {
TenantID string `json:"tenantId"`
Email string `json:"email"`
Token string `json:"token"`
}{
TenantID: tenantID,
Email: email,
Token: verificationToken,
}
err := doPost(ctx, fmt.Sprintf("%s/%s", a.baseURL, verifyUrl), payload,
nil, a.client)
if err != nil {
return err
}
return nil
}
// ChangePassword changes a password for an account, valid access token is required
func (a Account) ChangePassword(ctx context.Context, tenantID, email, newPassword, accessToken string) error {
payload := struct {
TenantID string `json:"tenantId"`
Email string `json:"email"`
NewPassword string `json:"newPassword"`
AccessToken string `json:"accessToken"`
}{
TenantID: tenantID,
Email: email,
NewPassword: newPassword,
AccessToken: accessToken,
}
err := doPut(ctx, fmt.Sprintf("%s/%s", a.baseURL, changePasswordUrl), payload, a.client)
if err != nil {
return err
}
return nil
}