forked from oauth2-proxy/mockoidc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
140 lines (124 loc) · 4.28 KB
/
user.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
package mockoidc
import (
"encoding/json"
"github.com/golang-jwt/jwt/v5"
)
// User represents a mock user that the server will grant Oauth tokens for.
// Calls to the `authorization_endpoint` will pop any mock Users added to the
// `UserQueue`. Otherwise `DefaultUser()` is returned.
type User interface {
// Unique ID for the User. This will be the Subject claim
ID() string
// Userinfo returns the Userinfo JSON representation of a User with data
// appropriate for the passed scope []string.
Userinfo([]string) ([]byte, error)
// Claims returns the ID Token Claims for a User with data appropriate for
// the passed scope []string. It builds off the passed BaseIDTokenClaims.
Claims([]string, *IDTokenClaims) (jwt.Claims, error)
}
// MockUser is a default implementation of the User interface
type MockUser struct {
Subject string
Email string
EmailVerified bool
PreferredUsername string
Phone string
Address string
Groups []string
CloudRoles []string
Tenant map[string]string
UserUuid string
SubType string
}
// DefaultUser returns a default MockUser that is set in
// `authorization_endpoint` if the UserQueue is empty.
func DefaultUser() *MockUser {
return &MockUser{
Subject: "1234567890",
Email: "[email protected]",
PreferredUsername: "jane.doe",
Phone: "555-987-6543",
Address: "123 Main Street",
Groups: []string{"engineering", "design"},
EmailVerified: true,
CloudRoles: []string{"b199ac60-ef4a-11ee-84e9-506b8dba89b0"},
UserUuid: "e3911826-94aa-4c00-8d99-c2a2e84e94ca",
SubType: "saml",
Tenant: map[string]string{
"uuid": "974f10fd-ac7a-498c-93a4-e2019a4f2e76",
"name": "xyzTenant"},
}
}
type mockUserinfo struct {
Email string `json:"email,omitempty"`
PreferredUsername string `json:"preferred_username,omitempty"`
Phone string `json:"phone_number,omitempty"`
Address string `json:"address,omitempty"`
Groups []string `json:"groups,omitempty"`
}
func (u *MockUser) ID() string {
return u.Subject
}
func (u *MockUser) Userinfo(scope []string) ([]byte, error) {
user := u.scopedClone(scope)
info := &mockUserinfo{
Email: user.Email,
PreferredUsername: user.PreferredUsername,
Phone: user.Phone,
Address: user.Address,
Groups: user.Groups,
}
return json.Marshal(info)
}
type mockClaims struct {
*IDTokenClaims
Email string `json:"email,omitempty"`
EmailVerified bool `json:"email_verified,omitempty"`
PreferredUsername string `json:"preferred_username,omitempty"`
Phone string `json:"phone_number,omitempty"`
Address string `json:"address,omitempty"`
Groups []string `json:"groups,omitempty"`
CloudRoles []string `json:"cloud_roles,omitempty"`
Tenant map[string]string `json:"tenant,omitempty"`
UserUuid string `json:"user_uuid,omitempty"`
SubType string `json:"sub_type,omitempty"`
}
func (u *MockUser) Claims(scope []string, claims *IDTokenClaims) (jwt.Claims, error) {
user := u.scopedClone(scope)
return &mockClaims{
IDTokenClaims: claims,
Email: user.Email,
EmailVerified: user.EmailVerified,
PreferredUsername: user.PreferredUsername,
Phone: user.Phone,
Address: user.Address,
Groups: user.Groups,
CloudRoles: user.CloudRoles,
Tenant: user.Tenant,
UserUuid: user.UserUuid,
SubType: user.SubType,
}, nil
}
func (u *MockUser) scopedClone(scopes []string) *MockUser {
clone := &MockUser{
Subject: u.Subject,
}
clone.CloudRoles = u.CloudRoles
clone.Tenant = u.Tenant
clone.UserUuid = u.UserUuid
clone.SubType = u.SubType
for _, scope := range scopes {
switch scope {
case "profile":
clone.PreferredUsername = u.PreferredUsername
clone.Address = u.Address
clone.Phone = u.Phone
case "email":
clone.Email = u.Email
clone.EmailVerified = u.EmailVerified
case "groups":
clone.Groups = append(make([]string, 0, len(u.Groups)), u.Groups...)
}
}
return clone
}