-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.go
43 lines (35 loc) · 978 Bytes
/
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
package token
import (
"crypto/rand"
"crypto/sha256"
"encoding/base32"
"time"
)
const (
ActivationToken = "activation"
RecoveryToken = "recovery"
)
// Token models tokens to be sent to users for
// activation and recovery purposes.
type Token struct {
Hash []byte `json:"-" db:"hash"`
UserID string `json:"userId" db:"user_id"`
Expiry time.Time `json:"expiry" db:"expiry"`
Scope string `json:"scope" db:"scope"`
}
// GenToken generates a new random token for a user.
func GenToken(userID string, ttl time.Duration, scope string) (string, Token, error) {
token := Token{
UserID: userID,
Expiry: time.Now().UTC().Add(ttl),
Scope: scope,
}
randomBytes := make([]byte, 16)
if _, err := rand.Read(randomBytes); err != nil {
return "", Token{}, err
}
plaintext := base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(randomBytes)
hash := sha256.Sum256([]byte(plaintext))
token.Hash = hash[:]
return plaintext, token, nil
}