Debian Codegraph @

pkg/op/mock/storage.mock.impl.go

raw
1 package mock
2
3 import (
4 "context"
5 "errors"
6 "testing"
7 "time"
8
9 "github.com/golang/mock/gomock"
10
11 "github.com/zitadel/oidc/v3/pkg/oidc"
12 "github.com/zitadel/oidc/v3/pkg/op"
13 )
14
15 func NewStorage(t *testing.T) op.Storage {
16 return NewMockStorage(gomock.NewController(t))
17 }
18
19 func NewMockStorageExpectValidClientID(t *testing.T) op.Storage {
20 m := NewStorage(t)
21 ExpectValidClientID(m)
22 return m
23 }
24
25 func NewMockStorageExpectInvalidClientID(t *testing.T) op.Storage {
26 m := NewStorage(t)
27 ExpectInvalidClientID(m)
28 return m
29 }
30
31 func NewMockStorageAny(t *testing.T) op.Storage {
32 m := NewStorage(t)
33 mockS := m.(*MockStorage)
34 mockS.EXPECT().GetClientByClientID(gomock.Any(), gomock.Any()).AnyTimes().Return(&ConfClient{}, nil)
35 mockS.EXPECT().AuthorizeClientIDSecret(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(nil)
36 return m
37 }
38
39 func NewMockStorageSigningKeyInvalid(t *testing.T) op.Storage {
40 m := NewStorage(t)
41 //ExpectSigningKeyInvalid(m)
42 return m
43 }
44
45 func NewMockStorageSigningKey(t *testing.T) op.Storage {
46 m := NewStorage(t)
47 //ExpectSigningKey(m)
48 return m
49 }
50
51 func ExpectInvalidClientID(s op.Storage) {
52 mockS := s.(*MockStorage)
53 mockS.EXPECT().GetClientByClientID(gomock.Any(), gomock.Any()).Return(nil, errors.New("client not found"))
54 }
55
56 func ExpectValidClientID(s op.Storage) {
57 mockS := s.(*MockStorage)
58 mockS.EXPECT().GetClientByClientID(gomock.Any(), gomock.Any()).DoAndReturn(
59 func(_ context.Context, id string) (op.Client, error) {
60 var appType op.ApplicationType
61 var authMethod oidc.AuthMethod
62 var accessTokenType op.AccessTokenType
63 var responseTypes []oidc.ResponseType
64 switch id {
65 case "web_client":
66 appType = op.ApplicationTypeWeb
67 authMethod = oidc.AuthMethodBasic
68 accessTokenType = op.AccessTokenTypeBearer
69 responseTypes = []oidc.ResponseType{oidc.ResponseTypeCode}
70 case "native_client":
71 appType = op.ApplicationTypeNative
72 authMethod = oidc.AuthMethodNone
73 accessTokenType = op.AccessTokenTypeBearer
74 responseTypes = []oidc.ResponseType{oidc.ResponseTypeCode}
75 case "useragent_client":
76 appType = op.ApplicationTypeUserAgent
77 authMethod = oidc.AuthMethodBasic
78 accessTokenType = op.AccessTokenTypeJWT
79 responseTypes = []oidc.ResponseType{oidc.ResponseTypeIDToken}
80 }
81 return &ConfClient{id: id, appType: appType, authMethod: authMethod, accessTokenType: accessTokenType, responseTypes: responseTypes}, nil
82 })
83 }
84
85 type ConfClient struct {
86 id string
87 appType op.ApplicationType
88 authMethod oidc.AuthMethod
89 accessTokenType op.AccessTokenType
90 responseTypes []oidc.ResponseType
91 grantTypes []oidc.GrantType
92 devMode bool
93 }
94
95 func (c *ConfClient) RedirectURIs() []string {
96 return []string{
97 "https://registered.com/callback",
98 "http://registered.com/callback",
99 "http://localhost:9999/callback",
100 "custom://callback",
101 }
102 }
103
104 func (c *ConfClient) PostLogoutRedirectURIs() []string {
105 return []string{}
106 }
107
108 func (c *ConfClient) LoginURL(id string) string {
109 return "login?id=" + id
110 }
111
112 func (c *ConfClient) ApplicationType() op.ApplicationType {
113 return c.appType
114 }
115
116 func (c *ConfClient) AuthMethod() oidc.AuthMethod {
117 return c.authMethod
118 }
119
120 func (c *ConfClient) GetID() string {
121 return c.id
122 }
123
124 func (c *ConfClient) AccessTokenLifetime() time.Duration {
125 return 5 * time.Minute
126 }
127
128 func (c *ConfClient) IDTokenLifetime() time.Duration {
129 return 5 * time.Minute
130 }
131
132 func (c *ConfClient) AccessTokenType() op.AccessTokenType {
133 return c.accessTokenType
134 }
135
136 func (c *ConfClient) ResponseTypes() []oidc.ResponseType {
137 return c.responseTypes
138 }
139
140 func (c *ConfClient) GrantTypes() []oidc.GrantType {
141 return c.grantTypes
142 }
143
144 func (c *ConfClient) DevMode() bool {
145 return c.devMode
146 }
147
148 func (c *ConfClient) AllowedScopes() []string {
149 return nil
150 }
151
152 func (c *ConfClient) RestrictAdditionalIdTokenScopes() func(scopes []string) []string {
153 return func(scopes []string) []string {
154 return scopes
155 }
156 }
157
158 func (c *ConfClient) RestrictAdditionalAccessTokenScopes() func(scopes []string) []string {
159 return func(scopes []string) []string {
160 return scopes
161 }
162 }
163
164 func (c *ConfClient) IsScopeAllowed(scope string) bool {
165 return false
166 }
167
168 func (c *ConfClient) IDTokenUserinfoClaimsAssertion() bool {
169 return false
170 }
171
172 func (c *ConfClient) ClockSkew() time.Duration {
173 return 0
174 }