forked from badkaktus/gorocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authentication.go
191 lines (165 loc) · 6.79 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package gorocket
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
)
type LoginPayload struct {
User string `json:"user"`
Password string `json:"password"`
Resume string `json:"resume,omitempty"`
}
type LoginResponse struct {
Status string `json:"status"`
Data data `json:"data"`
Message string `json:"message,omitempty"`
}
type data struct {
UserID string `json:"userId"`
AuthToken string `json:"authToken"`
Me me `json:"me"`
}
type me struct {
ID string `json:"_id"`
Services services `json:"services"`
Emails []email `json:"emails"`
Status string `json:"status"`
Active bool `json:"active"`
UpdatedAt time.Time `json:"_updatedAt"`
Roles []string `json:"roles"`
Name string `json:"name"`
StatusConnection string `json:"statusConnection"`
Username string `json:"username"`
UtcOffset int `json:"utcOffset"`
StatusText string `json:"statusText"`
Settings settings `json:"settings"`
AvatarOrigin string `json:"avatarOrigin"`
RequirePasswordChange bool `json:"requirePasswordChange"`
Language string `json:"language"`
Email string `json:"email"`
AvatarURL string `json:"avatarUrl"`
}
type services struct {
Password password `json:"password"`
}
type password struct {
Bcrypt string `json:"bcrypt"`
}
type email struct {
Address string `json:"address"`
Verified bool `json:"verified"`
}
type settings struct {
Preferences preferences `json:"preferences"`
}
type dontAskAgainList struct {
Action string `json:"action"`
Label string `json:"label"`
}
type preferences struct {
EnableAutoAway bool `json:"enableAutoAway"`
IdleTimeLimit int `json:"idleTimeLimit"`
AudioNotifications string `json:"audioNotifications"`
DesktopNotifications string `json:"desktopNotifications"`
MobileNotifications string `json:"mobileNotifications"`
UnreadAlert bool `json:"unreadAlert"`
UseEmojis bool `json:"useEmojis"`
ConvertASCIIEmoji bool `json:"convertAsciiEmoji"`
AutoImageLoad bool `json:"autoImageLoad"`
SaveMobileBandwidth bool `json:"saveMobileBandwidth"`
CollapseMediaByDefault bool `json:"collapseMediaByDefault"`
HideUsernames bool `json:"hideUsernames"`
HideRoles bool `json:"hideRoles"`
HideFlexTab bool `json:"hideFlexTab"`
HideAvatars bool `json:"hideAvatars"`
SidebarGroupByType bool `json:"sidebarGroupByType"`
SidebarViewMode string `json:"sidebarViewMode"`
SidebarHideAvatar bool `json:"sidebarHideAvatar"`
SidebarShowUnread bool `json:"sidebarShowUnread"`
SidebarShowFavorites bool `json:"sidebarShowFavorites"`
SendOnEnter string `json:"sendOnEnter"`
MessageViewMode int `json:"messageViewMode"`
EmailNotificationMode string `json:"emailNotificationMode"`
NewRoomNotification string `json:"newRoomNotification"`
NewMessageNotification string `json:"newMessageNotification"`
MuteFocusedConversations bool `json:"muteFocusedConversations"`
NotificationsSoundVolume int `json:"notificationsSoundVolume"`
SidebarShowDiscussion bool `json:"sidebarShowDiscussion"`
DesktopNotificationRequireInteraction bool `json:"desktopNotificationRequireInteraction"`
SidebarSortby string `json:"sidebarSortby"`
DesktopNotificationDuration int `json:"desktopNotificationDuration"`
DontAskAgainList []dontAskAgainList `json:"dontAskAgainList"`
Highlights []interface{} `json:"highlights"`
Language string `json:"language"`
}
type LogoutResponse struct {
Status string `json:"status"`
Data struct {
Message string `json:"message"`
} `json:"data"`
}
type MeResponse struct {
ID string `json:"_id"`
Services services `json:"services"`
Emails []email `json:"emails"`
Status string `json:"status"`
Active bool `json:"active"`
UpdatedAt time.Time `json:"_updatedAt"`
Roles []string `json:"roles"`
Name string `json:"name"`
StatusConnection string `json:"statusConnection"`
Username string `json:"username"`
UtcOffset int `json:"utcOffset"`
StatusText string `json:"statusText"`
Settings settings `json:"settings"`
AvatarOrigin string `json:"avatarOrigin"`
RequirePasswordChange bool `json:"requirePasswordChange"`
Language string `json:"language"`
Email string `json:"email"`
AvatarURL string `json:"avatarUrl"`
Success bool `json:"success"`
}
func (c *Client) Login(login *LoginPayload) (*LoginResponse, error) {
opt, _ := json.Marshal(login)
url := fmt.Sprintf("%s/%s/login", c.baseURL, c.apiVersion)
req, err := http.NewRequest("POST",
url,
bytes.NewBuffer(opt))
if err != nil {
return nil, err
}
res := LoginResponse{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
// success auth
if res.Status == "success" {
c.UserID = res.Data.UserID
c.XToken = res.Data.AuthToken
}
return &res, nil
}
func (c *Client) Logout() (*LogoutResponse, error) {
req, err := http.NewRequest("POST", fmt.Sprintf("%s/%s/logout", c.baseURL, c.apiVersion), nil)
if err != nil {
return nil, err
}
res := LogoutResponse{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}
func (c *Client) Me() (*MeResponse, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/%s/me", c.baseURL, c.apiVersion), nil)
if err != nil {
return nil, err
}
res := MeResponse{}
if err := c.sendRequest(req, &res); err != nil {
return nil, err
}
return &res, nil
}