/
extension.go
163 lines (136 loc) · 3.8 KB
/
extension.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
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package schema
import (
"bytes"
"context"
"encoding/json"
"github.com/pkg/errors"
"github.com/ory/jsonschema/v3"
"github.com/ory/kratos/embedx"
)
const (
extensionName string = "ory.sh/kratos"
)
type (
ExtensionConfig struct {
Credentials struct {
Password struct {
Identifier bool `json:"identifier"`
} `json:"password"`
WebAuthn struct {
Identifier bool `json:"identifier"`
} `json:"webauthn"`
TOTP struct {
AccountName bool `json:"account_name"`
} `json:"totp"`
Code struct {
Identifier bool `json:"identifier"`
Via string `json:"via"`
} `json:"code"`
} `json:"credentials"`
Verification struct {
Via string `json:"via"`
} `json:"verification"`
Recovery struct {
Via string `json:"via"`
} `json:"recovery"`
RawSchema map[string]interface{} `json:"-"`
}
ValidateExtension interface {
Run(ctx jsonschema.ValidationContext, config ExtensionConfig, value interface{}) error
Finish() error
}
CompileExtension interface {
Run(ctx jsonschema.CompilerContext, config ExtensionConfig, rawSchema map[string]interface{}) error
}
ExtensionRunner struct {
meta *jsonschema.Schema
compile func(ctx jsonschema.CompilerContext, rawSchema map[string]interface{}) (interface{}, error)
validate func(ctx jsonschema.ValidationContext, s interface{}, v interface{}) error
validateRunners []ValidateExtension
compileRunners []CompileExtension
}
ExtensionRunnerOption func(*ExtensionRunner)
)
func WithValidateRunners(runners ...ValidateExtension) ExtensionRunnerOption {
return func(r *ExtensionRunner) {
r.validateRunners = append(r.validateRunners, runners...)
}
}
func WithCompileRunners(runners ...CompileExtension) ExtensionRunnerOption {
return func(r *ExtensionRunner) {
r.compileRunners = append(r.compileRunners, runners...)
}
}
func NewExtensionRunner(ctx context.Context, opts ...ExtensionRunnerOption) (*ExtensionRunner, error) {
var err error
r := new(ExtensionRunner)
c := jsonschema.NewCompiler()
if err = embedx.AddSchemaResources(c, embedx.IdentityExtension); err != nil {
return nil, errors.WithStack(err)
}
r.meta, err = c.Compile(ctx, embedx.IdentityExtension.GetSchemaID())
if err != nil {
return nil, errors.WithStack(err)
}
r.compile = func(ctx jsonschema.CompilerContext, m map[string]interface{}) (interface{}, error) {
if raw, ok := m[extensionName]; ok {
var b bytes.Buffer
if err := json.NewEncoder(&b).Encode(raw); err != nil {
return nil, errors.WithStack(err)
}
var e ExtensionConfig
if err := json.NewDecoder(&b).Decode(&e); err != nil {
return nil, errors.WithStack(err)
}
for _, runner := range r.compileRunners {
if err := runner.Run(ctx, e, m); err != nil {
return nil, err
}
}
e.RawSchema = m
return &e, nil
}
return nil, nil
}
r.validate = func(ctx jsonschema.ValidationContext, s interface{}, v interface{}) error {
c, ok := s.(*ExtensionConfig)
if !ok {
return nil
}
for _, runner := range r.validateRunners {
if err := runner.Run(ctx, *c, v); err != nil {
return err
}
}
return nil
}
for _, opt := range opts {
opt(r)
}
return r, nil
}
func (r *ExtensionRunner) Register(compiler *jsonschema.Compiler) *ExtensionRunner {
compiler.Extensions[extensionName] = r.Extension()
return r
}
func (r *ExtensionRunner) Extension() jsonschema.Extension {
return jsonschema.Extension{
Meta: r.meta,
Compile: r.compile,
Validate: r.validate,
}
}
func (r *ExtensionRunner) AddRunner(run ValidateExtension) *ExtensionRunner {
r.validateRunners = append(r.validateRunners, run)
return r
}
func (r *ExtensionRunner) Finish() error {
for _, runner := range r.validateRunners {
if err := runner.Finish(); err != nil {
return err
}
}
return nil
}