/
attributes.go
165 lines (140 loc) · 3.68 KB
/
attributes.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
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
165
// Package session implements session attributes for stream connections.
package session
import (
"context"
"crypto/subtle"
"fmt"
"strings"
"golang.org/x/crypto/blake2b"
"google.golang.org/grpc/metadata"
)
type (
sessionAttributesKeyType string
)
const (
AttributeMetadataKey = "x-session-attribute"
AttributeLabelKey = "opni.io/session-attribute"
KeyLength = 32
)
const (
AttributesKey sessionAttributesKeyType = "session_attributes"
)
func (k sessionAttributesKeyType) GetRequestMetadata(ctx context.Context, _ ...string) (map[string]string, error) {
if v := ctx.Value(k); v != nil {
var names []string
switch v := v.(type) {
case []Attribute:
names = attributeNames(v)
case []SecretAttribute:
names = attributeNames(v)
default:
return nil, fmt.Errorf("invalid session attribute type %T", v)
}
return map[string]string{
string(k): strings.Join(names, " "),
}, nil
}
return nil, nil
}
func (k sessionAttributesKeyType) RequireTransportSecurity() bool {
return true
}
func (k sessionAttributesKeyType) FromIncomingCredentials(ctx context.Context) context.Context {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return ctx
}
if values := md.Get(string(k)); len(values) == 1 {
names := strings.Split(values[0], " ")
attrs := make([]Attribute, len(names))
for i, name := range names {
attrs[i] = &attribute{name: name}
}
ctx = context.WithValue(ctx, k, attrs)
}
return ctx
}
func StreamAuthorizedAttributes(ctx context.Context) []Attribute {
attrs := ctx.Value(AttributesKey)
if attrs == nil {
return nil
}
switch attrs := attrs.(type) {
case []Attribute:
return attrs
case []SecretAttribute:
return toAttributeSlice(attrs)
default:
return nil
}
}
type Attribute interface {
// Name returns the attribute name. This should be a unique identifier.
Name() string
}
type SecretAttribute interface {
Attribute
// Given a challenge string, Solve will compute a MAC using an
// implementation-specific secret key and return the result.
Solve(id string, challenge []byte) []byte
// Verify will check if a mac is valid for a given id and challenge.
Verify(id string, challenge []byte, response []byte) bool
}
func toAttributeSlice[T Attribute](attrs []T) []Attribute {
result := make([]Attribute, len(attrs))
for i, attr := range attrs {
result[i] = attr
}
return result
}
func attributeNames[T Attribute](attrs []T) []string {
names := make([]string, len(attrs))
for i, attr := range attrs {
names[i] = attr.Name()
}
return names
}
type attribute struct {
name string
}
type secretAttribute struct {
attribute
key [KeyLength]byte
}
var _ Attribute = (*attribute)(nil)
var _ SecretAttribute = (*secretAttribute)(nil)
// Name implements Attribute
func (a *attribute) Name() string {
return a.name
}
// Solve implements Attribute
func (a *secretAttribute) Solve(id string, challenge []byte) []byte {
mac, _ := blake2b.New512(a.key[:])
mac.Write([]byte(a.name))
mac.Write([]byte(id))
mac.Write(challenge[:])
return mac.Sum(nil)
}
// Verify implements Attribute
func (a *secretAttribute) Verify(id string, challenge []byte, response []byte) bool {
expectedMac, _ := blake2b.New512(a.key[:])
expectedMac.Write([]byte(a.name))
expectedMac.Write([]byte(id))
expectedMac.Write(challenge[:])
expected := expectedMac.Sum(nil)
return subtle.ConstantTimeCompare(expected, response) == 1
}
func NewSecretAttribute(name string, key []byte) (SecretAttribute, error) {
if len(key) != KeyLength {
return nil, fmt.Errorf("invalid key length: %d", len(key))
}
return &secretAttribute{
attribute: attribute{
name: name,
},
key: [KeyLength]byte(key),
}, nil
}
func NewAttribute(name string) Attribute {
return &attribute{name: name}
}