-
Notifications
You must be signed in to change notification settings - Fork 3
/
authz.go
111 lines (91 loc) · 2.79 KB
/
authz.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
// Copyright 2021 Monoskope Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package eventsourcing
import (
"fmt"
)
// Role is the name of a user's role.
type Role string
func (r Role) String() string {
return string(r)
}
// Scope is the scope of a role.
type Scope string
func (s Scope) String() string {
return string(s)
}
const (
AnyRole Role = "*"
AnyScope Scope = "*"
)
type Policy interface {
// Role returns the role this policy accepts.
Role() Role
// Scope returns the scope this policy accepts.
Scope() Scope
// WithRole sets the role this policy accepts to the given value.
WithRole(Role) Policy
// WithScope sets the scope this policy accepts to the given value.
WithScope(Scope) Policy
// AcceptsRole checks if the policy accepts the given role.
AcceptsRole(Role) bool
// AcceptsScope checks if the policy accepts the given scope.
AcceptsScope(Scope) bool
// String returns a string representation of the policy.
String() string
}
// Policy describes which Role/Scope combination is allowed to execute a certain Command.
type policy struct {
// Role is the Role a user must have due to the policy.
role Role
// Scope is the Scope of the Role a user must have due to the policy.
scope Scope
}
// NewPolicy creates a new policy which accepts anything.
func NewPolicy() Policy {
return &policy{
role: AnyRole,
scope: AnyScope,
}
}
// WithRole sets the role of this policy accepts to the given value.
func (p *policy) WithRole(role Role) Policy {
p.role = role
return p
}
// WithScope sets the scope of this policy accepts to the given value.
func (p *policy) WithScope(scope Scope) Policy {
p.scope = scope
return p
}
// Role returns the role this policy accepts.
func (p *policy) Role() Role {
return p.role
}
// Scope returns the scope this policy accepts.
func (p *policy) Scope() Scope {
return p.scope
}
// AcceptsRole checks if the policy accepts the given role.
func (p *policy) AcceptsRole(role Role) bool {
return p.role == AnyRole || p.role == role
}
// AcceptsScope checks if the policy accepts the given scope.
func (p *policy) AcceptsScope(scope Scope) bool {
return p.scope == AnyScope || p.scope == scope
}
// String returns a string representation of the policy.
func (p *policy) String() string {
return fmt.Sprintf("RO:%s|SC:%s", p.role, p.scope)
}