-
-
Notifications
You must be signed in to change notification settings - Fork 200
/
api_token.go
254 lines (222 loc) · 6.39 KB
/
api_token.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright © Martin Tournoij – This file is part of GoatCounter and published
// under the terms of a slightly modified EUPL v1.2 license, which can be found
// in the LICENSE file or at https://license.goatcounter.com
package goatcounter
import (
"context"
"strings"
"time"
"zgo.at/errors"
"zgo.at/zdb"
"zgo.at/zstd/zcrypto"
"zgo.at/zstd/zint"
"zgo.at/zstd/ztime"
"zgo.at/zstd/ztype"
)
// APIToken permissions.
//
// DO NOT change the values of these constants; they're stored in the database.
const (
APIPermNothing zint.Bitflag64 = 1 << iota
APIPermCount // 2
APIPermExport // 4
APIPermSiteRead // 8
APIPermSiteCreate // 16
APIPermSiteUpdate // 32
APIPermStats // 64
)
type APIToken struct {
ID int64 `db:"api_token_id" json:"-"`
SiteID int64 `db:"site_id" json:"-"`
UserID int64 `db:"user_id" json:"-"`
Name string `db:"name" json:"name"`
Token string `db:"token" json:"-"`
Permissions zint.Bitflag64 `db:"permissions" json:"permissions"`
CreatedAt time.Time `db:"created_at" json:"-"`
LastUsedAt *time.Time `db:"last_used_at" json:"-"`
}
type PermissionFlag struct {
Label, Help string
Flag zint.Bitflag64
}
// PermissionFlags returns a list of all flags we know for the Permissions settings.
func (t APIToken) PermissionFlags(only ...zint.Bitflag64) []PermissionFlag {
if len(only) > 1 {
for _, o := range only[1:] {
only[0] |= o
}
}
all := []PermissionFlag{
{
Label: "Record pageviews",
Help: "Record pageviews with /api/v0/count",
Flag: APIPermCount,
},
{
Label: "Read statistics",
Help: "Get statistics out of GoatCounter",
Flag: APIPermStats,
},
{
Label: "Export",
Help: "Export data with /api/v0/export",
Flag: APIPermExport,
},
{
Label: "Read sites",
Flag: APIPermSiteRead,
},
{
Label: "Create sites",
Flag: APIPermSiteCreate,
},
{
Label: "Update sites",
Flag: APIPermSiteUpdate,
},
}
if len(only) == 0 {
return all
}
filter := make([]PermissionFlag, 0, len(all))
for _, a := range all {
if !only[0].Has(a.Flag) {
continue
}
filter = append(filter, a)
}
return filter
}
func (t APIToken) FormatPermissions() string {
var all []string
if t.Permissions.Has(APIPermCount) {
all = append(all, "count")
}
if t.Permissions.Has(APIPermExport) {
all = append(all, "export")
}
if t.Permissions.Has(APIPermSiteRead) {
all = append(all, "site-read")
}
if t.Permissions.Has(APIPermSiteCreate) {
all = append(all, "site-create")
}
if t.Permissions.Has(APIPermSiteUpdate) {
all = append(all, "site-update")
}
return "'" + strings.Join(all, "', '") + "'"
}
// Defaults sets fields to default values, unless they're already set.
func (t *APIToken) Defaults(ctx context.Context) {
t.SiteID = MustGetSite(ctx).ID
t.Token = zcrypto.Secret256()
t.CreatedAt = ztime.Now()
}
func (t *APIToken) Validate(ctx context.Context) error {
v := NewValidate(ctx)
v.Required("name", t.Name)
v.Required("site_id", t.SiteID)
v.Required("user_id", t.SiteID)
v.Required("token", t.Token)
if t.Permissions == 1 {
v.Append("permissions", "must set at least one permission")
}
return v.ErrorOrNil()
}
// Insert a new row.
func (t *APIToken) Insert(ctx context.Context) error {
if t.ID > 0 {
return errors.New("ID > 0")
}
t.Defaults(ctx)
err := t.Validate(ctx)
if err != nil {
return err
}
t.ID, err = zdb.InsertID(ctx, "api_token_id",
`insert into api_tokens (site_id, user_id, name, token, permissions, created_at) values (?)`,
[]any{t.SiteID, GetUser(ctx).ID, t.Name, t.Token, t.Permissions, t.CreatedAt})
return errors.Wrap(err, "APIToken.Insert")
}
// Update the name and permissions.
func (t *APIToken) Update(ctx context.Context) error {
if t.ID == 0 {
return errors.New("ID == 0")
}
t.Defaults(ctx)
err := t.Validate(ctx)
if err != nil {
return err
}
err = zdb.Exec(ctx, `update api_tokens set name=?, permissions=? where api_token_id=?`,
t.Name, t.Permissions, t.ID)
return errors.Wrap(err, "APIToken.Update")
}
// UpdateLastUsed sets the last used time to the current time.
func (t *APIToken) UpdateLastUsed(ctx context.Context) error {
if t.ID == 0 {
return errors.New("ID == 0")
}
t.Defaults(ctx)
err := t.Validate(ctx)
if err != nil {
return err
}
t.LastUsedAt = ztype.Ptr(ztime.Now())
err = zdb.Exec(ctx, `update api_tokens set last_used_at=? where api_token_id=?`,
t.LastUsedAt, t.ID)
return errors.Wrap(err, "APIToken.UpdateLastUsed")
}
func (t *APIToken) ByID(ctx context.Context, id int64) error {
return errors.Wrapf(zdb.Get(ctx, t, `/* APIToken.ByID */
select * from api_tokens where api_token_id=$1 and site_id=$2`,
id, MustGetSite(ctx).ID), "APIToken.ByID %d", id)
}
func (t *APIToken) ByToken(ctx context.Context, token string) error {
return errors.Wrap(zdb.Get(ctx, t,
`/* APIToken.ByID */ select * from api_tokens where token=$1 and site_id=$2`,
token, MustGetSite(ctx).ID), "APIToken.ByToken")
}
func (t *APIToken) Delete(ctx context.Context) error {
err := zdb.Exec(ctx,
`/* APIToken.Delete */ delete from api_tokens where api_token_id=$1 and site_id=$2`,
t.ID, MustGetSite(ctx).ID)
return errors.Wrapf(err, "APIToken.Delete %d", t.ID)
}
type APITokens []APIToken
func (t *APITokens) List(ctx context.Context) error {
return errors.Wrap(zdb.Select(ctx, t,
`select * from api_tokens where site_id=$1 and user_id=$2`,
MustGetSite(ctx).ID, GetUser(ctx).ID), "APITokens.List")
}
// Find API tokens: by ID if ident is a number, or by token if it's not.
func (t *APITokens) Find(ctx context.Context, ident []string) error {
ids, strs := splitIntStr(ident)
err := zdb.Select(ctx, t, `select * from api_tokens where
{{:ids api_token_id in (:ids) or}}
{{:strs! 0=1}}
{{:strs token in (:strs)}}`,
map[string]any{"ids": ids, "strs": strs})
return errors.Wrap(err, "APITokens.Find")
}
// IDs gets a list of all IDs for these API tokens.
func (t *APITokens) IDs() []int64 {
ids := make([]int64, 0, len(*t))
for _, tt := range *t {
ids = append(ids, tt.ID)
}
return ids
}
// Delete all API tokens in this selection.
func (t *APITokens) Delete(ctx context.Context, _ bool) error {
err := zdb.TX(ctx, func(ctx context.Context) error {
for _, tt := range *t {
err := tt.Delete(WithSite(ctx, &Site{ID: tt.SiteID}))
if err != nil {
return err
}
}
return nil
})
return errors.Wrap(err, "Users.Delete")
}