-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
164 lines (128 loc) · 4.77 KB
/
example_test.go
File metadata and controls
164 lines (128 loc) · 4.77 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2026 Hyperscale. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package security_test
import (
"context"
"errors"
"fmt"
"github.com/hyperscale-stack/security"
)
// userAuth is an example concrete Authentication produced by a fictional
// authenticator. The point of this example is the Engine wiring, so the type
// is kept minimal.
type userAuth struct {
sub string
roles []string
credentials string
verified bool
}
func (u userAuth) Principal() security.Principal { return userPrincipal{sub: u.sub} }
func (u userAuth) Credentials() any { return u.credentials }
func (u userAuth) Authorities() []string { return u.roles }
func (u userAuth) IsAuthenticated() bool { return u.verified }
func (u userAuth) Name() string { return u.sub }
type userPrincipal struct{ sub string }
func (p userPrincipal) Subject() string { return p.sub }
// staticExtractor returns a fixed userAuth when the "X-Demo-User" header is
// set, and (nil, nil) otherwise.
type staticExtractor struct{}
func (staticExtractor) Extract(_ context.Context, c security.Carrier) (security.Authentication, error) {
sub := c.Get("X-Demo-User")
if sub == "" {
return nil, nil
}
return userAuth{sub: sub, credentials: "password"}, nil
}
// staticAuthenticator accepts only "alice" / "password".
type staticAuthenticator struct{}
func (staticAuthenticator) AuthenticatorName() string { return "static" }
func (staticAuthenticator) Supports(_ security.Authentication) bool { return true }
func (staticAuthenticator) Authenticate(_ context.Context, a security.Authentication) (security.Authentication, error) {
u, ok := a.(userAuth)
if !ok {
return a, security.ErrUnsupportedCredential
}
if u.sub != "alice" || u.credentials != "password" {
return a, security.ErrInvalidCredentials
}
u.roles = []string{"ROLE_USER"}
u.verified = true
return u, nil
}
// demoCarrier is a tiny Carrier used to drive the example without depending
// on the http sub-module.
type demoCarrier struct{ headers map[string]string }
func (c *demoCarrier) Get(k string) string { return c.headers[k] }
func (c *demoCarrier) Values(k string) []string { return []string{c.headers[k]} }
func (c *demoCarrier) Set(k, v string) { c.headers[k] = v }
func (c *demoCarrier) Add(k, v string) { c.headers[k] = v }
// roleVoter implements [security.Voter] for the example. It supports any
// attribute string starting with "role:" and grants when the principal has
// the matching role.
type roleVoter struct{}
func (roleVoter) Supports(a security.Attribute) bool {
if a == nil {
return false
}
const prefix = "role:"
if len(a.String()) < len(prefix) {
return false
}
return a.String()[:len(prefix)] == prefix
}
func (roleVoter) Vote(_ context.Context, auth security.Authentication, attrs []security.Attribute) security.Decision {
for _, a := range attrs {
const prefix = "role:"
if len(a.String()) < len(prefix) || a.String()[:len(prefix)] != prefix {
continue
}
want := a.String()[len(prefix):]
for _, r := range auth.Authorities() {
if r == want {
return security.DecisionGrant
}
}
}
return security.DecisionDeny
}
type roleAttr string
func (r roleAttr) String() string { return "role:" + string(r) }
// Example_engine shows the canonical pipeline: extractor -> authenticator
// orchestrated by the Engine, ending with an AccessDecisionManager.
func Example_engine() {
engine := security.NewEngine(
security.NewManager(staticAuthenticator{}),
staticExtractor{},
)
carrier := &demoCarrier{headers: map[string]string{"X-Demo-User": "alice"}}
ctx, auth, err := engine.Process(context.Background(), carrier)
if err != nil {
fmt.Println("auth error:", err)
return
}
fmt.Printf("authenticated=%t subject=%s\n", auth.IsAuthenticated(), auth.Principal().Subject())
adm := security.NewAffirmativeDecisionManager(roleVoter{})
if err := adm.Decide(ctx, auth, []security.Attribute{roleAttr("ROLE_USER")}); err != nil {
fmt.Println("denied:", err)
return
}
fmt.Println("granted")
// Output:
// authenticated=true subject=alice
// granted
}
// ExampleNewManager illustrates first-success-wins semantics.
func ExampleNewManager() {
first := security.AuthenticatorFunc(func(_ context.Context, a security.Authentication) (security.Authentication, error) {
return a, errors.New("first refuses")
})
second := security.AuthenticatorFunc(func(_ context.Context, a security.Authentication) (security.Authentication, error) {
return userAuth{sub: "bob", verified: true}, nil
})
m := security.NewManager(first, second)
auth, err := m.Authenticate(context.Background(), userAuth{sub: "bob"})
fmt.Println(auth.Name(), err)
// Output:
// bob <nil>
}