-
-
Notifications
You must be signed in to change notification settings - Fork 931
/
validator.go
66 lines (54 loc) · 1.51 KB
/
validator.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
// Copyright © 2022 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package identity
import (
"context"
"github.com/tidwall/sjson"
"github.com/ory/kratos/driver/config"
"github.com/ory/kratos/schema"
"github.com/ory/x/otelx"
)
type (
validatorDependencies interface {
IdentityTraitsSchemas(ctx context.Context) (schema.Schemas, error)
config.Provider
}
Validator struct {
v *schema.Validator
d validatorDependencies
}
ValidationProvider interface {
IdentityValidator() *Validator
}
)
func NewValidator(d validatorDependencies) *Validator {
return &Validator{v: schema.NewValidator(), d: d}
}
func (v *Validator) ValidateWithRunner(ctx context.Context, i *Identity, runners ...schema.Extension) error {
runner, err := schema.NewExtensionRunner(ctx, runners...)
if err != nil {
return err
}
ss, err := v.d.IdentityTraitsSchemas(ctx)
if err != nil {
return err
}
s, err := ss.GetByID(i.SchemaID)
if err != nil {
return err
}
traits, err := sjson.SetRawBytes([]byte(`{}`), "traits", i.Traits)
if err != nil {
return err
}
return v.v.Validate(ctx, s.URL.String(), traits, schema.WithExtensionRunner(runner))
}
func (v *Validator) Validate(ctx context.Context, i *Identity) error {
return otelx.WithSpan(ctx, "identity.Validator.Validate", func(ctx context.Context) error {
return v.ValidateWithRunner(ctx, i,
NewSchemaExtensionCredentials(i),
NewSchemaExtensionVerification(i, v.d.Config().SelfServiceFlowVerificationRequestLifespan(ctx)),
NewSchemaExtensionRecovery(i),
)
})
}