feat(channels): align email oauth authorization

This commit is contained in:
2026-06-06 18:53:04 +08:00
parent 5b143b6da7
commit 7c0a67a850
9 changed files with 247 additions and 8 deletions
@@ -21,6 +21,7 @@ import (
"github.com/gochat/gochat/internal/repository"
"github.com/gochat/gochat/internal/service"
applogger "github.com/gochat/gochat/pkg/logger"
"github.com/gochat/gochat/pkg/response"
)
// GoogleChannelHandler handles Google Chat channel management.
@@ -80,6 +81,34 @@ func (h *GoogleChannelHandler) Authorization(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"authorization_url": authURL})
}
// ChatwootAuthorization creates a Google email OAuth authorization URL.
// POST /api/v1/accounts/:account_id/google/authorization
func (h *GoogleChannelHandler) ChatwootAuthorization(c *gin.Context) {
accountID, err := parseUintParam(c, "account_id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
redirectURL, err := buildChatwootEmailOAuthAuthorizationURL(accountID, chatwootEmailOAuthAuthorizationConfig{
ClientIDEnv: "GOOGLE_OAUTH_CLIENT_ID",
ClientSecretEnv: "GOOGLE_OAUTH_CLIENT_SECRET",
AuthorizeURL: "https://accounts.google.com/o/oauth2/auth",
RedirectPath: "/google/callback",
Scope: "email profile https://mail.google.com/",
ExtraParams: map[string]string{
"prompt": "consent",
"access_type": "offline",
},
})
if err != nil {
applogger.L().Errorf("Failed to build Google authorization URL: %v", err)
c.JSON(http.StatusUnprocessableEntity, gin.H{"success": false})
return
}
c.JSON(http.StatusOK, gin.H{"success": true, "url": redirectURL})
}
// === OAuth Callback ===
// GoogleOAuthCallbackRequest is the DTO for Google OAuth callback.