feat[Go] implement /connectors/google/oauth (#15584)

### What problem does this PR solve?

The following API is available in go

> /api/v1/connectors/google/oauth/web/start POST
> /api/v1/connectors/gmail/oauth/web/callback GET
> /api/v1/connectors/google-drive/oauth/web/callback GET
> /api/v1/connectors/google/oauth/web/result POST


### Type of change

- [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
Haruko386
2026-06-03 20:08:55 +08:00
committed by GitHub
parent b946df8ba2
commit df55880b44
5 changed files with 710 additions and 0 deletions

View File

@@ -39,6 +39,9 @@ type connectorServiceIface interface {
RebuildConnector(connectorID, userID, kbID string) (bool, common.ErrorCode, error)
TestConnector(connectorID, userID string) error
UpdateConnector(connectorID, userID string, req *service.UpdateConnectorRequest) (*entity.Connector, common.ErrorCode, error)
StartGoogleWebOAuth(userID, source string, req *service.StartGoogleWebOAuthRequest) (*service.StartGoogleWebOAuthResponse, common.ErrorCode, error)
GoogleWebOAuthCallback(source, stateID, oauthError, errorDescription, code string) string
PollGoogleWebOAuthResult(userID, source string, req *service.PollGoogleWebOAuthResultRequest) (*service.PollGoogleWebOAuthResultResponse, common.ErrorCode, error)
}
// ConnectorHandler connector handler
@@ -418,3 +421,86 @@ func (h *ConnectorHandler) RebuildConnector(c *gin.Context) {
"message": "success",
})
}
func (h *ConnectorHandler) StartGoogleWebOAuth(c *gin.Context) {
user, errorCode, errorMessage := GetUser(c)
if errorCode != common.CodeSuccess {
jsonError(c, errorCode, errorMessage)
return
}
var req service.StartGoogleWebOAuthRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"code": common.CodeBadRequest,
"data": nil,
"message": err.Error(),
})
return
}
data, code, err := h.connectorService.StartGoogleWebOAuth(user.ID, c.DefaultQuery("type", "google-drive"), &req)
if err != nil {
jsonError(c, code, err.Error())
return
}
c.JSON(http.StatusOK, gin.H{
"code": common.CodeSuccess,
"data": data,
"message": "success",
})
}
func (h *ConnectorHandler) PollGoogleWebOAuthResult(c *gin.Context) {
user, errorCode, errorMessage := GetUser(c)
if errorCode != common.CodeSuccess {
jsonError(c, errorCode, errorMessage)
return
}
var req service.PollGoogleWebOAuthResultRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"code": common.CodeBadRequest,
"data": nil,
"message": err.Error(),
})
return
}
data, code, err := h.connectorService.PollGoogleWebOAuthResult(user.ID, c.Query("type"), &req)
if err != nil {
jsonError(c, code, err.Error())
return
}
c.JSON(http.StatusOK, gin.H{
"code": common.CodeSuccess,
"data": data,
"message": "success",
})
}
func (h *ConnectorHandler) GoogleWebOAuthCallback(c *gin.Context) {
h.googleWebOAuthCallback(c, c.Param("source"))
}
func (h *ConnectorHandler) GoogleDriveWebOAuthCallback(c *gin.Context) {
h.googleWebOAuthCallback(c, "google-drive")
}
func (h *ConnectorHandler) GmailWebOAuthCallback(c *gin.Context) {
h.googleWebOAuthCallback(c, "gmail")
}
func (h *ConnectorHandler) googleWebOAuthCallback(c *gin.Context, source string) {
html := h.connectorService.GoogleWebOAuthCallback(
source,
c.Query("state"),
c.Query("error"),
c.Query("error_description"),
c.Query("code"),
)
c.Data(http.StatusOK, "text/html; charset=utf-8", []byte(html))
}

View File

@@ -52,6 +52,24 @@ func (s fakeConnectorService) UpdateConnector(string, string, *service.UpdateCon
return s.connector, common.CodeSuccess, nil
}
func (s fakeConnectorService) StartGoogleWebOAuth(string, string, *service.StartGoogleWebOAuthRequest) (*service.StartGoogleWebOAuthResponse, common.ErrorCode, error) {
if s.err != nil {
return nil, s.code, s.err
}
return &service.StartGoogleWebOAuthResponse{}, common.CodeSuccess, nil
}
func (s fakeConnectorService) GoogleWebOAuthCallback(string, string, string, string, string) string {
return ""
}
func (s fakeConnectorService) PollGoogleWebOAuthResult(string, string, *service.PollGoogleWebOAuthResultRequest) (*service.PollGoogleWebOAuthResultResponse, common.ErrorCode, error) {
if s.err != nil {
return nil, s.code, s.err
}
return &service.PollGoogleWebOAuthResultResponse{}, common.CodeSuccess, nil
}
func (s fakeConnectorService) ListLog(string, string, int, int) ([]*entity.ConnectorSyncLog, int64, common.ErrorCode, error) {
if s.err != nil {
return nil, 0, s.code, s.err