arimelody.me/admin/admin.go

59 lines
1.2 KiB
Go
Raw Normal View History

package admin
import (
"fmt"
"math/rand"
2024-09-03 07:07:45 +00:00
"os"
"time"
2024-09-03 07:07:45 +00:00
"arimelody-web/global"
)
type (
Session struct {
Token string
UserID string
Expires time.Time
}
)
const TOKEN_LENGTH = 64
const TOKEN_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
var ADMIN_BYPASS = func() bool {
if global.Args["adminBypass"] == "true" {
fmt.Println("WARN: Admin login is currently BYPASSED. (-adminBypass)")
return true
}
return false
}()
var ADMIN_ID_DISCORD = func() string {
2024-11-10 05:34:04 +00:00
id := os.Getenv("DISCORD_ADMIN_ID")
if id == "" { id = global.Config.Discord.AdminID }
if id == "" {
2024-11-10 05:34:04 +00:00
fmt.Printf("WARN: discord.admin_id not provided. Admin login will be unavailable.\n")
}
return id
}()
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)
}