-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequire_groups_middleware.go
79 lines (67 loc) · 2.43 KB
/
require_groups_middleware.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
package gincloudflareaccess
import (
"errors"
"github.com/gin-gonic/gin"
)
type groupsRequirements struct {
allGroups []string
anyGroups []string
}
// RequireGroup will build a middleware restricting access
// to users belonging to a specific LDAP group
//
// note that as every middleware, .RequireGroup() can be applied to a single route,
// to a route group or to the whole router
func (instance *cloudflareAccessMiddlewareImpl) RequireGroup(group string) gin.HandlerFunc {
return buildGroupCheckMiddleware(instance, &groupsRequirements{
allGroups: []string{group},
})
}
// RequireAnyGroup will build a middleware restricting access
// to users belonging to at least one of some LDAP groups
//
// note that as every middleware, .RequireAnyGroup() can be applied to a single route,
// to a route group or to the whole router
func (instance *cloudflareAccessMiddlewareImpl) RequireAnyGroup(groups []string) gin.HandlerFunc {
return buildGroupCheckMiddleware(instance, &groupsRequirements{
anyGroups: groups,
})
}
// RequireAllGroups will build a middleware restricting access
// to users belonging to every one of the specified LDAP groups
//
// note that as every middleware, .RequireAllGroups() can be applied to a single route,
// to a route group or to the whole router
func (instance *cloudflareAccessMiddlewareImpl) RequireAllGroups(groups []string) gin.HandlerFunc {
return buildGroupCheckMiddleware(instance, &groupsRequirements{
allGroups: groups,
})
}
func buildGroupCheckMiddleware(instance *cloudflareAccessMiddlewareImpl, requirements *groupsRequirements) gin.HandlerFunc {
return func(c *gin.Context) {
assertRequestProcessedByAuthenticator(c)
if requirements == nil || ((requirements.allGroups == nil || len(requirements.allGroups) < 1) && (requirements.anyGroups == nil || len(requirements.anyGroups) < 1)) {
c.Next()
return
}
principal := GetPrincipal(c)
if principal == nil {
instance.handleUnauthorized(c, errors.New("authentication required"))
return
}
if requirements.allGroups != nil && len(requirements.allGroups) > 0 {
if !principalInAllGroups(principal, requirements.allGroups) {
instance.handleForbidden(c, errors.New("forbidden"))
return
}
}
if requirements.anyGroups != nil && len(requirements.anyGroups) > 0 {
if !principalInAnyGroups(principal, requirements.anyGroups) {
instance.handleForbidden(c, errors.New("forbidden"))
return
}
}
// May now proceed
c.Next()
}
}