-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.go
535 lines (450 loc) · 11.8 KB
/
app.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
package gocqrs
import (
"encoding/json"
"errors"
"github.com/dgrijalva/jwt-go"
"gopkg.in/gin-gonic/gin.v1"
"log"
"strconv"
"strings"
"sync"
"time"
)
var (
InvalidEntityError = errors.New("Invalid entity in command")
InvalidReferenceError = errors.New("Invalid reference")
BaseUnseted = errors.New("Basestruct not set, should be setted")
)
const (
EventTypeHeader = "X-Event"
EntityVersionHeader = "X-LockVersion"
CreateEventHeader = "X-Create"
EntityHeader = "X-Entity"
EventIDHeader = "X-EventID"
EntityGroupHeader = "X-Group"
SessionHeader = "X-Session"
UserHeader = "X-User"
CookieName = "san"
)
var runningApp *App
type App struct {
lock sync.Mutex
Version string `json:"version"`
Name string `json:"name"`
Port string `json:"port"`
// main log stream
MainLog string `json:"mlog"`
// Documentations
BaseURL string `json:"baseURL"`
Endpoints []Endpoint `json:"endpoints"`
// user roles for auth
Roles map[string]Role `json:"roles"`
Entities map[string]*EntityConf `json:"entities"`
Store EventStore `json:"-"`
// Gin router
Router *gin.Engine
// turn off auth service check
FirstRun bool `json:"firtRun"`
AuthOff bool `json:"authOff"`
Secret string `json:"-"`
SessionValidity string `json:"sessionValidity"`
sduration time.Duration
Domain string `json:"sessionDomain"`
LoginReferers []string `json:"loginReferers"`
}
func NewApp(name string, store EventStore) *App {
var app App
if name == "" {
log.Fatal("Invalid app name")
}
app.Name = name
app.Roles = make(map[string]Role)
app.Entities = make(map[string]*EntityConf)
app.Endpoints = make([]Endpoint, 0)
app.Router = gin.New()
app.Store = store
app.MainLog = strings.Replace(strings.ToLower(app.Name), " ", "_", -1) + "_log"
// set default session validity
app.SessionValidity = "300m"
d, _ := time.ParseDuration(app.SessionValidity)
app.sduration = d
return &app
}
func (app *App) String() string {
b, _ := json.Marshal(app)
return string(b)
}
// Add Auth functionality
func (app *App) Auth(evh ...EventHandler) {
userEntity := NewEntityConf(UserEntity)
userEntity.AddCRUD(false)
for _, h := range evh {
userEntity.AddEventHandler(h)
}
userEntity.AddEventHandler(UserEventHandler{})
userEntity.SetBaseStruct(User{})
app.RegisterEntity(userEntity)
}
func (app *App) SessionTTL(d string) {
sd, err := time.ParseDuration(d)
if err != nil {
log.Fatal(err)
}
app.SessionValidity = d
app.sduration = sd
}
func (app *App) AddRoles(roles ...Role) {
for _, r := range roles {
app.Roles[r.Name] = r
}
}
func (app *App) RegisterEntity(e *EntityConf) *App {
_, has := app.Entities[e.Name]
if !has {
app.Entities[e.Name] = e
} else {
log.Fatal("Entity already added")
}
return app
}
func (app *App) HandleEvent(entityName, id, accid, userid, role string, ev Eventer, versionLock uint64) (string, uint64, error) {
var err error
app.lock.Lock()
defer app.lock.Unlock()
econf, ok := app.Entities[entityName]
if !ok {
return "", 0, InvalidEntityError
}
// look for entity events, TODO eventstore should cache streams
stream := entityName + "-" + id
ch, _ := app.Store.Range(stream)
entity, err := econf.Aggregate(id, ch)
if err != nil {
return "", 0, err
}
h, has := econf.EventHandlers[ev.GetType()]
if !has {
return "", 0, errors.New("Invalid handler for event:" + ev.GetType())
}
// check base
if h.CheckBase(ev) {
if econf.BaseSeted {
err = econf.checkBase(ev.GetData())
if err != nil {
return "", 0, err
}
} else {
return "", 0, BaseUnseted
}
}
// handler event
opt, err := h.Handle(id, accid, userid, role, ev, entity, false)
if err != nil {
return "", 0, err
}
// check if references exist
for _, r := range econf.EntityReferences {
var v string
value := entity.Data[r.Key]
switch value.(type) {
case string:
v = value.(string)
err = app.CheckReference(r.Entity, r.Key, v, r.Null)
if err != nil {
return "", 0, err
}
case []string:
for _, v := range value.([]string) {
err = app.CheckReference(r.Entity, r.Key, v, r.Null)
if err != nil {
return "", 0, err
}
}
case nil:
if !r.Null {
return "", 0, errors.New("Invalid reference type, should be string")
}
default:
return "", 0, errors.New("Invalid reference type, should be string")
}
}
//validate entity
for n, v := range econf.Validators {
err = v.Validate(*entity)
if err != nil {
return "", 0, errors.New("Failed validation: " + n + " - " + err.Error())
}
}
version, err := app.Store.Store(ev, opt)
return entity.ID, version, err
}
// Start app
func (app *App) Run(port string) error {
app.Router.GET("/up", UpHandler)
app.Router.POST("/event/:entity", HTTPEventHandler)
app.Router.GET("/docs", DocHandler)
app.Router.GET("/docs/:entity", EventsDocHandler)
app.Router.GET("/entity/:entity/:id", EntityHandler)
app.Router.POST("/auth", AuthHandler)
app.Router.POST("/session/renew", AuthRenewHandler)
runningApp = app
log.Println("-----------------------------------", "\n")
log.Println("Correlation stream: ", app.MainLog)
log.Println("-----------------------------------")
return runningApp.Router.Run(port)
}
func UpHandler(c *gin.Context) {
c.AbortWithStatus(200)
}
type SessionClaims struct {
Username string `json:"use"`
Role string `json:"rol"`
jwt.StandardClaims
}
func AuthHandler(c *gin.Context) {
// AUTH USER
var u User
username := c.PostForm("u")
password := c.PostForm("p")
t := c.PostForm("t")
e, _, err := runningApp.Entity(UserEntity, username)
if err != nil {
c.JSON(401, map[string]string{"error": ".Failed to login:" + err.Error()})
return
}
e.Decode(&u)
err = u.CheckPassword(password)
if err != nil {
c.JSON(401, map[string]string{"error": "Failed to login p:" + err.Error()})
return
}
allowedReferer := false
// I have to check referers to login, or ask for user token
for _, r := range runningApp.LoginReferers {
if r == c.Request.Referer() {
allowedReferer = true
break
}
}
if t != "" {
err = u.CheckToken(t)
if err != nil {
c.JSON(401, map[string]string{"error": "Failed to login"})
return
}
}
if !allowedReferer {
//TODO CHECK IP
}
tokenString := BuildToken(u)
c.SetCookie("cs", tokenString, -1, "/", runningApp.Domain, false, true)
c.JSON(200, map[string]string{"auth-token": tokenString})
}
func AuthRenewHandler(c *gin.Context) {
// Renew session
}
func HTTPEventHandler(c *gin.Context) {
var err error
var userid string
var role string
var accid string
entityName := c.Param("entity")
eventType := c.Request.Header.Get(EventTypeHeader)
eventVersion := c.Request.Header.Get(EntityVersionHeader)
eventID := c.Request.Header.Get(EventIDHeader)
entityID := c.Request.Header.Get(EntityHeader)
if entityID == "" {
err := errors.New("Invalid entityid")
if err != nil {
c.JSON(400, map[string]interface{}{"error": err.Error()})
return
}
}
data := make(map[string]interface{})
// Auth event
if !runningApp.AuthOff {
claims, err := runningApp.auth(eventType, c)
if err != nil {
c.JSON(401, map[string]interface{}{"error": err.Error()})
return
}
userid = claims.Username
role = claims.Role
} else {
userid = c.Request.Header.Get(UserHeader)
if userid == "" {
c.JSON(401, map[string]interface{}{"error": "Invalid noauth username"})
return
}
if !runningApp.FirstRun {
err = runningApp.CheckReference(UserEntity, "", userid, false)
if err != nil {
c.JSON(401, map[string]interface{}{"error": "Invalid noauth username does not exist, try to run as: firstrun"})
return
}
}
}
err = c.BindJSON(&data)
if err != nil {
c.JSON(400, map[string]interface{}{"error": err.Error()})
return
}
versionLock, _ := strconv.ParseUint(eventVersion, 10, 64)
// get event id
event := NewEvent(eventID, eventType, data)
event.Entity = entityName
event.EventID = eventID
event.EntityID = entityID
event.CorrelationStream = runningApp.MainLog
// create event
id, version, err := runningApp.HandleEvent(event.Entity, event.EntityID, accid, userid, role, event, versionLock)
if err != nil {
c.JSON(400, map[string]interface{}{"error": err.Error()})
return
}
c.JSON(201, map[string]interface{}{"entity": entityName, "entity-id": id, "version": version})
return
}
func DocHandler(c *gin.Context) {
c.JSON(200, GenerateDocs(runningApp))
}
func EventsDocHandler(c *gin.Context) {
e := c.Param("entity")
c.JSON(200, GenerateDocs(runningApp).GetEvents(e))
}
func EntityHandler(c *gin.Context) {
//var cl *SessionClaims
var err error
e := c.Param("entity")
id := c.Param("id")
if !runningApp.AuthOff {
// I should auth read also
_, err = runningApp.authRead(e, c)
if err != nil {
c.JSON(401, map[string]string{"error1": err.Error()})
return
}
}
// get entity
entity, _, err := runningApp.Entity(e, id)
if err != nil {
c.JSON(400, map[string]string{"error": err.Error()})
return
}
_, ok := runningApp.Entities[e]
if !ok {
c.JSON(400, map[string]string{"error": "invalid entity conf"})
return
}
c.JSON(200, entity)
}
func (app *App) Entity(name, id string) (*Entity, uint64, error) {
econf, ok := app.Entities[name]
if !ok {
return nil, 0, errors.New("Invalid entity name")
}
// look for entity events, TODO eventstore should cache streams
stream := name + "-" + id
ch, version := app.Store.Range(stream)
entity, err := econf.Aggregate(id, ch)
if err != nil {
return nil, 0, err
}
entity.Version = version
return entity, version, err
}
func (app *App) authRole(role, eventType string) bool {
allowed := false
for _, r := range app.Roles {
if r.Name == role {
allowed = r.Can(eventType)
break
}
}
return allowed
}
func (app *App) CheckReference(e, k, value string, null bool) error {
if value == "" && null {
return nil
}
var stream string
stream = e + "-" + value
_, err := app.Store.Version(stream)
if err != nil {
return errors.New(InvalidReferenceError.Error() + ": " + k + " - " + value + " - " + stream + " - " + err.Error())
}
return err
}
func (app *App) authRead(entity string, c *gin.Context) (*SessionClaims, error) {
var err error
t := ""
// Read cookie
cookieVal, err := c.Cookie(CookieName)
if cookieVal == "" {
t = c.Request.Header.Get(SessionHeader)
} else {
t = cookieVal
}
// create session claimsfrom token
token, err := jwt.ParseWithClaims(t, &SessionClaims{}, func(token *jwt.Token) (interface{}, error) {
return []byte(app.Secret), nil
})
if token == nil {
return nil, err
}
if claims, ok := token.Claims.(*SessionClaims); ok && token.Valid {
r, exist := app.Roles[claims.Role]
if !exist {
return claims, errors.New("Invalid role")
}
if !r.CanRead(entity) {
return claims, errors.New("Cannot read entity")
}
return claims, err
} else {
return nil, err
}
}
func (app *App) auth(event string, c *gin.Context) (*SessionClaims, error) {
var err error
t := ""
// Read cookie
cookieVal, err := c.Cookie(CookieName)
if cookieVal == "" {
t = c.Request.Header.Get(SessionHeader)
} else {
t = cookieVal
}
// create session claimsfrom token
claims, err := AuthToken(t, app.Secret)
if err != nil {
return nil, err
}
if !app.authRole(claims.Role, event) {
return nil, errors.New("Invalid Role, " + claims.Role)
}
return claims, err
}
func (app *App) getSession(c *gin.Context) (*SessionClaims, error) {
var err error
t := ""
// Read cookie
cookieVal, err := c.Cookie(CookieName)
if cookieVal == "" {
t = c.Request.Header.Get(SessionHeader)
} else {
t = cookieVal
}
// create session claimsfrom token
token, err := jwt.ParseWithClaims(t, &SessionClaims{}, func(token *jwt.Token) (interface{}, error) {
return []byte(app.Secret), nil
})
if token == nil {
return nil, err
}
if claims, ok := token.Claims.(*SessionClaims); ok && token.Valid {
return claims, err
} else {
return nil, err
}
}