File size: 3,359 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
131
132
133
134
135
package permission

import (
	"context"
	"strings"
	"sync/atomic"
)

// A Checker is used to give a pass-or-fail result for a given context.
type Checker func(context.Context) bool

func checkLimit(ctx context.Context) error {
	n, ok := ctx.Value(contextKeyCheckCount).(*uint64)
	if !ok {
		return newGeneric(false, "invalid auth context for check limit")
	}
	max, ok := ctx.Value(contextKeyCheckCountMax).(uint64)
	if !ok {
		return newGeneric(false, "invalid auth context for max check limit")
	}

	v := atomic.AddUint64(n, 1) // always add
	if max > 0 && v > max {
		return newGeneric(false, "exceeded auth check limit")
	}

	return nil
}

// LimitCheckAny will return a permission error if none of the checks pass, or
// if the auth check limit is reached. If no checks are provided, only
// the limit check, and a check that the context has SOME type authorization is
// performed. nil can be passed as an always-fail check option (useful to prevent
// the no-check behavior, if required).
func LimitCheckAny(ctx context.Context, checks ...Checker) error {
	if !All(ctx) {
		return newGeneric(true, "")
	}

	// ensure we don't get hammered with auth checks (or DB calls, for example)
	err := checkLimit(ctx)
	if err != nil {
		return err
	}

	if len(checks) == 0 {
		return nil
	}
	for _, c := range checks {
		if c != nil && c(ctx) {
			return nil
		}
	}

	return newGeneric(false, "")
}

// All is a Checker that checks against ALL providers, returning true
// if any are found.
func All(ctx context.Context) bool {
	if v, ok := ctx.Value(contextHasAuth).(int); ok && v == 1 {
		return true
	}

	return false
}

// Admin is a Checker that determines if a context has the Admin or System role.
func Admin(ctx context.Context) bool {
	if System(ctx) {
		return true
	}
	r, ok := ctx.Value(contextKeyUserRole).(Role)
	if ok && r == RoleAdmin {
		return true
	}

	return false
}

// User is a Checker that determines if a context has the User, Admin or System role.
func User(ctx context.Context) bool {
	if System(ctx) {
		return true
	}
	r, ok := ctx.Value(contextKeyUserRole).(Role)
	if ok && (r == RoleUser || r == RoleAdmin) {
		return true
	}

	return false
}

// Service is a Checker that determines if a context has a serviceID.
func Service(ctx context.Context) bool {
	return ServiceID(ctx) != ""
}

// System is a Checker that determines if a context has system privileges.
func System(ctx context.Context) bool {
	return SystemComponentName(ctx) != ""
}

// Team is a Checker that determines if a context has team privileges.
func Team(ctx context.Context) bool {
	return TeamID(ctx) != ""
}

// MatchService will return a Checker that ensures the context has the given ServiceID.
func MatchService(serviceID string) Checker {
	return func(ctx context.Context) bool {
		if serviceID == "" {
			return false
		}
		return ServiceID(ctx) == strings.ToLower(serviceID)
	}
}

// MatchTeam will return a Checker that ensures the context has the given TeamID.
func MatchTeam(teamID string) Checker {
	return func(ctx context.Context) bool {
		return TeamID(ctx) == strings.ToLower(teamID)
	}
}

// MatchUser will return a Checker that ensures the context has the given UserID.
func MatchUser(userID string) Checker {
	return func(ctx context.Context) bool {
		if userID == "" {
			return false
		}
		return UserID(ctx) == strings.ToLower(userID)
	}
}