/*
* Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
*
* This file is licensed under AGPL-3.0-or-later, see NOTICE and LICENSE for
* more information.
*/
package handlers
import (
"crypto/rand"
"encoding/base64"
"net/http"
"tixe/auth"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
)
func AuthLogin(auth *auth.Auth) gin.HandlerFunc {
return func(c *gin.Context) {
state, err := randomState()
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
session := sessions.Default(c)
session.Set("state", state)
if err := session.Save(); err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
c.Redirect(http.StatusTemporaryRedirect, auth.AuthCodeURL(state))
}
}
func randomState() (string, error) {
buf := make([]byte, 32)
_, err := rand.Read(buf)
if err != nil {
return "", nil
}
return base64.StdEncoding.EncodeToString(buf), nil
}