-
Notifications
You must be signed in to change notification settings - Fork 1
/
manager.go
186 lines (153 loc) · 3.88 KB
/
manager.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package globalsign
import (
"context"
"encoding/base64"
"encoding/hex"
"errors"
"log"
"strings"
"sync"
"time"
)
const (
authTokenTTL = 30 * time.Minute
identityTTL = 10 * time.Minute
)
// ManagerOption .
type ManagerOption struct {
BaseURL string
APIKey string
APISecret string
TLSCertificatePath string
PrivateKeyPath string
InsecureSkipVerify bool
}
// Valid determine whether option is valid
func (o *ManagerOption) Valid() bool {
return o.APIKey != "" && o.APISecret != "" && o.TLSCertificatePath != "" && o.PrivateKeyPath != ""
}
// Manager .
type Manager struct {
sync.RWMutex
apiKey string
apiSecret string
token string
tokenTs time.Time
client *Client
vault *IdentityVault
}
// GetIdentity .
func (s *Manager) GetIdentity(ctx context.Context, signer string, req *IdentityRequest) (*DSSIdentity, error) {
// check identity in vault
identity, ok := s.vault.Get(signer)
if ok {
return identity, nil
}
// otherwise request new identity
err := s.ensureToken(ctx)
if err != nil {
return nil, err
}
// request id and signing certificate
identityResp, _, err := s.client.DigitalSigningService.Identity(ctx, req)
if err != nil {
return nil, err
}
// request cs certificate
certResp, _, err := s.client.DigitalSigningService.CertificatePath(ctx)
if err != nil {
return nil, err
}
identity = &DSSIdentity{
ID: identityResp.ID,
SigningCert: identityResp.SigningCert,
OCSP: identityResp.OCSPResponse,
CA: certResp.CA,
}
s.vault.Set(signer, identity)
return identity, nil
}
// Sign .
func (s *Manager) Sign(ctx context.Context, signer string, identityReq *IdentityRequest, digest []byte) ([]byte, error) {
log.Println("request hash")
err := s.ensureToken(ctx)
if err != nil {
return nil, err
}
identity, err := s.GetIdentity(ctx, signer, identityReq)
if err != nil {
return nil, err
}
// encode to hex
digestHex := strings.ToUpper(hex.EncodeToString(digest))
signatureResp, _, err := s.client.DigitalSigningService.Sign(ctx, &SigningRequest{
ID: identity.ID,
Digest: digestHex,
})
if err != nil {
return nil, err
}
return hex.DecodeString(signatureResp.Signature)
}
// Timestamp .
func (s *Manager) Timestamp(ctx context.Context, signer string, identityReq *IdentityRequest, digest []byte) ([]byte, error) {
log.Println("request timestamp")
err := s.ensureToken(ctx)
if err != nil {
return nil, err
}
// encode to hex
digestHex := strings.ToUpper(hex.EncodeToString(digest))
timestampResp, httpResp, err := s.client.DigitalSigningService.Timestamp(ctx, &TimestampRequest{
Digest: digestHex,
})
if err != nil {
log.Println("TS err:", httpResp.Header)
return nil, err
}
return base64.StdEncoding.DecodeString(timestampResp.Token)
}
// ensure token valid
func (s *Manager) ensureToken(ctx context.Context) error {
s.RLock()
token := s.token
tokenTs := s.tokenTs
s.RUnlock()
// if token not yet acquired or expired
if token == "" || time.Since(tokenTs) > authTokenTTL {
resp, _, err := s.client.LoginService.Login(ctx, &LoginRequest{
APIKey: s.apiKey,
APISecret: s.apiSecret,
})
if err != nil {
return err
}
s.Lock()
s.token = resp.AccessToken
s.tokenTs = time.Now()
s.client.SetAuthToken(s.token)
s.Unlock()
}
return nil
}
// NewManager is a wrapper for client and
func NewManager(option *ManagerOption) (*Manager, error) {
if !option.Valid() {
return nil, errors.New("option is not valid")
}
// create a client
httpClient, err := NewHTTPClientWithCertificate(option.TLSCertificatePath, option.PrivateKeyPath, option.InsecureSkipVerify)
if err != nil {
return nil, err
}
client, err := New(httpClient, SetBaseURL(option.BaseURL))
if err != nil {
return nil, err
}
return &Manager{
apiKey: option.APIKey,
apiSecret: option.APISecret,
client: client,
vault: NewIdentityVault(identityTTL),
}, nil
}