-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
85 lines (73 loc) · 1.75 KB
/
auth.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
package smartcharge
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
)
type AuthBody struct {
NotificationToken string `json:"NotificationToken"`
AppID string `json:"appID"`
Email string `json:"Email"`
Password string `json:"Password"`
PushState string `json:"pushState"`
AppToken string `json:"appToken"`
}
type AuthResponse struct {
Result Result `json:"Result"`
}
type Result struct {
User User `json:"user"`
Customer Customer `json:"customer"`
AccessToken string `json:"accessToken"`
RefreshToken string `json:"rToken"`
}
type User struct {
Id int `json:"PK_UserID"`
Username string `json:"Username"`
Email string `json:"Email"`
}
type Customer struct {
Id int `json:"PK_CustomerID"`
Email string `json:"Email"`
}
type Authentication struct {
UserId int
CustomerId int
AccessToken string
RefreshToken string
}
func authenticate(email string, password string) (*Authentication, error) {
authUrl := DefaultBaseUrl + "v2/Users/Authenticate"
body := AuthBody{
AppID: DefaultAppId,
Email: email,
Password: password,
AppToken: DefaultAppToken,
PushState: DefaultPushState,
}
authBody, err := json.Marshal(body)
if err != nil {
return nil, err
}
resp, err := http.Post(authUrl, "application/json", bytes.NewBuffer(authBody))
if err != nil {
return nil, err
}
v := &AuthResponse{}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(b, v)
if err != nil {
return nil, err
}
auth := &Authentication{
UserId: v.Result.User.Id,
CustomerId: v.Result.Customer.Id,
AccessToken: v.Result.AccessToken,
RefreshToken: v.Result.RefreshToken,
}
return auth, nil
}