forked from alvinbaena/passkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_signer.go
144 lines (114 loc) · 2.82 KB
/
file_signer.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
package passkit
import (
"archive/zip"
"bytes"
"crypto/sha1"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
type fileSigner struct {
}
func NewFileBasedSigner() Signer {
return &fileSigner{}
}
func (f *fileSigner) CreateSignedAndZippedPassArchive(p *Pass, t PassTemplate, i *SigningInformation) ([]byte, error) {
return f.CreateSignedAndZippedPersonalizedPassArchive(p, nil, t, i)
}
func (f *fileSigner) CreateSignedAndZippedPersonalizedPassArchive(p *Pass, pz *Personalization, t PassTemplate, i *SigningInformation) ([]byte, error) {
dir, err := ioutil.TempDir("", "pass")
if err != nil {
return nil, err
}
if err := t.ProvisionPassAtDirectory(dir); err != nil {
return nil, err
}
if err := f.createPassJSONFile(p, dir); err != nil {
return nil, err
}
if pz != nil {
if err := f.createPersonalizationJSONFile(pz, dir); err != nil {
return nil, err
}
}
mfst, err := f.createManifestJSONFile(dir)
if err != nil {
return nil, err
}
signedMfst, err := signManifestFile(mfst, i)
if err != nil {
return nil, err
}
err = ioutil.WriteFile(signatureFileName, signedMfst, 0644)
if err != nil {
return nil, err
}
z, err := f.createZipFile(dir)
if err != nil {
return nil, err
}
//Fail silently
_ = os.RemoveAll(dir)
return z, nil
}
func (f *fileSigner) SignManifestFile(manifestJson []byte, i *SigningInformation) ([]byte, error) {
return signManifestFile(manifestJson, i)
}
func (f *fileSigner) createPassJSONFile(p *Pass, tmpDir string) error {
if !p.IsValid() {
return fmt.Errorf("%v", p.GetValidationErrors())
}
b, err := p.toJSON()
if err != nil {
return err
}
return ioutil.WriteFile(filepath.Join(tmpDir, passJsonFileName), b, 0644)
}
func (f *fileSigner) createPersonalizationJSONFile(pz *Personalization, tmpDir string) error {
b, err := pz.toJSON()
if err != nil {
return err
}
return ioutil.WriteFile(filepath.Join(tmpDir, personalizationJsonFileName), b, 0644)
}
func (f *fileSigner) createManifestJSONFile(tmpDir string) ([]byte, error) {
m, err := f.hashFiles(tmpDir)
if err != nil {
return nil, err
}
bm, err := json.Marshal(m)
if err != nil {
return nil, err
}
err = ioutil.WriteFile(manifestJsonFileName, bm, 0644)
if err != nil {
return nil, err
}
return bm, nil
}
func (f *fileSigner) hashFiles(tmpDir string) (map[string]string, error) {
files, err := loadDir(tmpDir)
if err != nil {
return nil, err
}
ret := make(map[string]string)
for name, data := range files {
hash := sha1.Sum(data)
ret[filepath.Base(name)] = fmt.Sprintf("%x", hash)
}
return ret, nil
}
func (f *fileSigner) createZipFile(tmpDir string) ([]byte, error) {
buf := new(bytes.Buffer)
w := zip.NewWriter(buf)
err := addFiles(w, tmpDir, "")
if err != nil {
return nil, err
}
if err := w.Close(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}