-
Notifications
You must be signed in to change notification settings - Fork 8
/
bu_jwt_factory.go
60 lines (50 loc) · 1.55 KB
/
bu_jwt_factory.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
package business_unit
import (
"crypto/ecdsa"
"crypto/rsa"
"fmt"
"github.com/openebl/openebl/pkg/bu_server/model"
"github.com/openebl/openebl/pkg/pkix"
)
type JWTFactory interface {
NewJWSSigner(authentication model.BusinessUnitAuthentication) (JWSSigner, error)
NewJWEEncryptor(authentication model.BusinessUnitAuthentication) (JWEEncryptor, error)
}
type _JWTFactory struct{}
var DefaultJWTFactory = _JWTFactory{}
func (_JWTFactory) NewJWSSigner(authentication model.BusinessUnitAuthentication) (JWSSigner, error) {
privateKey, err := pkix.ParsePrivateKey([]byte(authentication.PrivateKey))
if err != nil {
return nil, err
}
certs, err := pkix.ParseCertificate([]byte(authentication.Certificate))
if err != nil {
return nil, err
}
switch pk := privateKey.(type) {
case *rsa.PrivateKey:
return &RSASigner{key: pk, cert: certs}, nil
case *ecdsa.PrivateKey:
return &ECDSASigner{key: pk, cert: certs}, nil
default:
return nil, fmt.Errorf("invalid private key type for JWS Signer")
}
}
func (_JWTFactory) NewJWEEncryptor(authentication model.BusinessUnitAuthentication) (JWEEncryptor, error) {
certs, err := pkix.ParseCertificate([]byte(authentication.Certificate))
if err != nil {
return nil, err
}
if len(certs) == 0 {
return nil, fmt.Errorf("no certificate found")
}
publicKey := certs[0].PublicKey
switch pk := publicKey.(type) {
case *rsa.PublicKey:
return &RSAEncryptor{key: pk}, nil
case *ecdsa.PublicKey:
return &ECDSAEncryptor{key: pk}, nil
default:
return nil, fmt.Errorf("invalid public key type for JWE Encryptor")
}
}