47 lines
943 B
Go
47 lines
943 B
Go
package authmiddleware
|
|
|
|
import (
|
|
"log"
|
|
"time"
|
|
|
|
"git.sapphic.engineer/roleypoly/v4/discord"
|
|
"git.sapphic.engineer/roleypoly/v4/types"
|
|
"github.com/goccy/go-json"
|
|
"github.com/gofiber/fiber/v3"
|
|
)
|
|
|
|
type Session struct {
|
|
Permissions Permission
|
|
User *types.DiscordUser
|
|
AccessToken string
|
|
LastRefresh time.Time
|
|
}
|
|
|
|
func (s *Session) AsJSON() []byte {
|
|
out, err := json.Marshal(s)
|
|
if err != nil {
|
|
log.Panicln("failed to marshal session json: ", err)
|
|
}
|
|
|
|
return out
|
|
}
|
|
|
|
func SessionFromJSON(input []byte) (*Session, error) {
|
|
var s Session
|
|
err := json.Unmarshal(input, &s)
|
|
return &s, err
|
|
}
|
|
|
|
var SessionKey uint8
|
|
|
|
func New(discordClient discord.IDiscordClient, supportIDs []string, superuserIDs []string) func(fiber.Ctx) error {
|
|
am := AuthMiddleware{
|
|
Client: discordClient,
|
|
supportIDs: supportIDs,
|
|
superuserIDs: superuserIDs,
|
|
}
|
|
|
|
return am.Handle
|
|
}
|
|
|
|
var DefaultSession *Session = &Session{Permissions: PermAnonymous}
|