-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.go
48 lines (39 loc) · 1.11 KB
/
validation.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
package pkce
// validateCodeVerifier ensures that the provided code verifier is specification
// compliant.
func validateCodeVerifier(verifier []byte) error {
if err := validateVerifierLen(len(verifier)); err != nil {
return err
}
return validateCodeVerifierCharacters(verifier)
}
// validateVerifierLen ensures the length of the code verifier is within the
// bounds of the specification's declared lengths.
func validateVerifierLen(n int) error {
if n < verifierMinLen || n > verifierMaxLen {
return ErrVerifierLength
}
return nil
}
// validateCodeVerifier ensures all characters provided are in the set of
// unreserved characters.
func validateCodeVerifierCharacters(chars []byte) error {
for _, char := range chars {
if !validVerifierChar(char) {
return ErrVerifierCharacters
}
}
return nil
}
// validVerifierChar ensures that any bytes provided are specifically from the
// unreserved character list.
func validVerifierChar(c byte) bool {
if 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || '0' <= c && c <= '9' {
return true
}
switch c {
case '-', '.', '_', '~':
return true
}
return false
}