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
|
package rest
import (
"bytes"
"encoding/json"
"fmt"
"net/url"
"time"
"github.com/matterbridge/Rocket.Chat.Go.SDK/models"
)
type logoutResponse struct {
Status
Data struct {
Message string `json:"message"`
} `json:"data"`
}
type logonResponse struct {
Status
Data struct {
Token string `json:"authToken"`
UserID string `json:"userID"`
} `json:"data"`
}
type CreateUserResponse struct {
Status
User struct {
ID string `json:"_id"`
CreatedAt time.Time `json:"createdAt"`
Services struct {
Password struct {
Bcrypt string `json:"bcrypt"`
} `json:"password"`
} `json:"services"`
Username string `json:"username"`
Emails []struct {
Address string `json:"address"`
Verified bool `json:"verified"`
} `json:"emails"`
Type string `json:"type"`
Status string `json:"status"`
Active bool `json:"active"`
Roles []string `json:"roles"`
UpdatedAt time.Time `json:"_updatedAt"`
Name string `json:"name"`
CustomFields map[string]string `json:"customFields"`
} `json:"user"`
}
// Login a user. The Email and the Password are mandatory. The auth token of the user is stored in the Client instance.
//
// https://rocket.chat/docs/developer-guides/rest-api/authentication/login
func (c *Client) Login(credentials *models.UserCredentials) error {
if c.auth != nil {
return nil
}
if credentials.ID != "" && credentials.Token != "" {
c.auth = &authInfo{id: credentials.ID, token: credentials.Token}
return nil
}
response := new(logonResponse)
data := url.Values{"user": {credentials.Email}, "password": {credentials.Password}}
if err := c.PostForm("login", data, response); err != nil {
return err
}
c.auth = &authInfo{id: response.Data.UserID, token: response.Data.Token}
credentials.ID, credentials.Token = response.Data.UserID, response.Data.Token
return nil
}
// CreateToken creates an access token for a user
//
// https://rocket.chat/docs/developer-guides/rest-api/users/createtoken/
func (c *Client) CreateToken(userID, username string) (*models.UserCredentials, error) {
response := new(logonResponse)
data := url.Values{"userId": {userID}, "username": {username}}
if err := c.PostForm("users.createToken", data, response); err != nil {
return nil, err
}
credentials := &models.UserCredentials{}
credentials.ID, credentials.Token = response.Data.UserID, response.Data.Token
return credentials, nil
}
// Logout a user. The function returns the response message of the server.
//
// https://rocket.chat/docs/developer-guides/rest-api/authentication/logout
func (c *Client) Logout() (string, error) {
if c.auth == nil {
return "Was not logged in", nil
}
response := new(logoutResponse)
if err := c.Get("logout", nil, response); err != nil {
return "", err
}
return response.Data.Message, nil
}
// CreateUser being logged in with a user that has permission to do so.
//
// https://rocket.chat/docs/developer-guides/rest-api/users/create
func (c *Client) CreateUser(req *models.CreateUserRequest) (*CreateUserResponse, error) {
body, err := json.Marshal(req)
if err != nil {
return nil, err
}
response := new(CreateUserResponse)
err = c.Post("users.create", bytes.NewBuffer(body), response)
return response, err
}
// UpdateUser updates a user's data being logged in with a user that has permission to do so.
//
// https://rocket.chat/docs/developer-guides/rest-api/users/update/
func (c *Client) UpdateUser(req *models.UpdateUserRequest) (*CreateUserResponse, error) {
body, err := json.Marshal(req)
if err != nil {
return nil, err
}
response := new(CreateUserResponse)
err = c.Post("users.update", bytes.NewBuffer(body), response)
return response, err
}
// SetUserAvatar updates a user's avatar being logged in with a user that has permission to do so.
// Currently only passing an URL is possible.
//
// https://rocket.chat/docs/developer-guides/rest-api/users/setavatar/
func (c *Client) SetUserAvatar(userID, username, avatarURL string) (*Status, error) {
body := fmt.Sprintf(`{ "userId": "%s","username": "%s","avatarUrl":"%s"}`, userID, username, avatarURL)
response := new(Status)
err := c.Post("users.setAvatar", bytes.NewBufferString(body), response)
return response, err
}
|