MCPcopy Index your code
hub / github.com/53AI/53AIHub / UserTokenAuth

Function UserTokenAuth

api/middleware/auth.go:15–45  ·  view source on GitHub ↗
(role int64)

Source from the content-addressed store, hash-verified

13)
14
15func UserTokenAuth(role int64) func(c *gin.Context) {
16 return func(c *gin.Context) {
17 token := c.Request.Header.Get("Authorization")
18 token = strings.Replace(token, "Bearer ", "", 1)
19 if token == "" {
20 token = c.Query("access_token")
21 }
22 if token == "" {
23 c.JSON(http.StatusUnauthorized, model.UnauthorizedError.ToResponse(nil))
24 c.Abort()
25 return
26 }
27 user, tokenEid, err := HandleAnyTokenAuth(token, role)
28 if err != nil {
29 switch err.Error() {
30 case "token is expired":
31 c.JSON(http.StatusUnauthorized, model.TokenExpiredError.ToResponse(nil))
32 case "token has invalid claims", "forbidden access":
33 c.JSON(http.StatusUnauthorized, model.ForbiddenError.ToResponse(nil))
34 default:
35 c.JSON(http.StatusUnauthorized, model.UnauthorizedError.ToResponse(nil))
36 }
37
38 c.Abort()
39 return
40 }
41
42 setUserSession(c, user, tokenEid)
43 c.Next()
44 }
45}
46
47func HandleAnyTokenAuth(token string, role int64) (user *model.User, tokenEid int64, err error) {
48 user, tokenEid, err = HandleTokenAuth(token, role)

Callers

nothing calls this directly

Calls 6

HandleAnyTokenAuthFunction · 0.85
setUserSessionFunction · 0.85
ToResponseMethod · 0.80
GetMethod · 0.65
QueryMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected