37 lines
940 B
Go
37 lines
940 B
Go
package oauth
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"git.0x7f.app/WOJ/woj-server/internal/e"
|
|
"git.0x7f.app/WOJ/woj-server/pkg/utils"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
)
|
|
|
|
// LoginHandler
|
|
// @Summary Login with OAuth2
|
|
// @Description Get OAuth2 Login URL
|
|
// @Tags oauth
|
|
// @Produce json
|
|
// @Response 200 {object} e.Response[string] "random string"
|
|
// @Router /oauth/login [post]
|
|
func (h *handler) LoginHandler() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
state := utils.RandomString(64)
|
|
key := utils.RandomString(16)
|
|
|
|
err := h.cache.Get().Set(context.Background(), fmt.Sprintf(oauthStateKey, key), state, oauthStateLiveness).Err()
|
|
if err != nil {
|
|
e.Pong[any](c, e.RedisError, nil)
|
|
return
|
|
}
|
|
|
|
c.SetSameSite(http.SameSiteStrictMode)
|
|
c.SetCookie(oauthStateCookieName, key, int(oauthStateLiveness.Seconds()), "/", "", false, true)
|
|
|
|
url := h.conf.AuthCodeURL(state)
|
|
e.Pong(c, e.Success, url)
|
|
}
|
|
}
|