-
Notifications
You must be signed in to change notification settings - Fork 16
/
dal.go
191 lines (169 loc) · 3.68 KB
/
dal.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
package main
import (
"database/sql"
"encoding/base64"
"errors"
_ "github.com/mattn/go-sqlite3"
"time"
)
type Dal struct {
db *sql.DB
}
type Key struct {
Id int
Name string
Created *time.Time
Used *time.Time
Counter int
Session int
Public string
Secret string
}
type App struct {
Id int
Name string
Key []byte
}
func newDAL(dbfile string) (*Dal, error) {
d, err := sql.Open("sqlite3", dbfile)
if err != nil {
return nil, err
}
ret := Dal{db: d}
ret.init()
return &ret, nil
}
func (d *Dal) CreateApp(app *App) (*App, error) {
// Truncate the key to the length we expect
app.Key = Sign([]string{app.Name}, app.Key)
stmt, err := d.db.Prepare(`insert into apps(name, key, created) values(?, ?, ?)`)
if err != nil {
return nil, err
}
defer stmt.Close()
_, err = stmt.Exec(app.Name, base64.StdEncoding.EncodeToString(app.Key), time.Now())
if err != nil {
return nil, err
}
stmt2, err := d.db.Prepare("select MAX(id) from apps LIMIT 1")
if err != nil {
return nil, err
}
defer stmt2.Close()
err = stmt2.QueryRow().Scan(&app.Id)
if err != nil {
return nil, err
}
return app, nil
}
func (d *Dal) CreateKey(key *Key) error {
if key.Name == "" {
return errors.New("name need to be indicated")
} else if key.Public == "" {
return errors.New("pub need to be indicated")
} else if key.Secret == "" {
return errors.New("secret need to be indicated")
} else {
k, _ := d.GetKey(key.Public)
if k != nil {
return errors.New("public key: " + key.Public + " already exists")
} else {
stmt, err := d.db.Prepare(`insert into keys(name, created, counter, session, public, secret) values(?, ?, ?, ?, ?, ?)`)
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(key.Name, time.Now(), 0, 0, key.Public, key.Secret)
if err != nil {
return err
}
return nil
}
}
return nil
}
func (d *Dal) DeleteKey(key *Key) error {
if key.Name == "" {
return errors.New("name need to be indicated")
} else {
stmt, err := d.db.Prepare(`delete from keys where name=?`)
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(key.Name)
if err != nil {
return err
}
return nil
}
return nil
}
func (d *Dal) UpdateKey(key *Key) error {
stmt, err := d.db.Prepare("update keys set counter = ?, session = ?, used = ?where public = ?")
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(key.Counter, key.Session, time.Now(), key.Public)
if err != nil {
return err
}
return nil
}
func (d *Dal) GetApp(id string) ([]byte, error) {
stmt, err := d.db.Prepare("select key from apps where id = ?")
if err != nil {
return nil, err
}
defer stmt.Close()
base64key := ""
err = stmt.QueryRow(id).Scan(&base64key)
if err != nil {
return nil, err
}
key, err := base64.StdEncoding.DecodeString(base64key)
if err != nil {
return nil, err
}
return key, nil
}
func (d *Dal) GetKey(pub string) (*Key, error) {
stmt, err := d.db.Prepare("select name, created, used, counter, session, public, secret from keys where public = ?")
if err != nil {
return nil, err
}
defer stmt.Close()
user := Key{}
err = stmt.QueryRow(pub).Scan(&user.Name, &user.Created, &user.Used, &user.Counter, &user.Session, &user.Public, &user.Secret)
if err != nil {
return nil, err
}
return &user, nil
}
func (d *Dal) init() {
sqlStmt := `
create table keys (
id integer not null primary key AUTOINCREMENT,
name text,
created datetime,
used datetime,
counter int,
session int,
public text,
secret text);
`
d.db.Exec(sqlStmt)
sqlStmt = `
create table apps (
id integer not null primary key AUTOINCREMENT,
name text,
created datetime,
key text
)`
d.db.Exec(sqlStmt)
return
}
func (d *Dal) Close() {
d.db.Close()
}