2024-08-13 19:39:33 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2024-08-13 20:00:07 +00:00
|
|
|
"context"
|
2024-08-13 19:39:33 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/base32"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
ScopeAuthentication = "authentication"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Token is the type for authentication tokens
|
|
|
|
type Token struct {
|
|
|
|
PlainText string `json:"token"`
|
|
|
|
UserID int64 `json:"-"`
|
|
|
|
Hash []byte `json:"-"`
|
|
|
|
Expiry time.Time `json:"expiry"`
|
|
|
|
Scope string `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateToken Generates a token that lasts for ttl, and returns it
|
|
|
|
func GenerateToken(userID int, ttl time.Duration, scope string) (*Token, error) {
|
|
|
|
token := &Token{
|
|
|
|
UserID: int64(userID),
|
|
|
|
Expiry: time.Now().Add(ttl),
|
|
|
|
Scope: scope,
|
|
|
|
}
|
|
|
|
|
|
|
|
randomBytes := make([]byte, 16)
|
|
|
|
_, err := rand.Read(randomBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
token.PlainText = base32.StdEncoding.WithPadding((base32.NoPadding)).
|
|
|
|
EncodeToString((randomBytes))
|
|
|
|
hash := sha256.Sum256([]byte(token.PlainText))
|
|
|
|
token.Hash = hash[:]
|
|
|
|
return token, nil
|
|
|
|
}
|
2024-08-13 20:00:07 +00:00
|
|
|
|
|
|
|
func (m *DBModel) InsertToken(t *Token, u User) error {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2024-08-13 20:46:26 +00:00
|
|
|
// delete existing tokens
|
|
|
|
stmt := `DELETE FROM tokens WHERE user_id = ?`
|
|
|
|
|
|
|
|
_, err := m.DB.ExecContext(ctx, stmt, u.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt = `INSERT INTO tokens
|
2024-08-13 20:00:07 +00:00
|
|
|
(user_id, name, email, token_hash, created_at, updated_at)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?)`
|
|
|
|
|
2024-08-13 20:46:26 +00:00
|
|
|
_, err = m.DB.ExecContext(ctx, stmt,
|
2024-08-13 20:00:07 +00:00
|
|
|
u.ID,
|
|
|
|
u.LastName,
|
|
|
|
u.Email,
|
|
|
|
t.Hash,
|
|
|
|
time.Now(),
|
|
|
|
time.Now(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|