-
Notifications
You must be signed in to change notification settings - Fork 3
/
api_key.go
187 lines (154 loc) · 3.71 KB
/
api_key.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
package eventline
import (
"context"
"crypto/sha256"
"errors"
"fmt"
"time"
"github.com/exograd/eventline/pkg/utils"
"github.com/exograd/go-daemon/check"
"github.com/exograd/go-daemon/pg"
"github.com/jackc/pgx/v4"
)
var APIKeySorts Sorts = Sorts{
Sorts: map[string]string{
"id": "id",
"name": "name",
},
Default: "name",
}
type UnknownAPIKeyError struct {
Id *Id
}
func (err UnknownAPIKeyError) Error() string {
if err.Id == nil {
return "unknown api key"
} else {
return fmt.Sprintf("unknown api key %q", err.Id)
}
}
type NewAPIKey struct {
Name string `json:"name"`
}
type APIKey struct {
Id Id `json:"id"`
AccountId Id `json:"account_id"`
Name string `json:"name"`
CreationTime time.Time `json:"creation_time"`
LastUseTime *time.Time `json:"last_use_time,omitempty"`
KeyHash []byte `json:"-"`
}
type APIKeys []*APIKey
func (nk *NewAPIKey) Check(c *check.Checker) {
CheckName(c, "name", nk.Name)
}
func (k *APIKey) SortKey(sort string) (key string) {
switch sort {
case "id":
key = k.Id.String()
case "name":
key = k.Name
default:
utils.Panicf("unknown api key sort %q", sort)
}
return
}
func APIKeyNameExists(conn pg.Conn, name string, scope Scope) (bool, error) {
ctx := context.Background()
query := fmt.Sprintf(`
SELECT COUNT(*)
FROM api_keys
WHERE %s AND name = $1
`, scope.SQLCondition())
var count int64
err := conn.QueryRow(ctx, query, name).Scan(&count)
if err != nil {
return false, err
}
return count > 0, nil
}
func (k *APIKey) LoadForUpdate(conn pg.Conn, id Id, scope Scope) error {
query := `
SELECT id, account_id, name, creation_time,
last_use_time, key_hash
FROM api_keys
WHERE id = $1
FOR UPDATE
`
err := pg.QueryObject(conn, k, query, id)
if errors.Is(err, pgx.ErrNoRows) {
return &UnknownAPIKeyError{Id: &id}
}
return err
}
func (k *APIKey) LoadUpdateByKeyHash(conn pg.Conn, keyHash []byte) error {
now := time.Now().UTC()
query := `
UPDATE api_keys SET
last_use_time = $2
WHERE key_hash = $1
RETURNING id, account_id, name, creation_time,
last_use_time, key_hash
`
err := pg.QueryObject(conn, k, query, keyHash, now)
if errors.Is(err, pgx.ErrNoRows) {
return &UnknownAPIKeyError{}
}
return err
}
func LoadAPIKeyPage(conn pg.Conn, cursor *Cursor, scope Scope) (*Page, error) {
query := fmt.Sprintf(`
SELECT id, account_id, name, creation_time,
last_use_time, key_hash
FROM api_keys
WHERE %s AND %s
`, scope.SQLCondition(), cursor.SQLConditionOrderLimit(APIKeySorts))
var apiKeys APIKeys
if err := pg.QueryObjects(conn, &apiKeys, query); err != nil {
return nil, err
}
return apiKeys.Page(cursor), nil
}
func (k *APIKey) Insert(conn pg.Conn) error {
query := `
INSERT INTO api_keys
(id, account_id, name, creation_time,
last_use_time, key_hash)
VALUES
($1, $2, $3, $4,
$5, $6);
`
return pg.Exec(conn, query,
k.Id, k.AccountId, k.Name, k.CreationTime,
k.LastUseTime, k.KeyHash)
}
func (k *APIKey) Delete(conn pg.Conn, scope Scope) error {
query := fmt.Sprintf(`
DELETE FROM api_keys
WHERE %s AND id = $1;
`, scope.SQLCondition())
return pg.Exec(conn, query, k.Id)
}
func (ks APIKeys) Page(cursor *Cursor) *Page {
elements := make([]PageElement, len(ks))
for i, k := range ks {
elements[i] = k
}
return NewPage(cursor, elements, APIKeySorts)
}
func (k *APIKey) FromRow(row pgx.Row) error {
return row.Scan(&k.Id, &k.AccountId, &k.Name, &k.CreationTime,
&k.LastUseTime, &k.KeyHash)
}
func (ks *APIKeys) AddFromRow(row pgx.Row) error {
var k APIKey
if err := k.FromRow(row); err != nil {
return err
}
*ks = append(*ks, &k)
return nil
}
func HashAPIKey(key string) []byte {
hash := sha256.Sum256([]byte(key))
return hash[:]
}