-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgin_middelware.go
130 lines (115 loc) · 3.36 KB
/
gin_middelware.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package gauth
import (
"errors"
"net/http"
"strings"
"github.com/COSSAS/gauth/context"
"github.com/COSSAS/gauth/cookies"
"github.com/COSSAS/gauth/api"
"github.com/gin-gonic/gin"
)
func (auth *Authenticator) Middleware(requiredGroups []string) gin.HandlerFunc {
return func(ginContext *gin.Context) {
_, exists := context.GetUserFromContext(ginContext)
if !exists {
api.JSONErrorStatus(ginContext, http.StatusUnauthorized, errors.New("user not authenticated"))
ginContext.Abort()
return
}
userGroups := context.GetUserAssignedGroups(ginContext)
if !hasRequiredGroups(userGroups, requiredGroups) {
api.JSONErrorStatus(ginContext, http.StatusForbidden, errors.New("insufficient permissions"))
ginContext.Abort()
return
}
ginContext.Next()
}
}
func hasRequiredGroups(userGroups []string, requiredGroups []string) bool {
if len(requiredGroups) == 0 {
return true
}
groupSet := make(map[string]bool)
for _, group := range userGroups {
groupSet[group] = true
}
for _, group := range requiredGroups {
if !groupSet[group] {
return false
}
}
return true
}
func (auth *Authenticator) LoadAuthContext() gin.HandlerFunc {
return func(ginContext *gin.Context) {
authToken := ginContext.Request.Header.Get("Authorization")
switch {
case authToken != "":
auth.setBearerAuthContext()(ginContext)
default:
auth.setSessionAuthContext()(ginContext)
}
ginContext.Next()
}
}
func (auth *Authenticator) setSessionAuthContext() gin.HandlerFunc {
return func(ginContext *gin.Context) {
tokenCookie, noCookie, err := auth.Cookiejar.Get(ginContext, cookies.Token)
if noCookie {
ginContext.Redirect(http.StatusFound, "/")
ginContext.Abort()
return
}
if err != nil {
api.JSONErrorStatus(ginContext, http.StatusBadRequest, errors.New("could not get cookie"))
ginContext.Abort()
return
}
user, err := auth.VerifyClaims(ginContext, tokenCookie)
if err != nil {
api.JSONErrorStatus(ginContext, http.StatusUnauthorized, errors.New("could not map token claims"))
ginContext.Abort()
return
}
err = context.SetContext(ginContext, *user)
if err != nil {
api.JSONErrorStatus(ginContext, http.StatusInternalServerError, errors.New("could not set context"))
ginContext.Abort()
return
}
err = context.SetTokenContext(ginContext, tokenCookie)
if err != nil {
api.JSONErrorStatus(ginContext, http.StatusInternalServerError, errors.New("could not set context"))
ginContext.Abort()
return
}
ginContext.Next()
}
}
func (auth *Authenticator) setBearerAuthContext() gin.HandlerFunc {
return func(ginContext *gin.Context) {
authHeader := ginContext.Request.Header.Get("Authorization")
if authHeader == "" {
ginContext.Abort()
}
tokenString := strings.TrimPrefix(authHeader, "Bearer ")
if tokenString == authHeader {
api.JSONErrorStatus(ginContext, http.StatusUnauthorized, errors.New("invalid authorization header format"))
ginContext.Abort()
return
}
user, err := auth.VerifyClaims(ginContext, tokenString)
if err != nil {
api.JSONErrorStatus(ginContext, http.StatusUnauthorized, errors.New("invalid bearer token"))
ginContext.Abort()
return
}
err = context.SetContext(ginContext, *user)
if err != nil {
api.JSONErrorStatus(ginContext, http.StatusInternalServerError, errors.New("could not set context"))
ginContext.Abort()
return
}
ginContext.Next()
}
}