package handlers import ( "net/http" "strings" "github.com/dusiburg/kotosoft-id/internal/services" "github.com/gin-gonic/gin" ) type OAuthHandler struct { svc *services.Service frontendURL string } func NewOAuthHandler(svc *services.Service, frontendURL string) *OAuthHandler { return &OAuthHandler{svc: svc, frontendURL: frontendURL} } func (h *OAuthHandler) Authorize(c *gin.Context) { token, err := h.svc.CreateOAuthSession( c.Request.Context(), c.Query("client_id"), c.Query("redirect_uri"), c.Query("scope"), c.Query("state"), c.Query("code_challenge"), c.Query("code_challenge_method"), ) if err != nil { oauthErrorJSON(c, err) return } c.Redirect(http.StatusFound, h.frontendURL+"/oauth?session="+token) } func (h *OAuthHandler) GetSession(c *gin.Context) { sess, err := h.svc.GetOAuthSession(c.Request.Context(), c.Param("token")) if err != nil { oauthErrorJSON(c, services.ErrInvalidGrant) return } c.JSON(http.StatusOK, sess) } func (h *OAuthHandler) SessionLogin(c *gin.Context) { var input struct { Session string `json:"session" binding:"required"` Email string `json:"email" binding:"required"` Password string `json:"password" binding:"required"` } if err := c.ShouldBindJSON(&input); err != nil { oauthErrorJSON(c, services.ErrInvalidRequest) return } redirectURL, err := h.svc.SessionLogin(c.Request.Context(), input.Session, input.Email, input.Password) if err != nil { oauthErrorJSON(c, err) return } if redirectURL != "" { c.JSON(http.StatusOK, gin.H{"redirect_url": redirectURL}) return } c.JSON(http.StatusOK, gin.H{}) } func (h *OAuthHandler) SessionAttach(c *gin.Context) { var input struct { Session string `json:"session" binding:"required"` } if err := c.ShouldBindJSON(&input); err != nil { oauthErrorJSON(c, services.ErrInvalidRequest) return } accessToken, ok := bearerToken(c) if !ok { oauthErrorJSON(c, services.ErrInvalidClient) return } redirectURL, err := h.svc.SessionAttach(c.Request.Context(), input.Session, accessToken) if err != nil { oauthErrorJSON(c, err) return } if redirectURL != "" { c.JSON(http.StatusOK, gin.H{"redirect_url": redirectURL}) return } c.Status(http.StatusOK) } func (h *OAuthHandler) SessionApprove(c *gin.Context) { var input struct { Session string `json:"session" binding:"required"` Allow bool `json:"allow"` } if err := c.ShouldBindJSON(&input); err != nil { oauthErrorJSON(c, services.ErrInvalidRequest) return } if !input.Allow { redirectURL, err := h.svc.SessionDeny(c.Request.Context(), input.Session) if err != nil { oauthErrorJSON(c, err) return } c.JSON(http.StatusOK, gin.H{"redirect_url": redirectURL}) return } redirectURL, err := h.svc.SessionApprove(c.Request.Context(), input.Session) if err != nil { oauthErrorJSON(c, err) return } c.JSON(http.StatusOK, gin.H{"redirect_url": redirectURL}) } func (h *OAuthHandler) Token(c *gin.Context) { auth := h.extractClientAuth(c) switch c.PostForm("grant_type") { case "authorization_code": pair, err := h.svc.ExchangeCode( c.Request.Context(), auth, c.PostForm("code"), c.PostForm("redirect_uri"), c.PostForm("code_verifier"), ) if err != nil { oauthErrorJSON(c, err) return } pair.User = nil c.JSON(http.StatusOK, pair) case "refresh_token": pair, err := h.svc.ExchangeRefreshToken(c.Request.Context(), auth, c.PostForm("refresh_token")) if err != nil { oauthErrorJSON(c, err) return } pair.User = nil c.JSON(http.StatusOK, pair) default: oauthErrorJSON(c, services.ErrUnsupportedGrantType) } } func (h *OAuthHandler) Revoke(c *gin.Context) { _ = h.svc.RevokeToken(c.Request.Context(), h.extractClientAuth(c), c.PostForm("token")) c.Status(http.StatusOK) } func (h *OAuthHandler) extractClientAuth(c *gin.Context) services.ClientAuth { if id, secret, ok := c.Request.BasicAuth(); ok { return services.ClientAuth{ClientID: id, ClientSecret: secret} } return services.ClientAuth{ ClientID: c.PostForm("client_id"), ClientSecret: c.PostForm("client_secret"), } } func bearerToken(c *gin.Context) (string, bool) { return strings.CutPrefix(c.GetHeader("Authorization"), "Bearer ") } func oauthErrorJSON(c *gin.Context, err error) { code, desc, status := oauthErrorDetails(err) if code == "invalid_client" { c.Header("WWW-Authenticate", `Basic realm="oauth"`) } c.JSON(status, gin.H{"error": code, "error_description": desc}) } func oauthErrorDetails(err error) (code, desc string, status int) { switch err { case services.ErrInvalidRequest: return "invalid_request", "Invalid request parameters", http.StatusBadRequest case services.ErrUnauthorizedClient: return "unauthorized_client", "Client not authorized", http.StatusBadRequest case services.ErrAccessDenied: return "access_denied", "Access denied", http.StatusForbidden case services.ErrInvalidClient: return "invalid_client", "Invalid client credentials", http.StatusUnauthorized case services.ErrInvalidGrant: return "invalid_grant", "Invalid or expired grant", http.StatusBadRequest case services.ErrUnsupportedGrantType: return "unsupported_grant_type", "Grant type not supported", http.StatusBadRequest default: return "server_error", "An unexpected error occurred", http.StatusInternalServerError } }