-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsqlite.go
More file actions
208 lines (166 loc) · 4.22 KB
/
Copy pathsqlite.go
File metadata and controls
208 lines (166 loc) · 4.22 KB
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
package main
import (
"database/sql"
"encoding/base64"
"encoding/hex"
"errors"
"time"
"github.com/mattn/go-sqlite3"
)
// Db structure
type Db struct {
sqlite *sql.DB
}
// Keyring structure
type Keyring struct {
Id int64
Name string
ApiKey []byte
}
// Key structure
type Key struct {
Id int64
Created time.Time
Counter int64
Session int64
Public string
Secret string
Keyring int64
}
// Token structure
type Token struct {
Uid [UidSize]byte
Ctr uint16
Tstpl uint16
Tstph uint8
Use uint8
Rnd uint16
Crc uint16
}
func air(err error) {
if err != nil {
panic(err)
}
}
func initSqlite(dbPath string) *Db {
dsn := "file:" + dbPath + "?_foreign_keys=1&_secure_delete=1"
db, err := sql.Open("sqlite3", dsn)
air(err)
keyringTable := `
CREATE TABLE IF NOT EXISTS keyrings(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
name TEXT UNIQUE,
key TEXT UNIQUE,
created DATETIME
);
`
db.Exec(keyringTable)
keyTable := `
CREATE TABLE IF NOT EXISTS keys(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
created DATETIME,
counter INTEGER,
session INTEGER,
public TEXT UNIQUE,
secret TEXT UNIQUE,
keyring INTEGER REFERENCES keyrings(id) ON DELETE CASCADE
);
`
db.Exec(keyTable)
return &Db{sqlite: db}
}
func (db *Db) createKeyring(name string) (*Keyring, error) {
// generate our ApiKey
key := generateApiKey(name)
// insert it in the keyring database
stmt, err := db.sqlite.Prepare(`INSERT INTO keyrings(name, key, created) VALUES(?, ?, ?)`)
defer stmt.Close()
air(err)
res, err := stmt.Exec(name, base64.StdEncoding.EncodeToString(key), time.Now())
if sqlErr, ok := err.(sqlite3.Error); ok {
switch sqlErr.ExtendedCode {
case sqlite3.ErrConstraintUnique:
return nil, errors.New("This keyring already exists")
default:
return nil, errors.New("Unknown error")
}
}
air(err)
i64, err := res.LastInsertId()
air(err)
var keyring Keyring
keyring.Id = i64
keyring.Name = name
keyring.ApiKey = key
return &keyring, nil
}
func (db *Db) updateKey(key *Key) error {
stmt, err := db.sqlite.Prepare("UPDATE keys SET counter = ?, session = ? WHERE public = ?")
defer stmt.Close()
if err != nil {
return errors.New(BACKEND_ERROR)
}
_, err = stmt.Exec(key.Counter, key.Session, key.Public)
if err != nil {
return errors.New(BACKEND_ERROR)
}
return nil
}
func (db *Db) addKey(id int64, public, secret string) (*Key, error) {
_, err := hex.DecodeString(secret)
if err != nil {
return nil, errors.New("Unable to convert secret key to hexadecimal format")
}
stmt, err := db.sqlite.Prepare(`INSERT INTO keys(created, counter, session, public, secret, keyring) values(?, ?, ?, ?, ?, ?)`)
defer stmt.Close()
air(err)
var key Key
key.Created = time.Now()
key.Counter = 0
key.Session = 0
key.Public = public
key.Secret = secret
key.Keyring = id
res, err := stmt.Exec(key.Created, key.Counter, key.Session, key.Public, key.Secret, key.Keyring)
if sqlErr, ok := err.(sqlite3.Error); ok {
switch sqlErr.ExtendedCode {
case sqlite3.ErrConstraintUnique:
return nil, errors.New("This public or secret key already exists")
default:
return nil, errors.New("Unknown error")
}
}
air(err)
key.Id, err = res.LastInsertId()
air(err)
return &key, nil
}
func (db *Db) getKey(public string) (*Key, error) {
stmt, err := db.sqlite.Prepare(`SELECT created, counter, session, public, secret, keyring FROM keys WHERE public = ?`)
defer stmt.Close()
air(err)
// get the designated key in the database
key := Key{}
err = stmt.QueryRow(public).Scan(&key.Created, &key.Counter, &key.Session, &key.Public, &key.Secret, &key.Keyring)
if err == sql.ErrNoRows {
return nil, errors.New("No such key exists")
}
air(err)
return &key, nil
}
func (db *Db) getKeyring(id int64) (*Keyring, error) {
stmt, err := db.sqlite.Prepare(`SELECT id, name, key FROM keyrings WHERE id = ?`)
defer stmt.Close()
air(err)
keyring := Keyring{}
err = stmt.QueryRow(id).Scan(&keyring.Id, &keyring.Name, &keyring.ApiKey)
if err == sql.ErrNoRows {
return nil, errors.New("No such keyring exists")
}
air(err)
keyring.ApiKey, err = base64.StdEncoding.DecodeString(string(keyring.ApiKey))
if err != nil {
return nil, errors.New("Unable to decode keyring key")
}
return &keyring, nil
}