-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathauth.go
76 lines (68 loc) · 1.76 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
package main
import (
"github.com/goadesign/goa"
"github.com/gopheracademy/congo/app"
"github.com/gopheracademy/congo/jwt"
"github.com/gopheracademy/congo/models"
)
// AuthController manages authentication
type AuthController struct {
*goa.Controller
e *Env
tm *jwt.TokenManager
spec *jwt.Specification
}
// NewAuthController creates a auth controller.
func NewAuthController(service *goa.Service, e *Env, tm *jwt.TokenManager, spec *jwt.Specification) *AuthController {
return &AuthController{Controller: service.NewController("AuthController"),
e: e,
tm: tm,
spec: spec,
}
}
// Refresh runs the refresh action.
func (c *AuthController) Refresh(ctx *app.RefreshAuthContext) error {
// TBD: implement
return nil
}
// Token runs the token action.
func (c *AuthController) Token(ctx *app.TokenAuthContext) error {
// authenticate
u, p, _ := ctx.Request.BasicAuth()
goa.LogInfo(ctx, "token auth", "user", u)
login := models.Login{
Email: u,
Password: p,
}
userdb := models.NewUserDB(c.e.DB)
user, err := userdb.GetByLogin(ctx, login)
if err != nil {
goa.LogError(ctx, "find user by login", "error", err.Error())
return err
}
tenantdb := models.NewTenantDB(c.e.DB)
var tenant models.Tenant
var e error
if user.TenantID > 0 {
tenant, e = tenantdb.Get(ctx, user.TenantID)
if e != nil {
goa.LogError(ctx, "find tenant by id", "error", e.Error())
return err
}
}
// create token
claims := make(map[string]interface{})
claims["sub"] = user.ID
claims["role"] = user.Role
t, err := c.tm.Create(claims)
// return token
a := &app.Authorize{}
tt := "Bearer"
a.TokenType = &tt
a.AccessToken = &t
ttl := c.spec.TTLMinutes * 60
a.ExpiresIn = &ttl
a.User = user.UserToUser()
a.Tenant = tenant.TenantToTenant()
return ctx.Created(a)
}