diff --git a/backend/app/api/v1/backup.go b/backend/app/api/v1/backup.go index 3380e4a36..95452ae23 100644 --- a/backend/app/api/v1/backup.go +++ b/backend/app/api/v1/backup.go @@ -98,6 +98,22 @@ func (b *BaseApi) ListBuckets(c *gin.Context) { helper.SuccessWithData(c, buckets) } +// @Tags Backup Account +// @Summary Load OneDrive info +// @Description 获取 OneDrive 信息 +// @Accept json +// @Success 200 string clientID +// @Security ApiKeyAuth +// @Router /settings/backup/onedrive [get] +func (b *BaseApi) LoadOneDriveInfo(c *gin.Context) { + clientID, err := backupService.LoadOneDriveInfo() + if err != nil { + helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) + return + } + helper.SuccessWithData(c, clientID) +} + // @Tags Backup Account // @Summary Delete backup account // @Description 删除备份账号 diff --git a/backend/app/service/backup.go b/backend/app/service/backup.go index 7538094b0..bbbb86d85 100644 --- a/backend/app/service/backup.go +++ b/backend/app/service/backup.go @@ -2,6 +2,7 @@ package service import ( "context" + "encoding/base64" "encoding/json" "fmt" "io" @@ -27,6 +28,7 @@ type BackupService struct{} type IBackupService interface { List() ([]dto.BackupInfo, error) SearchRecordsWithPage(search dto.RecordSearch) (int64, []dto.BackupRecords, error) + LoadOneDriveInfo() (string, error) DownloadRecord(info dto.DownloadRecord) (string, error) Create(backupDto dto.BackupOperate) error GetBuckets(backupDto dto.ForBuckets) ([]interface{}, error) @@ -88,6 +90,18 @@ func (u *BackupService) SearchRecordsWithPage(search dto.RecordSearch) (int64, [ return total, dtobas, err } +func (u *BackupService) LoadOneDriveInfo() (string, error) { + OneDriveID, err := settingRepo.Get(settingRepo.WithByKey("OneDriveID")) + if err != nil { + return "", err + } + idItem, err := base64.StdEncoding.DecodeString(OneDriveID.Value) + if err != nil { + return "", err + } + return string(idItem), err +} + func (u *BackupService) DownloadRecord(info dto.DownloadRecord) (string, error) { if info.Source == "LOCAL" { return info.FileDir + "/" + info.FileName, nil @@ -313,8 +327,8 @@ func (u *BackupService) loadAccessToken(backup *model.BackupAccount) error { } data := url.Values{} - data.Set("client_id", constant.OneDriveClientID) - data.Set("client_secret", constant.OneDriveClientSecret) + data.Set("client_id", global.CONF.System.OneDriveID) + data.Set("client_secret", global.CONF.System.OneDriveSc) data.Set("grant_type", "authorization_code") data.Set("code", varMap["code"].(string)) data.Set("redirect_uri", constant.OneDriveRedirectURI) diff --git a/backend/configs/system.go b/backend/configs/system.go index 373816511..3e46799dc 100644 --- a/backend/configs/system.go +++ b/backend/configs/system.go @@ -21,4 +21,6 @@ type System struct { IsDemo bool `mapstructure:"is_demo"` AppRepo string `mapstructure:"app_repo"` ChangeUserInfo bool `mapstructure:"change_user_info"` + OneDriveID string `mapstructure:"one_drive_id"` + OneDriveSc string `mapstructure:"one_drive_sc"` } diff --git a/backend/constant/backup.go b/backend/constant/backup.go index 57dbeed53..deb938614 100644 --- a/backend/constant/backup.go +++ b/backend/constant/backup.go @@ -12,7 +12,5 @@ const ( Cos = "COS" Kodo = "KODO" - OneDriveClientID = "5446cfe3-4c79-47a0-ae25-fc645478e2d9" - OneDriveClientSecret = "ITh8Q~0UKJNXAvz6HE~pd3DTnGJOgDEEpnDOJbqY" - OneDriveRedirectURI = "http://localhost/login/authorized" + OneDriveRedirectURI = "http://localhost/login/authorized" ) diff --git a/backend/init/hook/hook.go b/backend/init/hook/hook.go index 9d328dfe7..d179157d6 100644 --- a/backend/init/hook/hook.go +++ b/backend/init/hook/hook.go @@ -1,6 +1,8 @@ package hook import ( + "encoding/base64" + "github.com/1Panel-dev/1Panel/backend/app/repo" "github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/utils/cmd" @@ -26,6 +28,19 @@ func Init() { } global.CONF.System.SSL = sslSetting.Value + OneDriveID, err := settingRepo.Get(settingRepo.WithByKey("OneDriveID")) + if err != nil { + global.LOG.Errorf("load onedrive info from setting failed, err: %v", err) + } + idItem, _ := base64.StdEncoding.DecodeString(OneDriveID.Value) + global.CONF.System.OneDriveID = string(idItem) + OneDriveSc, err := settingRepo.Get(settingRepo.WithByKey("OneDriveSc")) + if err != nil { + global.LOG.Errorf("load onedrive info from setting failed, err: %v", err) + } + scItem, _ := base64.StdEncoding.DecodeString(OneDriveSc.Value) + global.CONF.System.OneDriveSc = string(scItem) + if _, err := settingRepo.Get(settingRepo.WithByKey("SystemStatus")); err != nil { _ = settingRepo.Create("SystemStatus", "Free") } diff --git a/backend/init/migration/migrations/init.go b/backend/init/migration/migrations/init.go index 874b1fb84..44fdc3802 100644 --- a/backend/init/migration/migrations/init.go +++ b/backend/init/migration/migrations/init.go @@ -410,6 +410,12 @@ var AddMfaInterval = &gormigrate.Migration{ if err := tx.Create(&model.Setting{Key: "SystemIP", Value: ""}).Error; err != nil { return err } + if err := tx.Create(&model.Setting{Key: "OneDriveID", Value: "MDEwOTM1YTktMWFhOS00ODU0LWExZGMtNmU0NWZlNjI4YzZi"}).Error; err != nil { + return err + } + if err := tx.Create(&model.Setting{Key: "OneDriveSc", Value: "akpuOFF+YkNXOU1OLWRzS1ZSRDdOcG1LT2ZRM0RLNmdvS1RkVWNGRA=="}).Error; err != nil { + return err + } return nil }, } diff --git a/backend/router/ro_setting.go b/backend/router/ro_setting.go index 57c43ebe1..ebaadca69 100644 --- a/backend/router/ro_setting.go +++ b/backend/router/ro_setting.go @@ -41,6 +41,7 @@ func (s *SettingRouter) InitSettingRouter(Router *gin.RouterGroup) { settingRouter.POST("/snapshot/description/update", baseApi.UpdateSnapDescription) settingRouter.GET("/backup/search", baseApi.ListBackup) + settingRouter.GET("/backup/onedrive", baseApi.LoadOneDriveInfo) settingRouter.POST("/backup/backup", baseApi.Backup) settingRouter.POST("/backup/recover", baseApi.Recover) settingRouter.POST("/backup/recover/byupload", baseApi.RecoverByUpload) diff --git a/backend/utils/cloud_storage/client/onedrive.go b/backend/utils/cloud_storage/client/onedrive.go index 084f77ffe..458a1a6c9 100644 --- a/backend/utils/cloud_storage/client/onedrive.go +++ b/backend/utils/cloud_storage/client/onedrive.go @@ -240,8 +240,8 @@ func (onedrive *oneDriveClient) loadIDByPath(path string) (string, error) { func refreshToken(oldToken string) (string, error) { data := url.Values{} - data.Set("client_id", constant.OneDriveClientID) - data.Set("client_secret", constant.OneDriveClientSecret) + data.Set("client_id", global.CONF.System.OneDriveID) + data.Set("client_secret", global.CONF.System.OneDriveSc) data.Set("grant_type", "refresh_token") data.Set("refresh_token", oldToken) data.Set("redirect_uri", constant.OneDriveRedirectURI) diff --git a/cmd/server/docs/docs.go b/cmd/server/docs/docs.go index 75f1af176..0cb79b2c2 100644 --- a/cmd/server/docs/docs.go +++ b/cmd/server/docs/docs.go @@ -7085,6 +7085,31 @@ var doc = `{ } } }, + "/settings/backup/onedrive": { + "get": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "获取 OneDrive 信息", + "consumes": [ + "application/json" + ], + "tags": [ + "Backup Account" + ], + "summary": "Load OneDrive info", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "string" + } + } + } + } + }, "/settings/backup/record/del": { "post": { "security": [ diff --git a/cmd/server/docs/swagger.json b/cmd/server/docs/swagger.json index 39c9f54de..27597d570 100644 --- a/cmd/server/docs/swagger.json +++ b/cmd/server/docs/swagger.json @@ -7071,6 +7071,31 @@ } } }, + "/settings/backup/onedrive": { + "get": { + "security": [ + { + "ApiKeyAuth": [] + } + ], + "description": "获取 OneDrive 信息", + "consumes": [ + "application/json" + ], + "tags": [ + "Backup Account" + ], + "summary": "Load OneDrive info", + "responses": { + "200": { + "description": "OK", + "schema": { + "type": "string" + } + } + } + } + }, "/settings/backup/record/del": { "post": { "security": [ diff --git a/cmd/server/docs/swagger.yaml b/cmd/server/docs/swagger.yaml index 7ccbc3116..b8626ff6c 100644 --- a/cmd/server/docs/swagger.yaml +++ b/cmd/server/docs/swagger.yaml @@ -7881,6 +7881,21 @@ paths: formatEN: delete backup account [types] formatZH: 删除备份账号 [types] paramKeys: [] + /settings/backup/onedrive: + get: + consumes: + - application/json + description: 获取 OneDrive 信息 + responses: + "200": + description: OK + schema: + type: string + security: + - ApiKeyAuth: [] + summary: Load OneDrive info + tags: + - Backup Account /settings/backup/record/del: post: consumes: diff --git a/frontend/src/api/modules/setting.ts b/frontend/src/api/modules/setting.ts index 0f8f7e514..fda1d9a35 100644 --- a/frontend/src/api/modules/setting.ts +++ b/frontend/src/api/modules/setting.ts @@ -85,6 +85,9 @@ export const searchBackupRecords = (params: Backup.SearchBackupRecord) => { export const getBackupList = () => { return http.get>(`/settings/backup/search`); }; +export const getOneDriveInfo = () => { + return http.get(`/settings/backup/onedrive`); +}; export const getFilesFromBackup = (type: string) => { return http.post>(`/settings/backup/search/files`, { type: type }); }; diff --git a/frontend/src/views/setting/backup-account/operate/index.vue b/frontend/src/views/setting/backup-account/operate/index.vue index e3fe1057b..fb609d07d 100644 --- a/frontend/src/views/setting/backup-account/operate/index.vue +++ b/frontend/src/views/setting/backup-account/operate/index.vue @@ -254,7 +254,7 @@ import i18n from '@/lang'; import { ElForm } from 'element-plus'; import { Backup } from '@/api/interface/backup'; import DrawerHeader from '@/components/drawer-header/index.vue'; -import { addBackup, editBackup, listBucket } from '@/api/modules/setting'; +import { addBackup, editBackup, getOneDriveInfo, listBucket } from '@/api/modules/setting'; import { deepCopy } from '@/utils/util'; import { MsgSuccess } from '@/utils/message'; @@ -309,10 +309,9 @@ const handleClose = () => { emit('search'); drawerVisiable.value = false; }; - -const jumpAzure = () => { - let commonUrl = - 'response_type=code&client_id=5446cfe3-4c79-47a0-ae25-fc645478e2d9&redirect_uri=http://localhost/login/authorized&scope=offline_access+Files.ReadWrite.All+User.Read'; +const jumpAzure = async () => { + const res = await getOneDriveInfo(); + let commonUrl = `response_type=code&client_id=${res.data}&redirect_uri=http://localhost/login/authorized&scope=offline_access+Files.ReadWrite.All+User.Read`; if (!dialogData.value.rowData!.varsJson['isCN']) { window.open('https://login.microsoftonline.com/common/oauth2/v2.0/authorize?' + commonUrl, '_blank'); } else {