This repository has been archived by the owner on Oct 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
issue.go
215 lines (177 loc) · 4.71 KB
/
issue.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
/*
Copyright (C) 2019 Steven Mirabito (smirabito@csh.rit.edu)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package tasks
import (
"errors"
"fmt"
"github.com/ComputerScienceHouse/gatekeeper/device"
"github.com/ComputerScienceHouse/gatekeeper/keys"
"github.com/ComputerScienceHouse/gatekeeper/sig"
"github.com/google/uuid"
"github.com/labstack/echo"
"github.com/labstack/gommon/log"
"net/http"
)
const taskTypeIssue = "issue"
type issueRequest struct {
SystemSecret string `json:"systemSecret"`
Realms []issueRequestRealm `json:"realms"`
}
type issueRequestRealm struct {
Name string `json:"name"`
Slot int `json:"slot"`
AssociationId string `json:"associationId"`
AuthKey string `json:"authKey"`
ReadKey string `json:"readKey"`
UpdateKey string `json:"updateKey"`
PublicKey string `json:"publicKey"`
PrivateKey string `json:"privateKey"`
}
type taskIssue struct {
ID uuid.UUID `json:"id"`
Type string `json:"type"`
Request *issueRequest `json:"-"`
Output chanWriter `json:"-"`
Logger log.Logger `json:"-"`
}
func (m *taskIssue) TaskType() string {
return taskTypeIssue
}
func (m *taskIssue) GetOutput() chanWriter {
return m.Output
}
func (m *taskIssue) LogError(err error) {
m.Logger.Errorf("[ERROR] %s", err)
m.Logger.Errorf("Aborting")
}
func (m *taskIssue) Run() {
m.Logger.Info("Parsing issue request...")
systemSecret, err := keys.Decode(m.Request.SystemSecret)
if err != nil {
m.LogError(err)
return
}
var realms []device.Realm
for _, realm := range m.Request.Realms {
if realm.Slot < 0 || realm.Slot > 15 {
m.LogError(errors.New("invalid slot number for realm, must be between 0-14"))
return
}
slot := uint32(realm.Slot)
associationId, err := uuid.Parse(realm.AssociationId)
if err != nil {
m.LogError(err)
return
}
authKey, err := keys.Decode(realm.AuthKey)
if err != nil {
m.LogError(err)
return
}
readKey, err := keys.Decode(realm.ReadKey)
if err != nil {
m.LogError(err)
return
}
updateKey, err := keys.Decode(realm.UpdateKey)
if err != nil {
m.LogError(err)
return
}
privateKey, publicKey, err := sig.Decode(realm.PrivateKey, realm.PublicKey)
if err != nil {
m.LogError(err)
return
}
realms = append(realms, device.Realm{
Name: realm.Name,
Slot: slot,
AssociationID: associationId,
AuthKey: authKey,
ReadKey: readKey,
UpdateKey: updateKey,
PublicKey: publicKey,
PrivateKey: privateKey,
})
}
m.Logger.Info("Opening NFC device...")
nfcDevice, err := device.OpenNFCDevice(m.Logger)
if err != nil {
m.LogError(err)
return
}
target, err := nfcDevice.Connect(m.Logger)
if err != nil {
m.LogError(err)
err = nfcDevice.Close(m.Logger)
if err != nil {
m.LogError(err)
}
return
}
m.Logger.Info("Writing tag...")
err = nfcDevice.Issue(*target, systemSecret, realms, m.Logger)
if err != nil {
m.LogError(err)
err = nfcDevice.Close(m.Logger)
if err != nil {
m.LogError(err)
}
return
}
m.Logger.Info("Closing NFC device...")
err = nfcDevice.Close(m.Logger)
if err != nil {
m.LogError(err)
err = nfcDevice.Close(m.Logger)
if err != nil {
m.LogError(err)
}
return
}
m.Logger.Info("Success")
}
func NewTaskIssue(request *issueRequest) (*taskIssue, error) {
id, err := uuid.NewRandom()
if err != nil {
return nil, err
}
output := newChanWriter()
logger := log.New(fmt.Sprintf("issue_%s", id))
logger.SetHeader("[${level}]")
logger.SetOutput(output)
return &taskIssue{
ID: id,
Type: taskTypeIssue,
Request: request,
Output: *output,
Logger: *logger,
}, nil
}
func CreateIssueTask(c echo.Context) error {
req := new(issueRequest)
if err := c.Bind(req); err != nil {
return err
}
task, err := NewTaskIssue(req)
if err != nil {
return err
}
taskStore[task.ID] = task
c.Logger().Info(fmt.Sprintf("Created '%s' task: %s", task.Type, task.ID.String()))
go task.Run()
taskURL := c.Echo().URL(GetTask, task.ID.String())
c.Response().Header().Set(echo.HeaderLocation, taskURL)
return c.NoContent(http.StatusSeeOther)
}