-
Notifications
You must be signed in to change notification settings - Fork 0
/
authentication.go
107 lines (89 loc) · 2.58 KB
/
authentication.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
package goswyftx
type Scope struct {
Display string `json:"display"`
Description string `json:"desc"`
Key string `json:"key"`
State int `json:"state"`
}
type AppScope struct {
ReadAccount Scope `json:"app.account.read"`
WithdrawFunds Scope `json:"app.funds.withdraw"`
DeleteOrders Scope `json:"app.orders.delete"`
}
type Key struct {
ID string `json:"id"`
Label string `json:"label"`
Scope string `json:"scope"`
Created SwyftxTime `json:"created"`
}
// AuthService hold methods for the Authentication endpoints
// https://docs.swyftx.com.au/#/reference/authentication
type AuthService service
// Authentication will create a new AuthService instance
func (c *Client) Authentication() *AuthService {
return (*AuthService)(&service{c})
}
// Refresh will regenerate a new access token (JWT token)
func (as *AuthService) Refresh() (string, error) {
var (
token struct {
Token string `json:"accessToken"`
}
body struct {
APIKey string `json:"apiKey"`
}
)
body.APIKey = as.client.apiKey
if err := as.client.Post("auth/refresh/", &body, &token); err != nil {
return "", err
}
return token.Token, nil
}
// Logout will invalidate the current access token (JWT token)
func (as *AuthService) Logout() (bool, error) {
var success struct {
Success bool `json:"success"`
}
if err := as.client.Post("auth/logout/", nil, &success); err != nil {
return success.Success, err
}
return success.Success, nil
}
// GetScope will get the scope of permmissions for an api key
func (as *AuthService) GetScope() (*AppScope, error) {
var appScope AppScope
if err := as.client.Get("user/apiKeys/scope/", &appScope); err != nil {
return nil, err
}
return &appScope, nil
}
// GetKeys will get all the keys available to a user
func (as *AuthService) GetKeys() ([]*Key, error) {
var keys []*Key
if err := as.client.Get("user/apiKeys/", &keys); err != nil {
return nil, err
}
return keys, nil
}
// RevokeKey will revoke an api key
// Returns the status of that action
func (as *AuthService) RevokeKey() (string, error) {
var status struct {
Status string `json:"status"`
}
if err := as.client.Post("user/apiKeys/revoke/", &as.client.apiKey, &status); err != nil {
return "", err
}
return status.Status, nil
}
// RevokeAllKeys will revoke all api keys for a user account
// Returns the status of that action
func (as *AuthService) RevokeAllKeys() (string, error) {
var status struct {
Status string `json:"status"`
}
if err := as.client.Post("user/apiKeys/revokeAll/", nil, &status); err != nil {
return "", err
}
return status.Status, nil
}