-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
139 lines (116 loc) · 4.76 KB
/
token.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
131
132
133
134
135
136
137
138
139
// MIT License
//
// Copyright (c) 2022 xybor-x
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package xypriv
import "strings"
// LeastPrivilegeToken is a Token implements Delegatee. It uses the principle of
// least privilege.
type LeastPrivilegeToken struct {
rules map[string]bool
}
// NewToken creates a LeastPrivilegeToken that implements Delegatee. It uses the
// principle of least privilege. By default, all privileges is rejected.
func NewToken() *LeastPrivilegeToken {
return &LeastPrivilegeToken{
rules: make(map[string]bool),
}
}
// AllowAction allows all privileges on action.
func (t *LeastPrivilegeToken) AllowAction(action ...string) {
t.setRule("", "", action, true)
}
// AllowScope allows all privileges in scope.
func (t *LeastPrivilegeToken) AllowScope(scope any) {
t.setRule("", scope, nil, true)
}
// AllowRelation allows all privileges of relation in scope.
func (t *LeastPrivilegeToken) AllowRelation(relation Relation, scope any) {
t.setRule(relation, scope, nil, true)
}
// AllowActionInScope allows all privileges on action in scope.
func (t *LeastPrivilegeToken) AllowActionInScope(scope any, action ...string) {
t.setRule("", scope, action, true)
}
// Allow allows all privileges of relation on action in scope.
func (t *LeastPrivilegeToken) Allow(relation Relation, scope any, action ...string) {
t.setRule(relation, scope, action, true)
}
// BanAction bans all privileges on action.
func (t *LeastPrivilegeToken) BanAction(action ...string) {
t.setRule("", "", action, false)
}
// BanScope bans all privileges in scope.
func (t *LeastPrivilegeToken) BanScope(scope any) {
t.setRule("", scope, nil, false)
}
// BanRelation bans all privileges of relation in scope.
func (t *LeastPrivilegeToken) BanRelation(relation Relation, scope any) {
t.setRule(relation, scope, nil, false)
}
// BanActionInScope bans all privileges on action in scope.
func (t *LeastPrivilegeToken) BanActionInScope(scope any, action ...string) {
t.setRule("", scope, action, false)
}
// Ban bans all privileges of relation on action in scope.
func (t *LeastPrivilegeToken) Ban(relation Relation, scope any, action ...string) {
t.setRule(relation, scope, action, false)
}
// Delegate checks if condition tuple is allowed or banned.
func (t LeastPrivilegeToken) Delegate(relation Relation, resource Resource, action ...string) bool {
var relName = string(relation)
var rsrName = getName(resource)
var ctxName = getName(resource.Context())
var actName = strings.Join(action, "_")
var keys = []string{
// Full-parameter keys.
strings.Join([]string{actName, relName, rsrName}, "."),
strings.Join([]string{actName, relName, ctxName}, "."),
// Partial keys.
strings.Join([]string{"", relName, rsrName}, "."),
strings.Join([]string{"", relName, ctxName}, "."),
strings.Join([]string{actName, "", rsrName}, "."),
strings.Join([]string{actName, "", ctxName}, "."),
// One-parameter keys.
strings.Join([]string{"", "", rsrName}, "."),
strings.Join([]string{"", "", ctxName}, "."),
strings.Join([]string{actName, "", ""}, "."),
}
var isAllow = false
for _, k := range keys {
if val, ok := t.rules[k]; ok {
if !val {
return false
}
isAllow = true
}
}
return isAllow
}
// setRule adds the condition tuple of relation, scope, and action into token
// rules. The scope could be context or resource. Use the empty relation to
// apply all relations in the condition. Use the empty string as scope to apply
// all scopes in the condition. Use no action to apply all actions in the
// condition.
func (t *LeastPrivilegeToken) setRule(relation Relation, scope any, action []string, result bool) {
var relName = string(relation)
var scopeName = getName(scope)
var actName = strings.Join(action, "_")
t.rules[strings.Join([]string{actName, relName, scopeName}, ".")] = result
}