/
store.go
46 lines (37 loc) · 991 Bytes
/
store.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
package token
import (
"context"
"fmt"
"github.com/jmoiron/sqlx"
"github.com/polldo/govod/database"
)
// Create inserts a new token.
func Create(ctx context.Context, db sqlx.ExtContext, token Token) error {
const q = `
INSERT INTO tokens
(hash, user_id, expiry, scope)
VALUES
(:hash, :user_id, :expiry, :scope)`
if err := database.NamedExecContext(ctx, db, q, token); err != nil {
return fmt.Errorf("inserting token: %w", err)
}
return nil
}
// DeleteByUser drops the token associated to the passed user with a
// specific scope, if any.
func DeleteByUser(ctx context.Context, db sqlx.ExtContext, userID string, scope string) error {
data := struct {
UserID string `db:"user_id"`
Scope string `db:"scope"`
}{
UserID: userID,
Scope: scope,
}
const q = `
DELETE FROM tokens
WHERE user_id = :user_id AND scope = :scope`
if err := database.NamedExecContext(ctx, db, q, data); err != nil {
return fmt.Errorf("deleting token: %w", err)
}
return nil
}