-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader.go
72 lines (60 loc) · 1.62 KB
/
header.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
package jws
import (
"crypto/x509"
"time"
"github.com/alexandru-ionut-balan/ing-jws/crypto"
"github.com/alexandru-ionut-balan/ing-jws/logging"
)
type JwsHeader struct {
B64 bool `json:"b64"`
S256 string `json:"x5t#S256"`
Crit []string `json:"crit"`
SigT string `json:"sigT"`
SigD SignedHeaders `json:"sigD"`
Alg string `json:"alg"`
}
type SignedHeaders struct {
Pars []string `json:"pars"`
MId string `json:"mId"`
}
func DefaultJwsHeader() *JwsHeader {
return &JwsHeader{
B64: false,
Crit: []string{"sigT", "sigD", "b64"},
Alg: "RS256",
SigD: SignedHeaders{
Pars: []string{"(request-target)", "digest"},
MId: "http://uri.etsi.org/19182/HttpHeaders",
},
}
}
func (jh *JwsHeader) WithB64(b64 bool) *JwsHeader {
jh.B64 = b64
return jh
}
func (jh *JwsHeader) WithCertificate(certificate x509.Certificate) *JwsHeader {
fingerprint, err := crypto.RawSha256(certificate.Raw)
if err != nil {
logging.Error("Cannot fill S256 (x5t#S256) header value beacuse certificate fingerprint could not be determined.", err)
return jh
}
jh.S256 = crypto.Base64(fingerprint)
return jh
}
func (jh *JwsHeader) WithCrit(criticalFields []string) *JwsHeader {
jh.Crit = criticalFields
return jh
}
func (jh *JwsHeader) WithClaimedTime(claimedTime time.Time) *JwsHeader {
formattedTime := claimedTime.In(time.UTC).Format(time.RFC3339)
jh.SigT = formattedTime
return jh
}
func (jh *JwsHeader) WithSignedHeaders(headers SignedHeaders) *JwsHeader {
jh.SigD = headers
return jh
}
func (jh *JwsHeader) WithSigningAlgorithm(algorithm string) *JwsHeader {
jh.Alg = algorithm
return jh
}