Browse Source

add team and role id

Paul 5 years ago
parent
commit
adebff9ccf
2 changed files with 24 additions and 0 deletions
  1. 16
    0
      middleware/auth/auth.go
  2. 8
    0
      middleware/auth/optional_auth.go

+ 16
- 0
middleware/auth/auth.go View File

10
 
10
 
11
 const (
11
 const (
12
 	CtxRequestHeaderUserId        = "user_id"
12
 	CtxRequestHeaderUserId        = "user_id"
13
+	CtxRequestHeaderTeamId        = "team_id"
14
+	CtxRequestHeaderRoleId        = "role_id"
13
 	ctxRequestHeaderAuthorization = "Authorization"
15
 	ctxRequestHeaderAuthorization = "Authorization"
14
 	ctxRequestCookieAuthorization = "ak"
16
 	ctxRequestCookieAuthorization = "ak"
15
 	ctxRequestTokenExpired        = "expired"
17
 	ctxRequestTokenExpired        = "expired"
58
 				ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"msg": "auth failed, mapClaims[CtxRequestHeaderUserId].(float64) error"})
60
 				ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"msg": "auth failed, mapClaims[CtxRequestHeaderUserId].(float64) error"})
59
 				return
61
 				return
60
 			}
62
 			}
63
+
64
+			if tid, ok := mapClaims[CtxRequestHeaderTeamId].(float64); ok {
65
+				ctx.Set(CtxRequestHeaderTeamId, int64(tid))
66
+			} else {
67
+				ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"msg": "auth failed, mapClaims[CtxRequestHeaderTeamId].(float64) error"})
68
+				return
69
+			}
70
+
71
+			if rid, ok := mapClaims[CtxRequestHeaderRoleId].(float64); ok {
72
+				ctx.Set(CtxRequestHeaderRoleId, int64(rid))
73
+			} else {
74
+				ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"msg": "auth failed, mapClaims[CtxRequestHeaderRoleId].(float64) error"})
75
+				return
76
+			}
61
 		} else {
77
 		} else {
62
 			ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"msg": "auth failed, token.Claims.(jwt.MapClaims) error"})
78
 			ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"msg": "auth failed, token.Claims.(jwt.MapClaims) error"})
63
 			return
79
 			return

+ 8
- 0
middleware/auth/optional_auth.go View File

38
 				if uid, ok := mapClaims[CtxRequestHeaderUserId].(float64); ok {
38
 				if uid, ok := mapClaims[CtxRequestHeaderUserId].(float64); ok {
39
 					ctx.Set(CtxRequestHeaderUserId, int64(uid))
39
 					ctx.Set(CtxRequestHeaderUserId, int64(uid))
40
 				}
40
 				}
41
+
42
+				if tid, ok := mapClaims[CtxRequestHeaderTeamId].(float64); ok {
43
+					ctx.Set(CtxRequestHeaderTeamId, int64(tid))
44
+				}
45
+
46
+				if rid, ok := mapClaims[CtxRequestHeaderRoleId].(float64); ok {
47
+					ctx.Set(CtxRequestHeaderRoleId, int64(rid))
48
+				}
41
 			}
49
 			}
42
 		}
50
 		}
43
 	}
51
 	}