fix(plugin): authorization code redirect to static url (#191)

This commit is contained in:
mrh997
2025-08-07 12:24:18 +08:00
committed by GitHub
parent efbc82e8b3
commit e2b1f6e381
12 changed files with 79 additions and 63 deletions

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2025 coze-dev Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package conf
import (
"os"
"strings"
)
func GetServerHost() string {
host := os.Getenv("SERVER_HOST")
if strings.HasPrefix(host, "http://") || strings.HasPrefix(host, "https://") {
return host
}
return "https://" + host
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package utils
package encrypt
import (
"bytes"

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package utils
package encrypt
import (
"testing"

View File

@@ -16,25 +16,6 @@
package entity
import (
"strings"
)
const (
larkPluginOAuthHostName = "open.larkoffice.com"
larkOAuthHostName = "open.feishu.cn"
)
func GetOAuthProvider(tokenURL string) OAuthProvider {
if strings.Contains(tokenURL, larkPluginOAuthHostName) {
return OAuthProviderOfLarkPlugin
}
if strings.Contains(tokenURL, larkOAuthHostName) {
return OAuthProviderOfLark
}
return OAuthProviderOfStandard
}
type SortField string
const (
@@ -43,9 +24,3 @@ const (
)
type OAuthProvider string
const (
OAuthProviderOfLarkPlugin OAuthProvider = "lark_plugin"
OAuthProviderOfLark OAuthProvider = "lark"
OAuthProviderOfStandard OAuthProvider = "standard"
)

View File

@@ -25,10 +25,10 @@ import (
"gorm.io/gorm"
"github.com/coze-dev/coze-studio/backend/domain/plugin/encrypt"
"github.com/coze-dev/coze-studio/backend/domain/plugin/entity"
"github.com/coze-dev/coze-studio/backend/domain/plugin/internal/dal/model"
"github.com/coze-dev/coze-studio/backend/domain/plugin/internal/dal/query"
"github.com/coze-dev/coze-studio/backend/domain/plugin/utils"
"github.com/coze-dev/coze-studio/backend/infra/contract/idgen"
"github.com/coze-dev/coze-studio/backend/pkg/lang/slices"
)
@@ -43,19 +43,19 @@ func NewPluginOAuthAuthDAO(db *gorm.DB, idGen idgen.IDGenerator) *PluginOAuthAut
type pluginOAuthAuthPO model.PluginOauthAuth
func (p pluginOAuthAuthPO) ToDO() *entity.AuthorizationCodeInfo {
secret := os.Getenv(utils.OAuthTokenSecretEnv)
secret := os.Getenv(encrypt.OAuthTokenSecretEnv)
if secret == "" {
secret = utils.DefaultOAuthTokenSecret
secret = encrypt.DefaultOAuthTokenSecret
}
if p.RefreshToken != "" {
refreshToken, err := utils.DecryptByAES(p.RefreshToken, secret)
refreshToken, err := encrypt.DecryptByAES(p.RefreshToken, secret)
if err == nil {
p.RefreshToken = string(refreshToken)
}
}
if p.AccessToken != "" {
accessToken, err := utils.DecryptByAES(p.AccessToken, secret)
accessToken, err := encrypt.DecryptByAES(p.AccessToken, secret)
if err == nil {
p.AccessToken = string(accessToken)
}
@@ -109,20 +109,20 @@ func (p *PluginOAuthAuthDAO) Upsert(ctx context.Context, info *entity.Authorizat
}
meta := info.Meta
secret := os.Getenv(utils.OAuthTokenSecretEnv)
secret := os.Getenv(encrypt.OAuthTokenSecretEnv)
if secret == "" {
secret = utils.DefaultOAuthTokenSecret
secret = encrypt.DefaultOAuthTokenSecret
}
var accessToken, refreshToken string
if info.AccessToken != "" {
accessToken, err = utils.EncryptByAES([]byte(info.AccessToken), secret)
accessToken, err = encrypt.EncryptByAES([]byte(info.AccessToken), secret)
if err != nil {
return err
}
}
if info.RefreshToken != "" {
refreshToken, err = utils.EncryptByAES([]byte(info.RefreshToken), secret)
refreshToken, err = encrypt.EncryptByAES([]byte(info.RefreshToken), secret)
if err != nil {
return err
}

View File

@@ -29,8 +29,9 @@ import (
model "github.com/coze-dev/coze-studio/backend/api/model/crossdomain/plugin"
common "github.com/coze-dev/coze-studio/backend/api/model/plugin_develop_common"
"github.com/coze-dev/coze-studio/backend/domain/plugin/conf"
"github.com/coze-dev/coze-studio/backend/domain/plugin/encrypt"
"github.com/coze-dev/coze-studio/backend/domain/plugin/entity"
"github.com/coze-dev/coze-studio/backend/domain/plugin/utils"
"github.com/coze-dev/coze-studio/backend/pkg/errorx"
"github.com/coze-dev/coze-studio/backend/pkg/lang/conv"
"github.com/coze-dev/coze-studio/backend/pkg/lang/ptr"
@@ -438,12 +439,12 @@ func genAuthURL(info *entity.AuthorizationCodeInfo) (string, error) {
return "", fmt.Errorf("marshal state failed, err=%v", err)
}
secret := os.Getenv(utils.StateSecretEnv)
secret := os.Getenv(encrypt.StateSecretEnv)
if secret == "" {
secret = utils.DefaultStateSecret
secret = encrypt.DefaultStateSecret
}
encryptState, err := utils.EncryptByAES(stateStr, secret)
encryptState, err := encrypt.EncryptByAES(stateStr, secret)
if err != nil {
return "", fmt.Errorf("encrypt state failed, err=%v", err)
}
@@ -464,7 +465,7 @@ func getStanderOAuthConfig(config *model.OAuthAuthorizationCodeConfig) *oauth2.C
TokenURL: config.AuthorizationURL,
AuthURL: config.ClientURL,
},
RedirectURL: fmt.Sprintf("https://%s/api/oauth/authorization_code", os.Getenv("SERVER_HOST")),
RedirectURL: fmt.Sprintf("%s/api/oauth/authorization_code", conf.GetServerHost()),
Scopes: strings.Split(config.Scope, " "),
}
}