-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathlicense.go
89 lines (72 loc) · 2.02 KB
/
license.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
package launch
import (
"bytes"
"crypto/sha256"
"io/ioutil"
"path/filepath"
"github.com/itchio/butler/butlerd"
"github.com/itchio/butler/butlerd/messages"
)
// ensureLicenseAcceptance checks whether we need the user to accept
// a license before continuing.
func ensureLicenseAcceptance(rc *butlerd.RequestContext, installFolder string) error {
consumer := rc.Consumer
license := getLicense(installFolder)
if license == "" {
consumer.Debugf("No license agreement, continuing")
return nil
}
hashed := hashLicense(license)
marker := getLicenseMarker(installFolder)
if bytes.Equal(hashed, marker) {
consumer.Infof("License agreement already accepted")
return nil
}
if marker == nil {
consumer.Infof("Found license, never accepted before")
} else {
consumer.Infof("Found license, a different one was accepted before")
}
res, err := messages.AcceptLicense.Call(rc, butlerd.AcceptLicenseParams{
Text: license,
})
if err != nil {
return err
}
if !res.Accept {
consumer.Errorf("License rejected, cancelling launch")
return butlerd.CodeOperationCancelled
}
err = writeLicenseMarker(installFolder, hashed)
if err != nil {
consumer.Warnf("Could not write license marker: %+v", err)
}
return nil
}
func licensePath(installFolder string) string {
return filepath.Join(installFolder, ".itch", "sla.txt")
}
func licenseMarkerPath(installFolder string) string {
return filepath.Join(installFolder, ".itch", "sla-accepted-hash.sha256")
}
func getLicense(installFolder string) string {
payload, err := ioutil.ReadFile(licensePath(installFolder))
if err != nil {
return ""
}
return string(payload)
}
func hashLicense(license string) []byte {
sum := sha256.Sum256([]byte(license))
return sum[:]
}
func getLicenseMarker(installFolder string) []byte {
payload, err := ioutil.ReadFile(licenseMarkerPath(installFolder))
if err != nil {
return nil
}
return payload
}
func writeLicenseMarker(installFolder string, hashed []byte) error {
return ioutil.WriteFile(licenseMarkerPath(installFolder), hashed, 0644)
}