Przeglądaj źródła

add team and role id

Paul 5 lat temu
rodzic
commit
adebff9ccf
2 zmienionych plików z 24 dodań i 0 usunięć
  1. 16
    0
      middleware/auth/auth.go
  2. 8
    0
      middleware/auth/optional_auth.go

+ 16
- 0
middleware/auth/auth.go Wyświetl plik

@@ -10,6 +10,8 @@ import (
10 10
 
11 11
 const (
12 12
 	CtxRequestHeaderUserId        = "user_id"
13
+	CtxRequestHeaderTeamId        = "team_id"
14
+	CtxRequestHeaderRoleId        = "role_id"
13 15
 	ctxRequestHeaderAuthorization = "Authorization"
14 16
 	ctxRequestCookieAuthorization = "ak"
15 17
 	ctxRequestTokenExpired        = "expired"
@@ -58,6 +60,20 @@ func Auth(authKey string, session Session) gin.HandlerFunc {
58 60
 				ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"msg": "auth failed, mapClaims[CtxRequestHeaderUserId].(float64) error"})
59 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 77
 		} else {
62 78
 			ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"msg": "auth failed, token.Claims.(jwt.MapClaims) error"})
63 79
 			return

+ 8
- 0
middleware/auth/optional_auth.go Wyświetl plik

@@ -38,6 +38,14 @@ func OptionalAuth(authKey string) gin.HandlerFunc {
38 38
 				if uid, ok := mapClaims[CtxRequestHeaderUserId].(float64); ok {
39 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
 	}