This repository has been archived by the owner on Sep 6, 2023. It is now read-only.
/
signature.go
265 lines (233 loc) · 8.19 KB
/
signature.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package themis_contract
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"sort"
"strings"
"github.com/rs/zerolog/log"
)
type SignatureParameter string
const (
SignatureEmail SignatureParameter = "email"
SignatureImage SignatureParameter = "image"
SignatureTimestampFormat = "2 January 2006"
)
// Signature is what we apply to a contract to sign it.
// TODO: Investigate GPG-based signing.
type Signature struct {
Name string `json:"name"` // A short, descriptive name for the signature.
Email string `json:"email"` // The e-mail address associated with a specific signature.
ImagePath string `json:"image"` // The filesystem path to the image constituting the image-based signature.
id string // A unique ID associated with this signature (derived from the filesystem path).
path string // The filesystem path to the signature's information.
}
// SignatureDB is a local database of signatures which can be applied to
// contracts when signing.
type SignatureDB struct {
sigs map[string]*Signature
sigsPath string
}
type SignatureByName []*Signature
var _ sort.Interface = SignatureByName{}
//------------------------------------------------------------------------------
//
// Signature database-related functionality
//
//------------------------------------------------------------------------------
// loadSignatureDB will attempt to load our local collection of signatures,
// given the specified home folder for Themis Contract.
func loadSignatureDB(home string) (*SignatureDB, error) {
sigsPath := themisContractSignaturesPath(home)
files, err := ioutil.ReadDir(sigsPath)
if err != nil {
return nil, err
}
db := &SignatureDB{
sigs: make(map[string]*Signature),
sigsPath: sigsPath,
}
for _, fi := range files {
if fi.IsDir() && !strings.HasPrefix(fi.Name(), ".") {
sigPath := path.Join(sigsPath, fi.Name())
sig, err := loadSignature(sigPath)
if err != nil {
return nil, fmt.Errorf("failed to load signature \"%s\": %s", sigPath, err)
}
db.sigs[sig.id] = sig
}
}
return db, nil
}
func (db *SignatureDB) newSignature(name, email, sigImage string) (*Signature, error) {
id, err := slugify(name)
if err != nil {
return nil, fmt.Errorf("failed to generate ID from signature name \"%s\": %s", name, err)
}
if _, exists := db.sigs[id]; exists {
return nil, fmt.Errorf("signature with ID \"%s\" (derived from name \"%s\") already exists", id, name)
}
sigPath := path.Join(db.sigsPath, id)
if err := os.MkdirAll(sigPath, 0755); err != nil {
return nil, fmt.Errorf("failed to create folder for new signature \"%s\": %s", sigPath, err)
}
log.Debug().Msgf("Created new folder for signature: %s", sigPath)
newSigImagePath := path.Join(sigPath, path.Base(sigImage))
// try copying the image
if err := copyFile(sigImage, newSigImagePath); err != nil {
return nil, fmt.Errorf("failed to copy supplied signature image \"%s\" for new signature: %s", sigImage, err)
}
log.Debug().Msgf("Copied signature image from %s to %s", sigImage, newSigImagePath)
sig := &Signature{
Name: name,
Email: email,
ImagePath: path.Base(sigImage),
id: id,
path: sigPath,
}
if err := sig.Save(); err != nil {
return nil, fmt.Errorf("failed to save signature: %s", err)
}
db.sigs[id] = sig
return sig, nil
}
// remove will attempt to delete the signature with the specified ID from the
// signature store.
func (db *SignatureDB) remove(id string) error {
sig, exists := db.sigs[id]
if !exists {
return fmt.Errorf("no such signature with ID \"%s\"", id)
}
delete(db.sigs, id)
if err := os.RemoveAll(sig.path); err != nil {
return fmt.Errorf("failed to delete signature \"%s\" from file system: %s", id, err)
}
return nil
}
func (db *SignatureDB) rename(srcID, destName string) (string, error) {
destID, err := slugify(destName)
if err != nil {
return "", fmt.Errorf("failed to derive ID for name \"%s\": %s", destName, err)
}
sig, exists := db.sigs[srcID]
if !exists {
return "", fmt.Errorf("no such signature with ID \"%s\"", srcID)
}
if _, exists := db.sigs[destID]; exists {
return "", fmt.Errorf("signature with ID \"%s\" (derived from name \"%s\") already exists", destID, destName)
}
destPath := path.Join(db.sigsPath, destID)
if err := os.Rename(sig.path, destPath); err != nil {
return "", fmt.Errorf("failed to rename signature folder from %s to %s: %s", sig.path, destPath, err)
}
sig.id = destID
sig.Name = destName
sig.path = destPath
if err := sig.Save(); err != nil {
return "", err
}
return destID, nil
}
//------------------------------------------------------------------------------
//
// Signature-related functionality
//
//------------------------------------------------------------------------------
func ValidSignatureParamNames() []string {
return []string{
string(SignatureEmail),
string(SignatureImage),
}
}
// loadSignature will attempt to load information relating to the signature
// located at the given filesystem path.
func loadSignature(sigPath string) (*Signature, error) {
sigJson, err := ioutil.ReadFile(path.Join(sigPath, "meta.json"))
if err != nil {
return nil, err
}
var sig Signature
if err := json.Unmarshal(sigJson, &sig); err != nil {
return nil, err
}
sig.id = path.Base(sigPath)
sig.path = sigPath
if len(sig.ImagePath) == 0 {
return nil, fmt.Errorf("signature \"%s\" is missing an image", sig.id)
}
sigImagePath := path.Join(sigPath, sig.ImagePath)
if _, err = os.Stat(sigImagePath); os.IsNotExist(err) {
return nil, fmt.Errorf("cannot find image file for signature \"%s\": %s", sig.id, sigImagePath)
}
log.Debug().Msgf("Loaded signature: %v", &sig)
return &sig, nil
}
func (s *Signature) Save() error {
content, err := json.Marshal(s)
if err != nil {
return fmt.Errorf("failed to convert signature \"%s\" to JSON: %s", s.id, err)
}
if err := ioutil.WriteFile(path.Join(s.path, "meta.json"), content, 0644); err != nil {
return fmt.Errorf("failed to save signature \"%s\": %s", s.id, err)
}
return nil
}
// applyTo will attempt to apply this signature to the contract in the specified
// path (assuming it's the full path to the contract file) on behalf of the
// specified signatory ID. On success, returns the path to the image we've just
// copied across.
func (s *Signature) applyTo(contractPath string, sigId string) (string, error) {
sigImageSrcPath := path.Join(s.path, s.ImagePath)
sigImageDestPath := path.Join(path.Dir(contractPath), sigImageFilename(sigId))
log.Debug().Msgf("Copying signature file from %s to %s", sigImageSrcPath, sigImageDestPath)
if err := copyFile(sigImageSrcPath, sigImageDestPath); err != nil {
return "", fmt.Errorf("failed to copy signature from %s to %s: %s", sigImageSrcPath, sigImageDestPath, err)
}
return sigImageDestPath, nil
}
func (s *Signature) String() string {
return fmt.Sprintf("Signature{Name: \"%s\", Email: \"%s\", ImagePath: \"%s\"}", s.Name, s.Email, s.ImagePath)
}
func (s *Signature) Display() string {
sigImagePath := "(none)"
if len(s.ImagePath) > 0 {
sigImagePath = path.Join(s.path, s.ImagePath)
}
return fmt.Sprintf("%s (ID: %s, e-mail: %s, image: %s)", s.Name, s.id, s.Email, sigImagePath)
}
//------------------------------------------------------------------------------
//
// Signature sorting
//
//------------------------------------------------------------------------------
func (n SignatureByName) Len() int { return len(n) }
func (n SignatureByName) Swap(i, j int) {
t := n[i]
n[i] = n[j]
n[j] = t
}
func (n SignatureByName) Less(i, j int) bool { return n[i].Name < n[j].Name }
//------------------------------------------------------------------------------
//
// Helper methods
//
//------------------------------------------------------------------------------
func sigImageFilename(sigId string) string {
return fmt.Sprintf("sig--%s.png", sigId)
}
func themisContractSignaturesPath(home string) string {
return path.Join(home, "signatures")
}
func initSignatures(home string) error {
return os.MkdirAll(themisContractSignaturesPath(home), 0755)
}
// TODO: Use Git to extract the signed date instead of just checking the timestamp of the file.
func getLatestSignedDate(sigFile string) (string, error) {
fi, err := os.Stat(sigFile)
if err != nil {
return "", err
}
return fi.ModTime().Format(SignatureTimestampFormat), nil
}