-
-
Notifications
You must be signed in to change notification settings - Fork 963
/
extension_recovery.go
65 lines (51 loc) · 1.35 KB
/
extension_recovery.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
package identity
import (
"fmt"
"sync"
"github.com/ory/jsonschema/v3"
"github.com/ory/kratos/schema"
)
type SchemaExtensionRecovery struct {
l sync.Mutex
v []RecoveryAddress
i *Identity
}
func NewSchemaExtensionRecovery(i *Identity) *SchemaExtensionRecovery {
return &SchemaExtensionRecovery{i: i}
}
func (r *SchemaExtensionRecovery) Run(ctx jsonschema.ValidationContext, s schema.ExtensionConfig, value interface{}) error {
r.l.Lock()
defer r.l.Unlock()
switch s.Recovery.Via {
case "email":
if !jsonschema.Formats["email"](value) {
return ctx.Error("format", "%q is not valid %q", value, "email")
}
address := NewRecoveryEmailAddress(fmt.Sprintf("%s", value), r.i.ID)
if has := r.has(r.i.RecoveryAddresses, address); has != nil {
if r.has(r.v, address) == nil {
r.v = append(r.v, *has)
}
return nil
}
if has := r.has(r.v, address); has == nil {
r.v = append(r.v, *address)
}
return nil
case "":
return nil
}
return ctx.Error("", "recovery.via has unknown value %q", s.Recovery.Via)
}
func (r *SchemaExtensionRecovery) has(haystack []RecoveryAddress, needle *RecoveryAddress) *RecoveryAddress {
for _, has := range haystack {
if has.Value == needle.Value && has.Via == needle.Via {
return &has
}
}
return nil
}
func (r *SchemaExtensionRecovery) Finish() error {
r.i.RecoveryAddresses = r.v
return nil
}