-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathentitystatement.go
More file actions
259 lines (233 loc) · 8.22 KB
/
entitystatement.go
File metadata and controls
259 lines (233 loc) · 8.22 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package oidfed
import (
"encoding/json"
"time"
"github.com/pkg/errors"
"github.com/vmihailenco/msgpack/v5"
"github.com/zachmann/go-utils/structutils"
"gopkg.in/yaml.v3"
"github.com/go-oidfed/lib/internal"
jwxi "github.com/go-oidfed/lib/internal/jwx"
"github.com/go-oidfed/lib/jwx"
"github.com/go-oidfed/lib/oidfedconst"
"github.com/go-oidfed/lib/unixtime"
)
const defaultEntityConfigurationLifetime = time.Hour * 24 // 1d
// EntityStatement is a type for holding an entity statement, more precisely an entity statement that was obtained
// as a jwt and created by us
type EntityStatement struct {
jwtMsg *jwxi.ParsedJWT
EntityStatementPayload
}
// Verify verifies that the EntityStatement jwt is valid
func (e EntityStatement) Verify(keys jwx.JWKS) bool {
_, err := e.jwtMsg.VerifyWithSet(keys)
if err != nil {
internal.Log("EntityStatement: ", err)
}
return err == nil
}
type entityStatementExported struct {
Payload EntityStatementPayload
JWTMsg jwxi.ParsedJWT
}
// MarshalMsgpack implements the msgpack.Marshaler interface for usage with caching
func (e EntityStatement) MarshalMsgpack() ([]byte, error) {
ee := entityStatementExported{
Payload: e.EntityStatementPayload,
}
if e.jwtMsg != nil {
ee.JWTMsg = *e.jwtMsg
}
data, err := msgpack.Marshal(ee)
return data, err
}
// UnmarshalMsgpack implements the msgpack.Unmarshaler interface for usage with caching
func (e *EntityStatement) UnmarshalMsgpack(data []byte) error {
var ee entityStatementExported
if err := msgpack.Unmarshal(data, &ee); err != nil {
return err
}
e.EntityStatementPayload = ee.Payload
e.jwtMsg = &ee.JWTMsg
return nil
}
// EntityStatementPayload is a type for holding the actual payload of an EntityStatement or EntityConfiguration;
// additional fields can be set in the Extra claim
type EntityStatementPayload struct {
Issuer string `json:"iss"`
Subject string `json:"sub"`
IssuedAt unixtime.Unixtime `json:"iat"`
ExpiresAt unixtime.Unixtime `json:"exp"`
JWKS jwx.JWKS `json:"jwks"`
Audience string `json:"aud,omitempty"`
AuthorityHints []string `json:"authority_hints,omitempty"`
TrustAnchorHints []string `json:"trust_anchor_hints,omitempty"`
Metadata *Metadata `json:"metadata,omitempty"`
MetadataPolicy *MetadataPolicies `json:"metadata_policy,omitempty"`
Constraints *ConstraintSpecification `json:"constraints,omitempty"`
CriticalExtensions []string `json:"crit,omitempty"`
MetadataPolicyCrit []PolicyOperatorName `json:"metadata_policy_crit,omitempty"`
TrustMarks TrustMarkInfos `json:"trust_marks,omitempty"`
TrustMarkIssuers AllowedTrustMarkIssuers `json:"trust_mark_issuers,omitempty"`
TrustMarkOwners TrustMarkOwners `json:"trust_mark_owners,omitempty"`
SourceEndpoint string `json:"source_endpoint,omitempty"`
TrustAnchor string `json:"trust_anchor,omitempty"`
Extra map[string]interface{} `json:"-"`
}
// TimeValid checks if the EntityStatementPayload is already valid and not yet expired.
func (e EntityStatementPayload) TimeValid() bool {
return unixtime.VerifyTime(&e.IssuedAt, &e.ExpiresAt) == nil
}
func extraMarshalHelper(explicitFields []byte, extra map[string]interface{}) ([]byte, error) {
var m map[string]json.RawMessage
if err := json.Unmarshal(explicitFields, &m); err != nil {
return nil, err
}
for k, v := range extra {
e, err := json.Marshal(v)
if err != nil {
return nil, err
}
m[k] = e
}
data, err := json.Marshal(m)
return data, errors.WithStack(err)
}
func yamlExtraMarshalHelper(explicitFields []byte, extra map[string]interface{}) (any, error) {
var m map[string]*yaml.Node
if err := yaml.Unmarshal(explicitFields, &m); err != nil {
return nil, err
}
for k, v := range extra {
node := &yaml.Node{}
if err := node.Encode(v); err != nil {
return nil, errors.WithStack(err)
}
m[k] = node
}
return m, nil
}
// MarshalJSON implements the json.Marshaler interface.
// It also marshals extra fields.
func (e EntityStatementPayload) MarshalJSON() ([]byte, error) {
type entityStatement EntityStatementPayload
explicitFields, err := json.Marshal(entityStatement(e))
if err != nil {
return nil, errors.WithStack(err)
}
return extraMarshalHelper(explicitFields, e.Extra)
}
func unmarshalWithExtra(data []byte, target interface{}) (map[string]interface{}, error) {
if err := json.Unmarshal(data, target); err != nil {
return nil, errors.WithStack(err)
}
extra := make(map[string]interface{})
if err := json.Unmarshal(data, &extra); err != nil {
return nil, errors.WithStack(err)
}
for _, tag := range structutils.FieldTagNames(target, "json") {
delete(extra, tag)
}
if len(extra) == 0 {
extra = nil
}
return extra, nil
}
func yamlUnmarshalWithExtra(data *yaml.Node, target interface{}) (map[string]interface{}, error) {
if err := data.Decode(target); err != nil {
return nil, errors.WithStack(err)
}
extra := make(map[string]interface{})
if err := data.Decode(&extra); err != nil {
return nil, errors.WithStack(err)
}
for _, tag := range structutils.FieldTagNames(target, "yaml") {
delete(extra, tag)
}
if len(extra) == 0 {
extra = nil
}
return extra, nil
}
// UnmarshalJSON implements the json.Unmarshaler interface.
// It also unmarshalls additional fields into the Extra claim.
func (e *EntityStatementPayload) UnmarshalJSON(data []byte) error {
type Alias EntityStatementPayload
ee := Alias(*e)
extra, err := unmarshalWithExtra(data, &ee)
if err != nil {
return err
}
ee.Extra = extra
*e = EntityStatementPayload(ee)
return nil
}
// UnmarshalMsgpack implements the msgpack.Unmarshaler interface.
func (e *EntityStatementPayload) UnmarshalMsgpack(data []byte) error {
type entityStatement EntityStatementPayload
ee := entityStatement(*e)
if err := msgpack.Unmarshal(data, &ee); err != nil {
return err
}
*e = EntityStatementPayload(ee)
return nil
}
// ConstraintSpecification is type for holding constraints according to the oidc fed spec
type ConstraintSpecification struct {
MaxPathLength *int `json:"max_path_length,omitempty"`
NamingConstraints *NamingConstraints `json:"naming_constraints,omitempty"`
AllowedEntityTypes []string `json:"allowed_entity_types,omitempty"`
}
// NamingConstraints is a type for holding constraints about naming
type NamingConstraints struct {
Permitted []string `json:"permitted,omitempty"`
Excluded []string `json:"excluded,omitempty"`
}
// AllowedTrustMarkIssuers is type for defining which TrustMark can be issued by which entities
type AllowedTrustMarkIssuers map[string][]string
// TrustMarkOwners defines owners for TrustMarks
type TrustMarkOwners map[string]TrustMarkOwnerSpec
// TrustMarkOwnerSpec describes the owner of a trust mark
type TrustMarkOwnerSpec struct {
ID string `json:"sub" yaml:"entity_id"`
JWKS jwx.JWKS `json:"jwks" yaml:"jwks"`
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (tmo *TrustMarkOwnerSpec) UnmarshalJSON(data []byte) error {
type trustMarkOwner TrustMarkOwnerSpec
o := trustMarkOwner(*tmo)
if err := json.Unmarshal(data, &o); err != nil {
return err
}
*tmo = TrustMarkOwnerSpec(o)
return nil
}
// UnmarshalMsgpack implements the msgpack.Unmarshaler interface.
func (tmo *TrustMarkOwnerSpec) UnmarshalMsgpack(data []byte) error {
type trustMarkOwner TrustMarkOwnerSpec
o := trustMarkOwner(*tmo)
if err := msgpack.Unmarshal(data, &o); err != nil {
return err
}
*tmo = TrustMarkOwnerSpec(o)
return nil
}
// ParseEntityStatement parses a jwt into an EntityStatement
func ParseEntityStatement(statementJWT []byte) (*EntityStatement, error) {
m, err := jwxi.Parse(statementJWT)
if err != nil {
return nil, err
}
if !m.VerifyType(oidfedconst.JWTTypeEntityStatement) {
return nil, errors.Errorf("entity statement does not have '%s' JWT type", oidfedconst.JWTTypeEntityStatement)
}
statement := &EntityStatement{
jwtMsg: m,
EntityStatementPayload: EntityStatementPayload{},
}
if err = json.Unmarshal(m.Payload(), &statement.EntityStatementPayload); err != nil {
return nil, err
}
return statement, err
}