2024-08-01 23:53:19 +00:00
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
2024-09-03 07:07:45 +00:00
|
|
|
"os"
|
2024-08-01 23:53:19 +00:00
|
|
|
"time"
|
2024-08-31 01:56:06 +00:00
|
|
|
|
2024-09-03 07:07:45 +00:00
|
|
|
"arimelody-web/global"
|
2024-08-01 23:53:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Session struct {
|
2024-08-02 21:48:26 +00:00
|
|
|
Token string
|
|
|
|
UserID string
|
|
|
|
Expires time.Time
|
2024-08-01 23:53:19 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const TOKEN_LENGTH = 64
|
|
|
|
const TOKEN_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
|
|
|
2024-08-05 15:49:49 +00:00
|
|
|
var ADMIN_BYPASS = func() bool {
|
2024-08-31 01:56:06 +00:00
|
|
|
if global.Args["adminBypass"] == "true" {
|
|
|
|
fmt.Println("WARN: Admin login is currently BYPASSED. (-adminBypass)")
|
2024-08-05 15:49:49 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}()
|
|
|
|
|
2024-08-01 23:53:19 +00:00
|
|
|
var ADMIN_ID_DISCORD = func() string {
|
2024-09-03 07:07:45 +00:00
|
|
|
id := os.Getenv("DISCORD_ADMIN")
|
2024-08-31 01:56:06 +00:00
|
|
|
if id == "" {
|
2024-09-03 07:07:45 +00:00
|
|
|
fmt.Printf("WARN: Discord admin ID (DISCORD_ADMIN) was not provided. Admin login will be unavailable.\n")
|
2024-08-01 23:53:19 +00:00
|
|
|
}
|
2024-08-31 01:56:06 +00:00
|
|
|
return id
|
2024-08-01 23:53:19 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
var sessions []*Session
|
|
|
|
|
2024-08-02 21:48:26 +00:00
|
|
|
func createSession(username string, expires time.Time) Session {
|
2024-08-01 23:53:19 +00:00
|
|
|
return Session{
|
|
|
|
Token: string(generateToken()),
|
2024-08-02 21:48:26 +00:00
|
|
|
UserID: username,
|
|
|
|
Expires: expires,
|
2024-08-01 23:53:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|