-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecryptopts.go
68 lines (52 loc) · 1.21 KB
/
decryptopts.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
// SPDX-FileCopyrightText: 2024 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0
package securly
import (
"fmt"
"github.com/lestrrat-go/jwx/v2/jwk"
)
// EncodeOption is a functional option for the Instructions constructor.
type DecryptOption interface {
apply(*decrypter) error
}
func errorDecrypt(err error) DecryptOption {
return errorDecryptOption{
err: err,
}
}
type errorDecryptOption struct {
err error
}
func (e errorDecryptOption) apply(d *decrypter) error {
return e.err
}
func DecryptWith(key jwk.Key) DecryptOption {
return decryptWithOption{
key: key,
}
}
func DecryptWithRaw(raw any) DecryptOption {
key, err := jwk.FromRaw(raw)
if err != nil {
return errorDecrypt(err)
}
return DecryptWith(key)
}
type decryptWithOption struct {
key jwk.Key
}
func (k decryptWithOption) apply(dec *decrypter) error {
dec.key = k.key
return nil
}
//------------------------------------------------------------------------------
func validateDecrypt() DecryptOption {
return validateDecryptOption{}
}
type validateDecryptOption struct{}
func (v validateDecryptOption) apply(d *decrypter) error {
if d.key == nil {
return fmt.Errorf("key is required")
}
return nil
}