-
Notifications
You must be signed in to change notification settings - Fork 0
/
statement.go
89 lines (71 loc) · 1.84 KB
/
statement.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
package leanauthz
import (
"crypto/rand"
"encoding/base64"
"github.com/lazharichir/leanauthz/matching"
)
func NewAllowStatement(principal, action, resource Pattern) Statement {
return NewStatement(ALLOW, principal, action, resource)
}
func NewDenyStatement(principal, action, resource Pattern) Statement {
return NewStatement(DENY, principal, action, resource)
}
func generateStatementID() (string, error) {
// Generate 16 random bytes
b := make([]byte, 22)
_, err := rand.Read(b)
if err != nil {
return "", err
}
// Encode the bytes as a base64 string
id := base64.URLEncoding.EncodeToString(b)
return id, nil
}
func NewStatement(effect Effect, principal, action, resource Pattern) Statement {
id, err := generateStatementID()
if err != nil {
panic(err)
}
return Statement{
ID: id,
Effect: effect,
Principal: principal,
Resource: resource,
Action: action,
}
}
type Statement struct {
ID string
Effect Effect
Principal Pattern
Resource Pattern
Action Pattern
}
func (s *Statement) SetEffect(e Effect) *Statement {
s.Effect = e
return s
}
func (s *Statement) SetPrincipal(p Pattern) *Statement {
s.Principal = p
return s
}
func (s *Statement) SetResource(r Pattern) *Statement {
s.Resource = r
return s
}
func (s *Statement) SetAction(a Pattern) *Statement {
s.Action = a
return s
}
func (s *Statement) MatchesPrincipal(p Principal) bool {
return matching.Match(string(s.Principal), string(p))
}
func (s *Statement) MatchesResource(r Resource) bool {
return matching.Match(s.Resource.String(), string(r))
}
func (s *Statement) MatchesAction(a Action) bool {
return matching.Match(s.Action.String(), string(a))
}
func (s *Statement) MatchesEvaluation(eval Evaluation) bool {
return s.MatchesPrincipal(eval.Principal) && s.MatchesResource(eval.Resource) && s.MatchesAction(eval.Action)
}