forked from G-Node/gin-auth
/
access_token.go
88 lines (71 loc) · 2.58 KB
/
access_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
// Copyright (c) 2016, German Neuroinformatics Node (G-Node),
// Adrian Stoewer <adrian.stoewer@rz.ifi.lmu.de>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted under the terms of the BSD License. See
// LICENSE file in the root of the Project.
package data
import (
"database/sql"
"time"
"github.com/G-Node/gin-auth/conf"
"github.com/G-Node/gin-auth/util"
)
// AccessToken represents an OAuth access token
type AccessToken struct {
Token string // This is just a random string not the JWT token
Scope util.StringSet
Expires time.Time
ClientUUID string
AccountUUID sql.NullString
CreatedAt time.Time
UpdatedAt time.Time
}
// ListAccessTokens returns all access tokens sorted by creation time.
func ListAccessTokens() []AccessToken {
const q = `SELECT * FROM AccessTokens WHERE expires > now() ORDER BY createdAt`
accessTokens := make([]AccessToken, 0)
err := database.Select(&accessTokens, q)
if err != nil {
panic(err)
}
return accessTokens
}
// GetAccessToken returns a access token with a given token.
// Returns false if no such access token exists.
func GetAccessToken(token string) (*AccessToken, bool) {
const q = `SELECT * FROM AccessTokens WHERE token=$1 AND expires > now()`
accessToken := &AccessToken{}
err := database.Get(accessToken, q, token)
if err != nil && err != sql.ErrNoRows {
panic(err)
}
return accessToken, err == nil
}
// Create stores a new access token in the database.
// If the token is empty a random token will be generated.
func (tok *AccessToken) Create() error {
const q = `INSERT INTO AccessTokens (token, scope, expires, clientUUID, accountUUID, createdAt, updatedAt)
VALUES ($1, $2, $3, $4, $5, now(), now())
RETURNING *`
tok.Expires = time.Now().Add(conf.GetServerConfig().TokenLifeTime)
if tok.Token == "" {
tok.Token = util.RandomToken()
}
return database.Get(tok, q, tok.Token, tok.Scope, tok.Expires, tok.ClientUUID, tok.AccountUUID)
}
// UpdateExpirationTime updates the expiration time and stores
// the new time in the database.
func (tok *AccessToken) UpdateExpirationTime() error {
const q = `UPDATE AccessTokens SET (expires, updatedAt) = ($1, now())
WHERE token=$2
RETURNING *`
return database.Get(tok, q, time.Now().Add(conf.GetServerConfig().TokenLifeTime), tok.Token)
}
// Delete removes an access token from the database.
func (tok *AccessToken) Delete() error {
const q = `DELETE FROM AccessTokens WHERE token=$1`
_, err := database.Exec(q, tok.Token)
return err
}