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" "time" ) const ( oauthStateCookieName = "oauth_state" oauthStateKey = "OAuthState:%s" ) // 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 (s *service) LoginHandler() gin.HandlerFunc { return func(c *gin.Context) { state := utils.RandomString(64) key := utils.RandomString(16) err := s.cache.Get().Set(context.Background(), fmt.Sprintf(oauthStateKey, key), state, 15*time.Minute).Err() if err != nil { e.Pong[any](c, e.RedisError, nil) return } c.SetSameSite(http.SameSiteStrictMode) c.SetCookie(oauthStateCookieName, key, 15*60, "/", "", false, true) url := s.conf.AuthCodeURL(state) e.Pong(c, e.Success, url) } }