forked from getfider/fider
/
user.go
110 lines (95 loc) · 3.04 KB
/
user.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
package middlewares
import (
"fmt"
"strconv"
"strings"
"github.com/getfider/fider/app/pkg/validate"
"github.com/getfider/fider/app/models"
"github.com/getfider/fider/app"
"github.com/getfider/fider/app/pkg/errors"
"github.com/getfider/fider/app/pkg/jwt"
"github.com/getfider/fider/app/pkg/web"
webutil "github.com/getfider/fider/app/pkg/web/util"
)
// User gets JWT Auth token from cookie and insert into context
func User() web.MiddlewareFunc {
return func(next web.HandlerFunc) web.HandlerFunc {
return func(c web.Context) error {
var (
token string
user *models.User
)
cookie, err := c.Request.Cookie(web.CookieAuthName)
if err == nil {
token = cookie.Value
} else {
token = webutil.GetSignUpAuthCookie(c)
if token != "" {
webutil.AddAuthTokenCookie(c, token)
}
}
if token != "" {
claims, err := jwt.DecodeFiderClaims(token)
if err != nil {
c.RemoveCookie(web.CookieAuthName)
c.SetClaims(claims)
return next(c)
}
user, err = c.Services().Users.GetByID(claims.UserID)
if err != nil {
if errors.Cause(err) == app.ErrNotFound {
c.RemoveCookie(web.CookieAuthName)
return next(c)
}
return err
}
} else if c.Request.IsAPI() {
authHeader := c.Request.GetHeader("Authorization")
parts := strings.Split(authHeader, "Bearer")
if len(parts) == 2 {
apiKey := strings.TrimSpace(parts[1])
user, err = c.Services().Users.GetByAPIKey(apiKey)
if err != nil {
if errors.Cause(err) == app.ErrNotFound {
return c.HandleValidation(validate.Failed("API Key is invalid"))
}
return err
}
if !user.IsCollaborator() {
return c.HandleValidation(validate.Failed("API Key is invalid"))
}
if impersonateUserIDStr := c.Request.GetHeader("X-Fider-UserID"); impersonateUserIDStr != "" {
if !user.IsAdministrator() {
return c.HandleValidation(validate.Failed("Only Administrators are allowed to impersonate another user"))
}
impersonateUserID, err := strconv.Atoi(impersonateUserIDStr)
if err != nil {
return c.HandleValidation(validate.Failed(fmt.Sprintf("User not found for given impersonate UserID '%s'", impersonateUserIDStr)))
}
user, err = c.Services().Users.GetByID(impersonateUserID)
if err != nil {
if errors.Cause(err) == app.ErrNotFound {
return c.HandleValidation(validate.Failed(fmt.Sprintf("User not found for given impersonate UserID '%s'", impersonateUserIDStr)))
}
return err
}
}
}
}
if user != nil && c.Tenant() != nil && user.Tenant.ID == c.Tenant().ID {
// only administrators should be allowed to sign in to a locked tenant
if c.Tenant().Status == models.TenantLocked && !user.IsAdministrator() {
c.RemoveCookie(web.CookieAuthName)
return c.Unauthorized()
}
// blocked users are unable to sign in
if user.Status == models.UserBlocked {
c.RemoveCookie(web.CookieAuthName)
return c.Unauthorized()
}
c.SetUser(user)
}
return next(c)
}
}
}