52 lines
1.2 KiB
Go
52 lines
1.2 KiB
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"
|
|
)
|
|
|
|
type LoginResponse struct {
|
|
Url string `json:"url"`
|
|
Cookie struct {
|
|
Name string `json:"name"`
|
|
Value string `json:"value"`
|
|
Live int `json:"live"`
|
|
} `json:"cookie"`
|
|
}
|
|
|
|
// LoginHandler
|
|
// @Summary Login with OAuth2
|
|
// @Description Get OAuth2 Login URL
|
|
// @Tags oauth
|
|
// @Produce json
|
|
// @Response 200 {object} e.Response[oauth.LoginResponse] "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
|
|
}
|
|
|
|
e.Pong(c, e.Success, LoginResponse{
|
|
Url: h.conf.AuthCodeURL(state),
|
|
Cookie: struct {
|
|
Name string `json:"name"`
|
|
Value string `json:"value"`
|
|
Live int `json:"live"`
|
|
}{
|
|
Name: oauthStateCookieName,
|
|
Value: key,
|
|
Live: int(oauthStateLiveness.Seconds()),
|
|
},
|
|
})
|
|
}
|
|
}
|