forked from lestrrat-go/jwx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_ops.go
58 lines (53 loc) · 1.31 KB
/
key_ops.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
package jwk
import "github.com/pkg/errors"
func (ops *KeyOperationList) Get() KeyOperationList {
if ops == nil {
return nil
}
return *ops
}
func (ops *KeyOperationList) Accept(v interface{}) error {
switch x := v.(type) {
case string:
return ops.Accept([]string{x})
case []interface{}:
l := make([]string, len(x))
for i, e := range x {
if es, ok := e.(string); ok {
l[i] = es
} else {
return errors.Errorf(`invalid list element type: expected string, got %T`, v)
}
}
return ops.Accept(l)
case []string:
list := make(KeyOperationList, len(x))
for i, e := range x {
switch e := KeyOperation(e); e {
case KeyOpSign, KeyOpVerify, KeyOpEncrypt, KeyOpDecrypt, KeyOpWrapKey, KeyOpUnwrapKey, KeyOpDeriveKey, KeyOpDeriveBits:
list[i] = e
default:
return errors.Errorf(`invalid keyoperation %v`, e)
}
}
*ops = list
return nil
case []KeyOperation:
list := make(KeyOperationList, len(x))
for i, e := range x {
switch e {
case KeyOpSign, KeyOpVerify, KeyOpEncrypt, KeyOpDecrypt, KeyOpWrapKey, KeyOpUnwrapKey, KeyOpDeriveKey, KeyOpDeriveBits:
list[i] = e
default:
return errors.Errorf(`invalid keyoperation %v`, e)
}
}
*ops = list
return nil
case KeyOperationList:
*ops = x
return nil
default:
return errors.Errorf(`invalid value %T`, v)
}
}