-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmock_op.go
More file actions
81 lines (72 loc) · 1.9 KB
/
mock_op.go
File metadata and controls
81 lines (72 loc) · 1.9 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
package oidfed
import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/md5"
"crypto/rand"
"fmt"
"time"
"github.com/lestrrat-go/jwx/v3/jwa"
"github.com/go-oidfed/lib/jwx"
"github.com/go-oidfed/lib/unixtime"
)
type mockOP struct {
EntityID string
authorities []string
jwks jwx.JWKS
*jwx.EntityStatementSigner
metadata *OpenIDProviderMetadata
}
func (op mockOP) EntityConfigurationJWT() ([]byte, error) {
return op.EntityStatementSigner.JWT(op.EntityStatementPayload())
}
func newMockOP(entityID string, metadata *OpenIDProviderMetadata) *mockOP {
sk, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
if err != nil {
panic(err)
}
metadata.Issuer = entityID
jwks, err := jwx.KeyToJWKS(sk.Public(), jwa.ES512())
if err != nil {
panic(err)
}
o := &mockOP{
EntityID: entityID,
metadata: metadata,
EntityStatementSigner: jwx.NewEntityStatementSigner(
jwx.NewSingleKeyVersatileSigner(sk, jwa.ES512()),
),
jwks: jwks,
}
mockEntityConfiguration(o.EntityID, o)
return o
}
func (op mockOP) EntityStatementPayload() EntityStatementPayload {
now := time.Now()
orgID := fmt.Sprintf("%x", md5.Sum([]byte(op.EntityID)))
payload := EntityStatementPayload{
Issuer: op.EntityID,
Subject: op.EntityID,
IssuedAt: unixtime.Unixtime{Time: now},
ExpiresAt: unixtime.Unixtime{Time: now.Add(time.Second * time.Duration(mockStmtLifetime))},
JWKS: op.jwks,
Audience: "",
AuthorityHints: op.authorities,
Metadata: &Metadata{
FederationEntity: &FederationEntityMetadata{
OrganizationName: fmt.Sprintf("Organization: %s", orgID[:8]),
},
OpenIDProvider: op.metadata,
},
}
return payload
}
func (op mockOP) GetSubordinateInfo() mockSubordinateInfo {
return mockSubordinateInfo{
entityID: op.EntityID,
jwks: op.jwks,
}
}
func (op *mockOP) AddAuthority(authorityID string) {
op.authorities = append(op.authorities, authorityID)
}