feat: oauth/login: cookie is returned in response

This commit is contained in:
Paul Pan 2024-03-16 00:56:46 +08:00
parent 99aec47c76
commit bbe525a774
Signed by: Paul
GPG Key ID: D639BDF5BA578AF4
2 changed files with 22 additions and 11 deletions

View File

@ -17,8 +17,6 @@ import (
// @Produce json // @Produce json
// @Router /oauth/callback [get] // @Router /oauth/callback [get]
func (h *handler) CallbackHandler() gin.HandlerFunc { func (h *handler) CallbackHandler() gin.HandlerFunc {
// TODO: we are returning e.Response directly here, we should redirect to a trampoline page, passing the response as query string
return func(c *gin.Context) { return func(c *gin.Context) {
// Extract key from cookie // Extract key from cookie
key, err := c.Cookie(oauthStateCookieName) key, err := c.Cookie(oauthStateCookieName)
@ -37,7 +35,6 @@ func (h *handler) CallbackHandler() gin.HandlerFunc {
// Whether state is valid, delete it // Whether state is valid, delete it
h.cache.Get().Unlink(context.Background(), key) h.cache.Get().Unlink(context.Background(), key)
c.SetCookie(oauthStateCookieName, "", -1, "/", "", false, true)
// Verify state // Verify state
if c.Query("state") != expected { if c.Query("state") != expected {
@ -111,6 +108,5 @@ func (h *handler) CallbackHandler() gin.HandlerFunc {
// TODO: Figure out a better way to cooperate with frontend // TODO: Figure out a better way to cooperate with frontend
c.Redirect(http.StatusFound, "/login?redirect_token="+jwt) c.Redirect(http.StatusFound, "/login?redirect_token="+jwt)
// e.Pong(c, status, userApi.LoginResponse{Token: jwt, NickName: u.NickName})
} }
} }

View File

@ -6,15 +6,23 @@ import (
"git.0x7f.app/WOJ/woj-server/internal/e" "git.0x7f.app/WOJ/woj-server/internal/e"
"git.0x7f.app/WOJ/woj-server/pkg/utils" "git.0x7f.app/WOJ/woj-server/pkg/utils"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/http"
) )
type LoginResponse struct {
Url string `json:"url"`
Cookie struct {
Name string `json:"name"`
Value string `json:"value"`
Live int `json:"live"`
} `json:"cookie"`
}
// LoginHandler // LoginHandler
// @Summary Login with OAuth2 // @Summary Login with OAuth2
// @Description Get OAuth2 Login URL // @Description Get OAuth2 Login URL
// @Tags oauth // @Tags oauth
// @Produce json // @Produce json
// @Response 200 {object} e.Response[string] "random string" // @Response 200 {object} e.Response[oauth.LoginResponse] "random string"
// @Router /oauth/login [post] // @Router /oauth/login [post]
func (h *handler) LoginHandler() gin.HandlerFunc { func (h *handler) LoginHandler() gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
@ -27,10 +35,17 @@ func (h *handler) LoginHandler() gin.HandlerFunc {
return return
} }
c.SetSameSite(http.SameSiteStrictMode) e.Pong(c, e.Success, LoginResponse{
c.SetCookie(oauthStateCookieName, key, int(oauthStateLiveness.Seconds()), "/", "", false, true) Url: h.conf.AuthCodeURL(state),
Cookie: struct {
url := h.conf.AuthCodeURL(state) Name string `json:"name"`
e.Pong(c, e.Success, url) Value string `json:"value"`
Live int `json:"live"`
}{
Name: oauthStateCookieName,
Value: key,
Live: int(oauthStateLiveness.Seconds()),
},
})
} }
} }