-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.go
57 lines (46 loc) · 1.18 KB
/
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
package auth
import (
"context"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"github.com/acoshift/pgsql/pgctx"
)
const tokenPrefix = "deploys-api."
func generateBase64RandomString(s int) string {
b := make([]byte, s)
_, err := rand.Read(b[:])
if err != nil {
panic(err)
}
return base64.URLEncoding.EncodeToString(b[:])
}
func generateToken() string {
return tokenPrefix + generateBase64RandomString(32)
}
func generateState() string {
return generateBase64RandomString(24)
}
func generateCode() string {
return generateBase64RandomString(32)
}
func generateSessionID() string {
return generateBase64RandomString(32)
}
func hashToken(token string) string {
h := sha256.New()
h.Write([]byte(token))
// h.Write(secret)
return base64.RawURLEncoding.EncodeToString(h.Sum(nil))
}
func insertToken(ctx context.Context, hashedToken string, email string) error {
_, err := pgctx.Exec(ctx, `
insert into user_tokens (token, email, expires_at)
values ($1, $2, now() + interval '7 days')
`, hashedToken, email)
return err
}
func deleteToken(ctx context.Context, token string) error {
_, err := pgctx.Exec(ctx, `delete from user_tokens where token = $1`, token)
return err
}