forked from oauth2-proxy/mockoidc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
118 lines (102 loc) · 3.34 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
package mockoidc
import (
"encoding/json"
"github.com/golang-jwt/jwt"
)
// 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
}
// DefaultUser returns a default MockUser that is set in
// `authorization_endpoint` if the UserQueue is empty.
func DefaultUser() *MockUser {
return &MockUser{
Subject: "1234567890",
Email: "jane.doe@example.com",
PreferredUsername: "jane.doe",
Phone: "555-987-6543",
Address: "123 Main Street",
Groups: []string{"engineering", "design"},
EmailVerified: true,
}
}
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"`
}
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,
}, nil
}
func (u *MockUser) scopedClone(scopes []string) *MockUser {
clone := &MockUser{
Subject: u.Subject,
}
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
}