Session management utilities for generating tokens, creating, validating, and cleaning expired sessions.
// Source: https://github.com/openilink/openilink-hub | License: MIT | Copyright 2026 The OpeniLink Authors
// Provided via justclaw.it.com; keep this notice with the code.
package auth
import (
"crypto/rand"
"encoding/hex"
"errors"
"time"
"github.com/openilink/openilink-hub/internal/store"
)
var errSessionExpired = errors.New("session expired")
const sessionTTL = 7 * 24 * time.Hour
func generateToken() string {
b := make([]byte, 32)
_, _ = rand.Read(b)
return hex.EncodeToString(b)
}
func CreateSession(s store.SessionStore, userID string) (string, error) {
token := generateToken()
expiresAt := time.Now().Add(sessionTTL)
err := s.CreateSession(token, userID, expiresAt)
if err != nil {
return "", err
}
return token, nil
}
func ValidateSession(s store.SessionStore, toke
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key