-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdss.go
174 lines (139 loc) · 3.89 KB
/
dss.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
package globalsign
import (
"context"
"errors"
"net/http"
"time"
)
// errors definition
var (
ErrDigestRequired = errors.New("file digest required")
)
// DSSIdentity represent acquired credential
// from login and identity request
type DSSIdentity struct {
// Identity
ID string
SigningCert string
OCSP string
CA string
Ts time.Time
}
// IdentityRequest .
type IdentityRequest struct {
SubjectDn map[string]interface{} `json:"subject_dn"`
}
// IdentityResponse .
type IdentityResponse struct {
ID string `json:"id"`
SigningCert string `json:"signing_cert"`
OCSPResponse string `json:"ocsp_response"`
}
// TimestampRequest .
type TimestampRequest struct {
Digest string `json:"digest"`
}
// TimestampResponse .
type TimestampResponse struct {
Token string `json:"token"`
}
// SigningRequest .
type SigningRequest struct {
ID string `json:"id"`
// a hex encoded sha256 checksum for source file
Digest string `json:"digest"`
}
// SigningResponse .
type SigningResponse struct {
Signature string `json:"signature"`
}
// CertificateResponse .
type CertificateResponse struct {
CA string `json:"path"`
}
// TrusChainResponse .
type TrustChainResponse struct {
Path string `json:"path"`
}
// DigitalSigningService .
type DigitalSigningService interface {
Identity(context.Context, *IdentityRequest) (*IdentityResponse, *Response, error)
Timestamp(context.Context, *TimestampRequest) (*TimestampResponse, *Response, error)
Sign(context.Context, *SigningRequest) (*SigningResponse, *Response, error)
CertificatePath(context.Context) (*CertificateResponse, *Response, error)
TrustChain(context.Context) (*TrustChainResponse, *Response, error)
}
type digitalSigningService struct {
client *Client
}
func (s *digitalSigningService) Identity(ctx context.Context, req *IdentityRequest) (*IdentityResponse, *Response, error) {
path := baseAPI + "/identity"
r, err := s.client.NewRequest(ctx, http.MethodPost, path, req)
if err != nil {
return nil, nil, err
}
result := new(IdentityResponse)
resp, err := s.client.Do(ctx, r, result)
if err != nil {
return nil, resp, err
}
return result, resp, nil
}
func (s *digitalSigningService) Timestamp(ctx context.Context, req *TimestampRequest) (*TimestampResponse, *Response, error) {
if req == nil {
return nil, nil, ErrDigestRequired
}
path := baseAPI + "/timestamp/" + req.Digest
r, err := s.client.NewRequest(ctx, http.MethodGet, path, struct{}{})
if err != nil {
return nil, nil, err
}
result := new(TimestampResponse)
resp, err := s.client.Do(ctx, r, result)
if err != nil {
return nil, resp, err
}
return result, resp, nil
}
func (s *digitalSigningService) Sign(ctx context.Context, req *SigningRequest) (*SigningResponse, *Response, error) {
if req == nil {
return nil, nil, ErrDigestRequired
}
path := baseAPI + "/identity/" + req.ID + "/sign/" + req.Digest
r, err := s.client.NewRequest(ctx, http.MethodGet, path, struct{}{})
if err != nil {
return nil, nil, err
}
result := new(SigningResponse)
resp, err := s.client.Do(ctx, r, result)
if err != nil {
return nil, resp, err
}
return result, resp, nil
}
func (s *digitalSigningService) CertificatePath(ctx context.Context) (*CertificateResponse, *Response, error) {
path := baseAPI + "/certificate_path"
r, err := s.client.NewRequest(ctx, http.MethodGet, path, struct{}{})
if err != nil {
return nil, nil, err
}
result := new(CertificateResponse)
resp, err := s.client.Do(ctx, r, result)
if err != nil {
return nil, resp, err
}
return result, resp, nil
}
func (s *digitalSigningService) TrustChain(ctx context.Context) (*TrustChainResponse, *Response, error) {
path := baseAPI + "/certificate_path"
r, err := s.client.NewRequest(ctx, http.MethodGet, path, struct{}{})
if err != nil {
return nil, nil, err
}
result := new(TrustChainResponse)
resp, err := s.client.Do(ctx, r, result)
if err != nil {
return nil, resp, err
}
return result, resp, nil
}