Skip to content

Commit

Permalink
Merge b499b73 into 9b090ec
Browse files Browse the repository at this point in the history
  • Loading branch information
rubicae committed Feb 14, 2020
2 parents 9b090ec + b499b73 commit 9363955
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions db/sqldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"fmt"
"log"
"math/rand"
"crypto/rand"
"net/url"
"strings"
"time"
Expand Down Expand Up @@ -52,10 +52,13 @@ func InitSQLDatabase(cfg Config) (*SQLDatabase, error) {
// TOKEN DB FUNCTIONS

// randToken generates a random token.
func randToken() string {
func randToken() (string, error) {
b := make([]byte, 8)
rand.Read(b)
return fmt.Sprintf("%x", b)
_, err := rand.Read(b)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", b), nil
}

// UseToken sets the `used` flag on a particular email validation token to
Expand All @@ -80,13 +83,17 @@ func (db *SQLDatabase) GetTokenByDomain(domain string) (string, error) {
// PutToken generates and inserts a token into the database for a particular
// domain, and returns the resulting token row.
func (db *SQLDatabase) PutToken(domain string) (models.Token, error) {
tokenId, err := randToken()
if err != nil {
return models.Token{}, err
}
token := models.Token{
Domain: domain,
Token: randToken(),
Token: tokenId,
Expires: time.Now().Add(time.Duration(time.Hour * 72)),
Used: false,
}
_, err := db.conn.Exec("INSERT INTO tokens(domain, token, expires) VALUES($1, $2, $3) "+
_, err = db.conn.Exec("INSERT INTO tokens(domain, token, expires) VALUES($1, $2, $3) "+
"ON CONFLICT (domain) DO UPDATE SET token=$2, expires=$3, used=FALSE",
domain, token.Token, token.Expires.UTC().Format(sqlTimeFormat))
if err != nil {
Expand Down

0 comments on commit 9363955

Please sign in to comment.