-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
350 lines (335 loc) · 12.5 KB
/
http.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package main
import (
"fmt"
"net/http"
"time"
"github.com/mdigger/log"
"github.com/mdigger/rest"
)
type Service struct {
mux *rest.ServeMux // мультиплексор запросов
config *Config // конфигурация сервиса
}
// NewService инициализирует новый сервис по конфигурации.
func NewService(config *Config) *Service {
// инициализируем мультиплексор HTTP-запросов
var mux = &rest.ServeMux{
Headers: map[string]string{
"Server": "Pusher/2.0",
"X-API-Version": "1.1",
"X-Service-Version": version,
},
Logger: log.Default,
}
var service = &Service{
mux: mux,
config: config,
}
// добавляем обработчики запросов администрирования
mux.Handle("GET", "/users", service.GetUsers)
mux.Handle("POST", "/users", service.AddUser)
mux.Handle("DELETE", "/users/:login", service.RemoveUser)
mux.Handle("PUT", "/users/:login", service.ChangeUser)
// токены устройств пользователя
mux.Handle("GET", "/apns/:topic/users/:login", service.GetTokens)
mux.Handle("POST", "/apns/:topic/users/:login", service.AddToken)
// отправка push-уведомлений
mux.Handle("POST", "/apns/:topic/push", service.Push)
mux.Handle("POST", "/apns/:topic/users/:login/push", service.PushUser)
return service
}
// AdminAuth проверяет авторизацию администратора. Возвращает 0, nil, если
// администратор успешно авторизован.
func (s *Service) AdminAuth(c *rest.Context) error {
if !s.config.IsAdminAuthorization() {
return nil // авторизация не требуется
}
// разбираем заголовок с авторизацией
login, password, ok := c.BasicAuth()
if !ok {
realm := fmt.Sprintf("Basic realm=%s admin", appName)
c.SetHeader("WWW-Authenticate", realm)
return rest.ErrUnauthorized
}
// проверяем авторизацию
if !s.config.AdminAuthorization(login, password) {
return rest.ErrForbidden
}
return nil // администратор авторизован
}
// GetUsers отдает список пользователей для авторизации.
func (s *Service) GetUsers(c *rest.Context) error {
// проверяем авторизацию администратора
if err := s.AdminAuth(c); err != nil {
return err
}
// отдаем список пользователей
return c.Write(rest.JSON{"users": s.config.UsersList()})
}
// AddUser регистрирует нового пользователя для авторизации обращения к сервису.
func (s *Service) AddUser(c *rest.Context) error {
// проверяем авторизацию администратора
if err := s.AdminAuth(c); err != nil {
return err
}
// разбираем информацию о пользователе из запроса
var user = new(struct {
Login string `json:"login" form:"login"`
Password string `json:"password" form:"password"`
})
err := c.Bind(user)
if err != nil {
return err
}
if user.Login == "" {
return rest.ErrBadRequest
}
// добавляем информацию о пользователе
exist := s.config.AddUser(user.Login, user.Password)
// если это новый пользователь, то отдаем статус создания
var code = http.StatusOK
if !exist {
code = http.StatusCreated
}
c.SetStatus(code)
// отдаем список пользователей
return c.Write(rest.JSON{"users": s.config.UsersList()})
}
// RemoveUser удаляет пользователя из списка авторизации.
func (s *Service) RemoveUser(c *rest.Context) error {
// проверяем авторизацию администратора
if err := s.AdminAuth(c); err != nil {
return err
}
// получаем логин из запроса пути
var login = c.Param("login")
// удаляем пользователя из списка
exist := s.config.RemoveUser(login)
if !exist {
return c.Error(http.StatusNotFound, fmt.Sprintf("user %s not registered", login))
}
// отдаем список пользователей
return c.Write(rest.JSON{"users": s.config.UsersList()})
}
// ChangeUser изменяет пароль пользователя для авторизации.
func (s *Service) ChangeUser(c *rest.Context) error {
// проверяем авторизацию администратора
if err := s.AdminAuth(c); err != nil {
return err
}
// получаем логин из запроса пути
var login = c.Param("login")
// получаем пароль из запроса
var password = new(struct {
Password string `json:"password" form:"password"`
})
err := c.Bind(password)
if err != nil {
return err
}
// изменяем пароль пользователя
exist := s.config.AddUser(login, password.Password)
code := http.StatusOK
if !exist {
code = http.StatusCreated
}
c.SetStatus(code)
// отдаем список пользователей
return c.Write(rest.JSON{"users": s.config.UsersList()})
}
// UserAuth проверяет авторизацию пользователя. Возвращает 0, nil, если
// пользователь успешно авторизован или авторизация не требуется.
func (s *Service) UserAuth(c *rest.Context) error {
if !s.config.IsUserAuthorization() {
return nil // авторизация не требуется
}
// разбираем заголовок с авторизацией
login, password, ok := c.BasicAuth()
if !ok {
realm := fmt.Sprintf("Basic realm=%s", appName)
c.SetHeader("WWW-Authenticate", realm)
return rest.ErrUnauthorized
}
// проверяем авторизацию
if !s.config.UserAuthorization(login, password) {
return rest.ErrForbidden
}
return nil // администратор авторизован
}
// GetTokens отдает список зарегистрированных токенов пользователя
func (s *Service) GetTokens(c *rest.Context) error {
// проверяем авторизацию пользователя
if err := s.UserAuth(c); err != nil {
return err
}
user := c.Param("login") // логин пользователя
if user == "" {
return rest.ErrNotFound
}
topic := c.Param("topic") // тема
if topic == "" {
return rest.ErrNotFound
}
query := c.Request.URL.Query() // разобранные параметры запроса
sandbox := len(query["sandbox"]) != 0 // флаг sandbox
// запрашиваем список токенов пользователя
tokens, err := s.config.Store.GetUserTopicTokens(topic, sandbox, user)
if err != nil {
return err
}
if len(tokens) == 0 {
return c.Error(http.StatusNotFound,
fmt.Sprintf("tokens for user %s not registered", user))
}
return c.Write(rest.JSON{"tokens": tokens})
}
// AddToken регистрирует токен пользовательского устройства.
func (s *Service) AddToken(c *rest.Context) error {
// проверяем авторизацию пользователя
if err := s.UserAuth(c); err != nil {
return err
}
user := c.Param("login") // логин пользователя
if user == "" {
return rest.ErrNotFound
}
topic := c.Param("topic") // тема
if topic == "" {
return c.Error(http.StatusNotFound, "empty topic")
}
query := c.Request.URL.Query() // разобранные параметры запроса
sandbox := len(query["sandbox"]) != 0 // флаг sandbox
var token = new(struct {
Token string `json:"token" form:"token"`
})
err := c.Bind(token)
if err != nil {
return err
}
// сохраняем в хранилище токенов устройств
err = s.config.AddToken(user, topic, token.Token, sandbox)
if err != nil {
return err
}
// запрашиваем список токенов пользователя
tokens, err := s.config.Store.GetUserTopicTokens(topic, sandbox, user)
if err != nil {
return err
}
c.SetStatus(http.StatusCreated)
return c.Write(rest.JSON{"tokens": tokens})
}
// PushUser отправляет push-уведомления на все устройства пользователя.
func (s *Service) PushUser(c *rest.Context) error {
// проверяем авторизацию пользователя
if err := s.UserAuth(c); err != nil {
return err
}
user := c.Param("login") // логин пользователя
if user == "" {
return rest.ErrNotFound
}
topic := c.Param("topic") // тема
if topic == "" {
return c.Error(http.StatusNotFound, "empty topic")
}
query := c.Request.URL.Query() // разобранные параметры запроса
sandbox := len(query["sandbox"]) != 0 // флаг sandbox
// получаем список токенов пользователя
tokens, err := s.config.Store.GetUserTopicTokens(topic, sandbox, user)
if err != nil {
return err
}
if len(tokens) == 0 {
return c.Error(http.StatusNotFound,
fmt.Sprintf("tokens for user %s not registered", user))
}
// разбираем запроса для отправки уведомления
var notification = new(struct {
Payload map[string]interface{} `json:"payload" form:"payload"`
Expiration time.Time `json:"expiration" form:"expiration"`
LowPriority bool `json:"lowPriority" form:"lowPriority"`
CollapseID string `json:"collapseId" form:"collapseId"`
})
err = c.Bind(notification)
if err != nil {
return err
}
if len(notification.Payload) == 0 {
return c.Error(http.StatusBadRequest, "empty payload")
}
// формируем данные для уведомления
var n = Notification{
Payload: notification.Payload,
Expiration: notification.Expiration,
LowPriority: notification.LowPriority,
Topic: topic,
CollapseID: notification.CollapseID,
Sandbox: sandbox,
}
// отправляем на все токены пользователя
// отправляем на все токены пользователя
sent, err := s.config.Push(n, tokens)
if err != nil {
return err
}
// отдаем количество отправленных сообщений
return c.Write(rest.JSON{"sent": sent})
}
// Push отправляет push-уведомления на все устройства указанных в запросе
// пользователей.
func (s *Service) Push(c *rest.Context) error {
// проверяем авторизацию пользователя
if err := s.UserAuth(c); err != nil {
return err
}
topic := c.Param("topic") // тема
if topic == "" {
return c.Error(http.StatusNotFound, "empty topic")
}
query := c.Request.URL.Query() // разобранные параметры запроса
sandbox := len(query["sandbox"]) != 0 // флаг sandbox
// разбираем запроса для отправки уведомления
var notification = new(struct {
Payload map[string]interface{} `json:"payload" form:"payload"`
Expiration time.Time `json:"expiration" form:"expiration"`
LowPriority bool `json:"lowPriority" form:"lowPriority"`
CollapseID string `json:"collapseId" form:"collapseId"`
Users []string `json:"users" form:"user"`
})
err := c.Bind(notification)
if err != nil {
return err
}
if len(notification.Payload) == 0 {
return c.Error(http.StatusBadRequest, "empty payload")
}
if len(notification.Users) == 0 {
return c.Error(http.StatusBadRequest, "empty users list")
}
// получаем список токенов пользователя
tokens, err := s.config.Store.GetUserTopicTokens(topic, sandbox,
notification.Users...)
if err != nil {
return err
}
if len(tokens) == 0 {
return c.Error(http.StatusNotFound, "tokens not registered")
}
// формируем данные для уведомления
var n = Notification{
Payload: notification.Payload,
Expiration: notification.Expiration,
LowPriority: notification.LowPriority,
Topic: topic,
CollapseID: notification.CollapseID,
Sandbox: sandbox,
}
// отправляем на все токены пользователя
sent, err := s.config.Push(n, tokens)
if err != nil {
return err
}
// отдаем количество отправленных сообщений
return c.Write(rest.JSON{"sent": sent})
}