arimelody.me/admin/admin.go

56 lines
1.1 KiB
Go
Raw Normal View History

package admin
import (
"fmt"
"math/rand"
"os"
"time"
)
type (
Session struct {
Token string
UserID string
Expires time.Time
}
)
const TOKEN_LENGTH = 64
const TOKEN_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
var ADMIN_BYPASS = func() bool {
if os.Getenv("ARIMELODY_ADMIN_BYPASS") == "true" {
fmt.Println("WARNING: Admin login is currently BYPASSED by ARIMELODY_ADMIN_BYPASS.")
return true
}
return false
}()
var ADMIN_ID_DISCORD = func() string {
envvar := os.Getenv("DISCORD_ADMIN_ID")
if envvar == "" {
fmt.Printf("DISCORD_ADMIN_ID was not provided. Admin login will be unavailable.\n")
}
return envvar
}()
var sessions []*Session
func createSession(username string, expires time.Time) Session {
return Session{
Token: string(generateToken()),
UserID: username,
Expires: expires,
}
}
func generateToken() string {
var token []byte
for i := 0; i < TOKEN_LENGTH; i++ {
token = append(token, TOKEN_CHARS[rand.Intn(len(TOKEN_CHARS))])
}
return string(token)
}