Files
nixcn-cms/pkgs/authcode/authcode.go
Asai Neko 3d685b5a86
All checks were successful
Build Backend (NixCN CMS) TeamCity build finished
Build Frontend (NixCN CMS) TeamCity build finished
Add hot reload for backend
Signed-off-by: Asai Neko <sugar@sne.moe>
2026-01-01 20:22:55 +08:00

54 lines
831 B
Go

package authcode
import (
"crypto/rand"
"encoding/base64"
"sync"
"time"
"github.com/spf13/viper"
)
type Token struct {
Email string
ExpiresAt time.Time
}
var (
store = sync.Map{}
)
// Generate magic token
func NewAuthCode(email string) (string, error) {
b := make([]byte, 32)
if _, err := rand.Read(b); err != nil {
return "", err
}
token := base64.RawURLEncoding.EncodeToString(b)
store.Store(token, Token{
Email: email,
ExpiresAt: time.Now().Add(viper.GetDuration("ttl.magic_link_ttl")),
})
return token, nil
}
// Verify magic token
func VerifyAuthCode(token string) (string, bool) {
val, ok := store.Load(token)
if !ok {
return "", false
}
t := val.(Token)
if time.Now().After(t.ExpiresAt) {
store.Delete(token)
return "", false
}
store.Delete(token)
return t.Email, true
}