-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthentication.go
134 lines (125 loc) · 3.33 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
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
package fluidpay
import (
"encoding/json"
"fmt"
)
// Auth is the struct to store your authentication.
type Auth struct {
authType int
authorization string
}
// NewAuth initializes a new authentication.
// Set authType to 1 for API key and 2 for JWT token.
func NewAuth(auth string, authType int) (Auth, error) {
switch authType {
case 1:
return Auth{
authType: authType,
authorization: auth,
}, nil
case 2:
return Auth{
authType: authType,
authorization: "Bearer " + auth,
}, nil
default:
return Auth{}, fmt.Errorf("Invalid authType %v please give 1 or 2 as a value", authType)
}
}
// SetAuth updates the previous authentication.
// Set authType to 1 for API key and 2 for JWT token.
func (a *Auth) SetAuth(auth string, authType int) error {
switch authType {
case 1:
a.authType = authType
a.authorization = auth
return nil
case 2:
a.authType = authType
a.authorization = "Bearer " + auth
return nil
default:
return fmt.Errorf("invalid authType %v please give 1 or 2 as a value", authType)
}
}
// ObtainJWT obtains a new JWT token.
func ObtainJWT(fluidpay Fluidpay, reqBody JWTTokenRequest) (JWTTokenResponse, error) {
var response JWTTokenResponse
body, err := json.Marshal(reqBody)
if err != nil {
return response, err
}
param := []string{"token-auth"}
resBody, err := DoRequest(fluidpay, "POST", param, body)
if err != nil {
return response, err
}
err = json.Unmarshal(resBody, &response)
if err != nil {
return response, err
}
return response, nil
}
//ForgottenUsername requests a reminder e-mail for the username.
func ForgottenUsername(fluidpay Fluidpay, reqBody ForgottenUsernameRequest) (GeneralResponse, error) {
var response GeneralResponse
body, err := json.Marshal(reqBody)
if err != nil {
return response, err
}
param := []string{"user", "forgot-username"}
resBody, err := DoRequest(fluidpay, "POST", param, body)
if err != nil {
return response, err
}
err = json.Unmarshal(resBody, &response)
if err != nil {
return response, err
}
return response, nil
}
//ForgottenPassword requests a reminder e-mail containing a reset code for the password.
func ForgottenPassword(fluidpay Fluidpay, reqBody ForgottenPasswordRequest) (GeneralResponse, error) {
var response GeneralResponse
body, err := json.Marshal(reqBody)
if err != nil {
return response, err
}
param := []string{"user", "forgot-password"}
resBody, err := DoRequest(fluidpay, "POST", param, body)
if err != nil {
return response, err
}
err = json.Unmarshal(resBody, &response)
if err != nil {
return response, err
}
return response, nil
}
//PasswordReset resets your password to the one given in the request body.
func PasswordReset(fluidpay Fluidpay, reqBody PasswordResetRequest) (GeneralResponse, error) {
var response GeneralResponse
body, err := json.Marshal(reqBody)
if err != nil {
return response, err
}
param := []string{"user", "forgot-password", "reset"}
resBody, err := DoRequest(fluidpay, "POST", param, body)
if err != nil {
return response, err
}
err = json.Unmarshal(resBody, &response)
if err != nil {
return response, err
}
return response, nil
}
//TokenLogout terminates a valid authentication token.
func TokenLogout(fluidpay Fluidpay) error {
param := []string{"logout"}
_, err := DoRequest(fluidpay, "GET", param, nil)
if err != nil {
return err
}
return nil
}