...
1
2
3 package jwa
4
5 import (
6 "fmt"
7 "sort"
8 "sync"
9
10 "github.com/pkg/errors"
11 )
12
13
14 type KeyType string
15
16
17 const (
18 EC KeyType = "EC"
19 InvalidKeyType KeyType = ""
20 OKP KeyType = "OKP"
21 OctetSeq KeyType = "oct"
22 RSA KeyType = "RSA"
23 )
24
25 var allKeyTypes = map[KeyType]struct{}{
26 EC: {},
27 OKP: {},
28 OctetSeq: {},
29 RSA: {},
30 }
31
32 var listKeyTypeOnce sync.Once
33 var listKeyType []KeyType
34
35
36 func KeyTypes() []KeyType {
37 listKeyTypeOnce.Do(func() {
38 listKeyType = make([]KeyType, 0, len(allKeyTypes))
39 for v := range allKeyTypes {
40 listKeyType = append(listKeyType, v)
41 }
42 sort.Slice(listKeyType, func(i, j int) bool {
43 return string(listKeyType[i]) < string(listKeyType[j])
44 })
45 })
46 return listKeyType
47 }
48
49
50
51 func (v *KeyType) Accept(value interface{}) error {
52 var tmp KeyType
53 if x, ok := value.(KeyType); ok {
54 tmp = x
55 } else {
56 var s string
57 switch x := value.(type) {
58 case fmt.Stringer:
59 s = x.String()
60 case string:
61 s = x
62 default:
63 return errors.Errorf(`invalid type for jwa.KeyType: %T`, value)
64 }
65 tmp = KeyType(s)
66 }
67 if _, ok := allKeyTypes[tmp]; !ok {
68 return errors.Errorf(`invalid jwa.KeyType value`)
69 }
70
71 *v = tmp
72 return nil
73 }
74
75
76 func (v KeyType) String() string {
77 return string(v)
78 }
79
View as plain text