-
Notifications
You must be signed in to change notification settings - Fork 6
/
auth.go
45 lines (39 loc) · 1.01 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
package auth
import (
"github.com/dgrijalva/jwt-go"
"github.com/hyacinthus/x/xerr"
"github.com/labstack/echo/v4"
)
// ParseJWT 在 echo 的 jwt 插件验证完成后,解析我们需要的数据存储到 context
// TODO: 已经废弃这一部分,等到完全不被依赖后删除。
func ParseJWT(c echo.Context) {
user := c.Get("user").(*jwt.Token)
claims := user.Claims.(jwt.MapClaims)
c.Set("uid", claims["uid"])
c.Set("oid", claims["oid"])
c.Set("role", claims["role"])
}
// CheckOwner 检查是否机构所有者
func CheckOwner(c echo.Context) error {
role := int(c.Get("role").(float64))
if role != 0 {
return xerr.ErrForbidden
}
return nil
}
// CheckAdmin 检查是否机构管理员
func CheckAdmin(c echo.Context) error {
role := int(c.Get("role").(float64))
if role > 1 {
return xerr.ErrForbidden
}
return nil
}
// GetUID 获得 user id
func GetUID(c echo.Context) string {
return c.Get("uid").(string)
}
// GetOID 获得 org id
func GetOID(c echo.Context) string {
return c.Get("oid").(string)
}