-
Notifications
You must be signed in to change notification settings - Fork 44
/
apikey.go
149 lines (122 loc) · 3.06 KB
/
apikey.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
// Copyright 2015 mokey Authors. All rights reserved.
// Use of this source code is governed by a BSD style
// license that can be found in the LICENSE file.
package model
import (
"crypto/rand"
"crypto/sha256"
"database/sql"
"encoding/base64"
"time"
)
type ApiKeyClient struct {
Name string
ClientID string `mapstructure:"client_id"`
Description string `mapstructure:"desc"`
Scopes string
}
type ApiKey struct {
UserName string `db:"user_name"`
Key string `db:"api_key"`
ClientID string `db:"client_id"`
Scopes string `db:"scopes"`
CreatedAt *time.Time `db:"created_at"`
LastAccessed *time.Time `db:"last_accessed"`
}
func (db *DB) RandKey() (string, error) {
b := make([]byte, 32)
_, err := rand.Read(b)
if err != nil {
return "", err
}
return base64.RawURLEncoding.EncodeToString(b), nil
}
func (db *DB) FetchApiKeys(uid string) ([]*ApiKey, error) {
keys := []*ApiKey{}
err := db.Select(&keys, `
select
user_name,
api_key,
client_id,
scopes,
created_at,
last_accessed
from api_key where user_name = ?`, uid)
if err == sql.ErrNoRows {
return nil, ErrNotFound
} else if err != nil {
return nil, err
}
return keys, nil
}
func (db *DB) FetchApiKey(key string) (*ApiKey, error) {
h := sha256.Sum256([]byte(key))
hash := base64.StdEncoding.EncodeToString(h[:])
a := ApiKey{}
err := db.Get(&a, `
select
user_name,
api_key,
client_id,
scopes,
created_at,
last_accessed
from api_key where api_key = ?`, hash)
if err == sql.ErrNoRows {
return nil, ErrNotFound
} else if err != nil {
return nil, err
}
return &a, nil
}
func (db *DB) CreateApiKey(uid, clientID, scopes string) (*ApiKey, string, error) {
key, err := db.RandKey()
if err != nil {
return nil, "", err
}
h := sha256.Sum256([]byte(key))
hash := base64.StdEncoding.EncodeToString(h[:])
a := ApiKey{
UserName: uid,
ClientID: clientID,
Scopes: scopes,
Key: string(hash)}
_, err = db.NamedExec(`
replace into api_key
(user_name,api_key,client_id,scopes,created_at,last_accessed)
values (:user_name, :api_key, :client_id, :scopes, now(), now())`, a)
if err != nil {
return nil, "", err
}
return &a, key, nil
}
func (db *DB) RefreshApiKey(ak *ApiKey) error {
_, err := db.NamedExec(`
update api_key set
last_accessed = now()
where user_name = :user_name and api_key = :api_key`, ak)
if err == sql.ErrNoRows {
return ErrNotFound
} else if err != nil {
return err
}
return nil
}
func (db *DB) DestroyApiKey(user, clientID string) error {
_, err := db.Exec("delete from api_key where user_name = ? and client_id = ?", user, clientID)
if err == sql.ErrNoRows {
return ErrNotFound
} else if err != nil {
return err
}
return nil
}
func (db *DB) DestroyApiKeys(uid string) error {
_, err := db.Exec("delete from api_key where user_name = ?", uid)
if err == sql.ErrNoRows {
return ErrNotFound
} else if err != nil {
return err
}
return nil
}