-
-
Notifications
You must be signed in to change notification settings - Fork 964
/
errors.go
381 lines (331 loc) · 11.7 KB
/
errors.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package schema
import (
"fmt"
"github.com/pkg/errors"
"github.com/ory/jsonschema/v3"
"github.com/ory/kratos/text"
)
type ValidationError struct {
*jsonschema.ValidationError
Messages text.Messages
}
func NewRequiredError(missingPtr, missingFieldName string) error {
return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: fmt.Sprintf("missing properties: %s", missingFieldName),
InstancePtr: missingPtr,
Context: &jsonschema.ValidationErrorContextRequired{
Missing: []string{missingFieldName},
},
},
Messages: new(text.Messages).Add(text.NewValidationErrorRequired(missingFieldName)),
})
}
func NewTOTPVerifierWrongError(instancePtr string) error {
t := text.NewErrorValidationTOTPVerifierWrong()
return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: t.Text,
InstancePtr: instancePtr,
},
Messages: new(text.Messages).Add(t),
})
}
func NewWebAuthnVerifierWrongError(instancePtr string) error {
t := text.NewErrorValidationTOTPVerifierWrong()
return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: t.Text,
InstancePtr: instancePtr,
},
Messages: new(text.Messages).Add(t),
})
}
func NewLookupAlreadyUsed() error {
t := text.NewErrorValidationLookupAlreadyUsed()
return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: t.Text,
InstancePtr: "#/",
},
Messages: new(text.Messages).Add(t),
})
}
func NewErrorValidationLookupInvalid() error {
t := text.NewErrorValidationLookupInvalid()
return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: t.Text,
InstancePtr: "#/",
},
Messages: new(text.Messages).Add(t),
})
}
type ValidationErrorContextPasswordPolicyViolation struct {
Reason string
}
func (r *ValidationErrorContextPasswordPolicyViolation) AddContext(_, _ string) {}
func (r *ValidationErrorContextPasswordPolicyViolation) FinishInstanceContext() {}
func NewPasswordPolicyViolationError(instancePtr string, message *text.Message) error {
return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: fmt.Sprintf("the password does not fulfill the password policy because: %s", message.Text),
InstancePtr: instancePtr,
Context: &ValidationErrorContextPasswordPolicyViolation{
Reason: message.Text,
},
},
Messages: new(text.Messages).Add(message),
})
}
func NewMissingIdentifierError() error {
return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: "could not find any identifiers",
InstancePtr: "#/",
},
Messages: new(text.Messages).Add(text.NewErrorValidationIdentifierMissing()),
})
}
type ValidationErrorContextInvalidCredentialsError struct{}
func (r *ValidationErrorContextInvalidCredentialsError) AddContext(_, _ string) {}
func (r *ValidationErrorContextInvalidCredentialsError) FinishInstanceContext() {}
func NewInvalidCredentialsError() error {
return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: `the provided credentials are invalid, check for spelling mistakes in your password or username, email address, or phone number`,
InstancePtr: "#/",
},
Messages: new(text.Messages).Add(text.NewErrorValidationInvalidCredentials()),
})
}
func NewAccountNotFoundError() error {
return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: "this account does not exist or has no login method configured",
InstancePtr: "#/identifier",
},
Messages: new(text.Messages).Add(text.NewErrorValidationAccountNotFound()),
})
}
type ValidationErrorContextDuplicateCredentialsError struct {
AvailableCredentials []string `json:"available_credential_types"`
AvailableOIDCProviders []string `json:"available_oidc_providers"`
IdentifierHint string `json:"credential_identifier_hint"`
}
func (r *ValidationErrorContextDuplicateCredentialsError) AddContext(_, _ string) {}
func (r *ValidationErrorContextDuplicateCredentialsError) FinishInstanceContext() {}
type DuplicateCredentialsHinter interface {
AvailableCredentials() []string
AvailableOIDCProviders() []string
IdentifierHint() string
HasHints() bool
}
func NewDuplicateCredentialsError(err error) error {
if hinter := DuplicateCredentialsHinter(nil); errors.As(err, &hinter) && hinter.HasHints() {
return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: `an account with the same identifier (email, phone, username, ...) exists already`,
InstancePtr: "#/",
Context: &ValidationErrorContextDuplicateCredentialsError{
AvailableCredentials: hinter.AvailableCredentials(),
AvailableOIDCProviders: hinter.AvailableOIDCProviders(),
IdentifierHint: hinter.IdentifierHint(),
},
},
Messages: new(text.Messages).Add(text.NewErrorValidationDuplicateCredentialsWithHints(hinter.AvailableCredentials(), hinter.AvailableOIDCProviders(), hinter.IdentifierHint())),
})
}
return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: `an account with the same identifier (email, phone, username, ...) exists already`,
InstancePtr: "#/",
Context: &ValidationErrorContextDuplicateCredentialsError{},
},
Messages: new(text.Messages).Add(text.NewErrorValidationDuplicateCredentials()),
})
}
func NewNoLoginStrategyResponsible() error {
return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: `could not find a strategy to login with`,
InstancePtr: "#/",
},
Messages: new(text.Messages).Add(text.NewErrorValidationLoginNoStrategyFound()),
})
}
func NewNoRegistrationStrategyResponsible() error {
return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: `could not find a strategy to sign up with`,
InstancePtr: "#/",
},
Messages: new(text.Messages).Add(text.NewErrorValidationRegistrationNoStrategyFound()),
})
}
func NewNoSettingsStrategyResponsible() error {
return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: `could not find a strategy to update settings with`,
InstancePtr: "#/",
},
Messages: new(text.Messages).Add(text.NewErrorValidationSettingsNoStrategyFound()),
})
}
func NewNoRecoveryStrategyResponsible() error {
return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: `could not find a strategy to recover your account with`,
InstancePtr: "#/",
},
Messages: new(text.Messages).Add(text.NewErrorValidationRecoveryNoStrategyFound()),
})
}
func NewNoVerificationStrategyResponsible() error {
return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: `could not find a strategy to verify your account with`,
InstancePtr: "#/",
},
Messages: new(text.Messages).Add(text.NewErrorValidationVerificationNoStrategyFound()),
})
}
func NewAddressNotVerifiedError() error {
return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: `account address not yet verified`,
InstancePtr: "#/",
},
Messages: new(text.Messages).Add(text.NewErrorValidationAddressNotVerified()),
})
}
func NewNoTOTPDeviceRegistered() error {
return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: `you have no TOTP device set up`,
InstancePtr: "#/",
},
Messages: new(text.Messages).Add(text.NewErrorValidationNoTOTPDevice()),
})
}
func NewNoLookupDefined() error {
return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: `you have no backup recovery codes set up`,
InstancePtr: "#/",
},
Messages: new(text.Messages).Add(text.NewErrorValidationNoLookup()),
})
}
func NewNoWebAuthnRegistered() error {
return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: `you have no WebAuthn device set up`,
InstancePtr: "#/",
},
Messages: new(text.Messages).Add(text.NewErrorValidationNoWebAuthnDevice()),
})
}
func NewHookValidationError(instancePtr, message string, messages text.Messages) *ValidationError {
return &ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: message,
InstancePtr: instancePtr,
},
Messages: messages,
}
}
type ValidationListError struct {
Validations []*ValidationError
}
func (e ValidationListError) Error() string {
var detailError string
for pos, validationErr := range e.Validations {
detailError = detailError + fmt.Sprintf("\n(%d) %s", pos, validationErr.Error())
}
return fmt.Sprintf("%d validation errors occurred:%s", len(e.Validations), detailError)
}
func (e *ValidationListError) Add(v *ValidationError) {
e.Validations = append(e.Validations, v)
}
func (e ValidationListError) HasErrors() bool {
return len(e.Validations) > 0
}
func (e *ValidationListError) WithError(instancePtr, message string, details text.Messages) {
e.Validations = append(e.Validations, &ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: message,
InstancePtr: instancePtr,
},
Messages: details,
})
}
func NewValidationListError(errs []*ValidationError) error {
return errors.WithStack(&ValidationListError{Validations: errs})
}
func NewNoWebAuthnCredentials() error {
return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: `account does not exist or has no security key set up`,
InstancePtr: "#/",
},
Messages: new(text.Messages).Add(text.NewErrorValidationSuchNoWebAuthnUser()),
})
}
func NewNoCodeAuthnCredentials() error {
return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: `account does not exist or has not setup up sign in with code`,
InstancePtr: "#/",
},
Messages: new(text.Messages).Add(text.NewErrorValidationNoCodeUser()),
})
}
func NewTraitsMismatch() error {
return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: `the submitted form data has changed from the previous submission`,
InstancePtr: "#/",
},
Messages: new(text.Messages).Add(text.NewErrorValidationTraitsMismatch()),
})
}
func NewRegistrationCodeInvalid() error {
return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: `the provided code is invalid or has already been used`,
InstancePtr: "#/",
},
Messages: new(text.Messages).Add(text.NewErrorValidationRegistrationCodeInvalidOrAlreadyUsed()),
})
}
func NewLoginCodeInvalid() error {
return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: `the provided code is invalid or has already been used`,
InstancePtr: "#/",
},
Messages: new(text.Messages).Add(text.NewErrorValidationLoginCodeInvalidOrAlreadyUsed()),
})
}
func NewLinkedCredentialsDoNotMatch() error {
return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: `linked credentials do not match; please start a new flow`,
InstancePtr: "#/",
},
Messages: new(text.Messages).Add(text.NewErrorValidationLoginLinkedCredentialsDoNotMatch()),
})
}
func NewUnknownAddressError() error {
return errors.WithStack(&ValidationError{
ValidationError: &jsonschema.ValidationError{
Message: `the supplied address does not match any known addresses.`,
InstancePtr: "#/",
},
Messages: new(text.Messages).Add(text.NewErrorValidationAddressUnknown()),
},
)
}