Repository navigation
Expand file tree
/
Copy pathdid_auth_handler_validator.go
More file actions
73 lines (58 loc) · 1.84 KB
/
Copy pathdid_auth_handler_validator.go
File metadata and controls
73 lines (58 loc) · 1.84 KB
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
package validators
import (
"context"
"fmt"
"net/url"
"github.com/NdoleStudio/httpsms/pkg/did"
"github.com/NdoleStudio/httpsms/pkg/requests"
"github.com/NdoleStudio/httpsms/pkg/telemetry"
)
// DIDAuthHandlerValidator validates models used in handlers.DIDAuthHandler
type DIDAuthHandlerValidator struct {
validator
logger telemetry.Logger
tracer telemetry.Tracer
}
// NewDIDAuthHandlerValidator creates a new handlers.DIDAuthHandler validator
func NewDIDAuthHandlerValidator(
logger telemetry.Logger,
tracer telemetry.Tracer,
) (v *DIDAuthHandlerValidator) {
return &DIDAuthHandlerValidator{
logger: logger.WithService(fmt.Sprintf("%T", v)),
tracer: tracer,
}
}
// ValidateChallenge validates requests.DIDChallenge
func (validator *DIDAuthHandlerValidator) ValidateChallenge(_ context.Context, request requests.DIDChallenge) url.Values {
return validator.validateDID(request.DID)
}
// ValidateVerify validates requests.DIDVerify
func (validator *DIDAuthHandlerValidator) ValidateVerify(_ context.Context, request requests.DIDVerify) url.Values {
errors := validator.validateDID(request.DID)
if request.ID == "" {
errors.Add("id", "The id field is required")
}
if request.Nonce == "" {
errors.Add("nonce", "The nonce field is required")
}
if request.Signature == "" {
errors.Add("signature", "The signature field is required")
}
if request.Scheme != "" && request.Scheme != "mldsa" {
errors.Add("scheme", "The scheme field must be \"mldsa\"")
}
return errors
}
// validateDID validates a did:key identifier
func (validator *DIDAuthHandlerValidator) validateDID(value string) url.Values {
errors := url.Values{}
if value == "" {
errors.Add("did", "The did field is required")
return errors
}
if !did.IsValidPQDID(value) {
errors.Add("did", "The did field must be a valid ML-DSA-87 did:key identifier")
}
return errors
}