Skip to content

Commit c08f2b1

Browse files
authored
feat: support Casdoor storage provider (casdoor#3147)
* feat: support Casdoor storage provider * fix: fix code format and nil pointer error * feat: change cert if statement
1 parent 62bb257 commit c08f2b1

File tree

8 files changed

+87
-16
lines changed

8 files changed

+87
-16
lines changed

deployment/deploy.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,18 @@ import (
2727
)
2828

2929
func deployStaticFiles(provider *object.Provider) {
30-
storageProvider, err := storage.GetStorageProvider(provider.Type, provider.ClientId, provider.ClientSecret, provider.RegionId, provider.Bucket, provider.Endpoint)
30+
certificate := ""
31+
if provider.Category == "Storage" && provider.Type == "Casdoor" {
32+
cert, err := object.GetCert(util.GetId(provider.Owner, provider.Cert))
33+
if err != nil {
34+
panic(err)
35+
}
36+
if cert == nil {
37+
panic(err)
38+
}
39+
certificate = cert.Certificate
40+
}
41+
storageProvider, err := storage.GetStorageProvider(provider.Type, provider.ClientId, provider.ClientSecret, provider.RegionId, provider.Bucket, provider.Endpoint, certificate, provider.Content)
3142
if err != nil {
3243
panic(err)
3344
}

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/casdoor/go-sms-sender v0.24.0
1313
github.com/casdoor/gomail/v2 v2.0.1
1414
github.com/casdoor/notify v0.45.0
15-
github.com/casdoor/oss v1.7.0
15+
github.com/casdoor/oss v1.8.0
1616
github.com/casdoor/xorm-adapter/v3 v3.1.0
1717
github.com/casvisor/casvisor-go-sdk v1.4.0
1818
github.com/dchest/captcha v0.0.0-20200903113550-03f5f0333e1f
@@ -30,7 +30,7 @@ require (
3030
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
3131
github.com/go-webauthn/webauthn v0.6.0
3232
github.com/golang-jwt/jwt/v4 v4.5.0
33-
github.com/google/uuid v1.4.0
33+
github.com/google/uuid v1.6.0
3434
github.com/json-iterator/go v1.1.12
3535
github.com/lestrrat-go/jwx v1.2.29
3636
github.com/lib/pq v1.10.9

go.sum

+6-3
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,8 @@ github.com/casbin/casbin/v2 v2.28.3/go.mod h1:vByNa/Fchek0KZUgG5wEsl7iFsiviAYKRt
10831083
github.com/casbin/casbin/v2 v2.37.0/go.mod h1:vByNa/Fchek0KZUgG5wEsl7iFsiviAYKRtgrQfcJqHg=
10841084
github.com/casbin/casbin/v2 v2.77.2 h1:yQinn/w9x8AswiwqwtrXz93VU48R1aYTXdHEx4RI3jM=
10851085
github.com/casbin/casbin/v2 v2.77.2/go.mod h1:mzGx0hYW9/ksOSpw3wNjk3NRAroq5VMFYUQ6G43iGPk=
1086+
github.com/casdoor/casdoor-go-sdk v0.50.0 h1:bUYbz/MzJuWfLKJbJM0+U0YpYewAur+THp5TKnufWZM=
1087+
github.com/casdoor/casdoor-go-sdk v0.50.0/go.mod h1:cMnkCQJgMYpgAlgEx8reSt1AVaDIQLcJ1zk5pzBaz+4=
10861088
github.com/casdoor/go-reddit/v2 v2.1.0 h1:kIbfdJ7AA7H0uTQ8s0q4GGZqSS5V9wVE74RrXyD9XPs=
10871089
github.com/casdoor/go-reddit/v2 v2.1.0/go.mod h1:eagkvwlZ4Hcsuc/uQsLHYEulz5jN65SVSwV/AIE7zsc=
10881090
github.com/casdoor/go-sms-sender v0.24.0 h1:LNLsce3EG/87I3JS6UiajF3LlQmdIiCgebEu0IE4wSM=
@@ -1091,8 +1093,8 @@ github.com/casdoor/gomail/v2 v2.0.1 h1:J+FG6x80s9e5lBHUn8Sv0Y56mud34KiWih5YdmudR
10911093
github.com/casdoor/gomail/v2 v2.0.1/go.mod h1:VnGPslEAtpix5FjHisR/WKB1qvZDBaujbikxDe9d+2Q=
10921094
github.com/casdoor/notify v0.45.0 h1:OlaFvcQFjGOgA4mRx07M8AH1gvb5xNo21mcqrVGlLgk=
10931095
github.com/casdoor/notify v0.45.0/go.mod h1:wNHQu0tiDROMBIvz0j3Om3Lhd5yZ+AIfnFb8MYb8OLQ=
1094-
github.com/casdoor/oss v1.7.0 h1:VCOuD+CcD0MAA99p6JTyUak14bVR6UsaeyuTaVg0Mrs=
1095-
github.com/casdoor/oss v1.7.0/go.mod h1:rJAWA0hLhtu94t6IRpotLUkXO1NWMASirywQYaGizJE=
1096+
github.com/casdoor/oss v1.8.0 h1:uuyKhDIp7ydOtV4lpqhAY23Ban2Ln8La8+QT36CwylM=
1097+
github.com/casdoor/oss v1.8.0/go.mod h1:uaqO7KBI2lnZcnB8rF7O6C2bN7llIbfC5Ql8ex1yR1U=
10961098
github.com/casdoor/xorm-adapter/v3 v3.1.0 h1:NodWayRtSLVSeCvL9H3Hc61k0G17KhV9IymTCNfh3kk=
10971099
github.com/casdoor/xorm-adapter/v3 v3.1.0/go.mod h1:4WTcUw+bTgBylGHeGHzTtBvuTXRS23dtwzFLl9tsgFM=
10981100
github.com/casvisor/casvisor-go-sdk v1.4.0 h1:hbZEGGJ1cwdHFAxeXrMoNw6yha6Oyg2F0qQhBNCN/dg=
@@ -1460,8 +1462,9 @@ github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
14601462
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
14611463
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
14621464
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
1463-
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
14641465
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
1466+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
1467+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
14651468
github.com/googleapis/enterprise-certificate-proxy v0.0.0-20220520183353-fd19c99a87aa/go.mod h1:17drOmN3MwGY7t0e+Ei9b45FFGA3fBs3x36SsCg1hq8=
14661469
github.com/googleapis/enterprise-certificate-proxy v0.1.0/go.mod h1:17drOmN3MwGY7t0e+Ei9b45FFGA3fBs3x36SsCg1hq8=
14671470
github.com/googleapis/enterprise-certificate-proxy v0.2.0/go.mod h1:8C0jb7/mgJe/9KK8Lm7X9ctZC2t60YyIpYEI16jx0Qg=

object/storage.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,18 @@ func GetUploadFileUrl(provider *Provider, fullFilePath string, hasTimestamp bool
117117

118118
func getStorageProvider(provider *Provider, lang string) (oss.StorageInterface, error) {
119119
endpoint := getProviderEndpoint(provider)
120-
storageProvider, err := storage.GetStorageProvider(provider.Type, provider.ClientId, provider.ClientSecret, provider.RegionId, provider.Bucket, endpoint)
120+
certificate := ""
121+
if provider.Category == "Storage" && provider.Type == "Casdoor" {
122+
cert, err := GetCert(util.GetId(provider.Owner, provider.Cert))
123+
if err != nil {
124+
return nil, err
125+
}
126+
if cert == nil {
127+
return nil, fmt.Errorf("no cert for %s", provider.Cert)
128+
}
129+
certificate = cert.Certificate
130+
}
131+
storageProvider, err := storage.GetStorageProvider(provider.Type, provider.ClientId, provider.ClientSecret, provider.RegionId, provider.Bucket, endpoint, certificate, provider.Content)
121132
if err != nil {
122133
return nil, err
123134
}
@@ -145,11 +156,15 @@ func uploadFile(provider *Provider, fullFilePath string, fileBuffer *bytes.Buffe
145156
fileUrl, objectKey := GetUploadFileUrl(provider, fullFilePath, true)
146157
objectKeyRefined := refineObjectKey(provider, objectKey)
147158

148-
_, err = storageProvider.Put(objectKeyRefined, fileBuffer)
159+
object, err := storageProvider.Put(objectKeyRefined, fileBuffer)
149160
if err != nil {
150161
return "", "", err
151162
}
152163

164+
if provider.Type == "Casdoor" {
165+
fileUrl = object.Path
166+
}
167+
153168
return fileUrl, objectKey, nil
154169
}
155170

storage/casdoor.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package storage
2+
3+
import (
4+
"github.com/casdoor/oss"
5+
"github.com/casdoor/oss/casdoor"
6+
)
7+
8+
func NewCasdoorStorageProvider(providerType string, clientId string, clientSecret string, region string, bucket string, endpoint string, cert string, content string) oss.StorageInterface {
9+
sp := casdoor.New(&casdoor.Config{
10+
clientId,
11+
clientSecret,
12+
endpoint,
13+
cert,
14+
region,
15+
content,
16+
bucket,
17+
})
18+
return sp
19+
}

storage/storage.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ package storage
1616

1717
import "github.com/casdoor/oss"
1818

19-
func GetStorageProvider(providerType string, clientId string, clientSecret string, region string, bucket string, endpoint string) (oss.StorageInterface, error) {
19+
func GetStorageProvider(providerType string, clientId string, clientSecret string, region string, bucket string, endpoint string, cert string, content string) (oss.StorageInterface, error) {
2020
switch providerType {
2121
case "Local File System":
2222
return NewLocalFileSystemStorageProvider(), nil
@@ -36,6 +36,8 @@ func GetStorageProvider(providerType string, clientId string, clientSecret strin
3636
return NewGoogleCloudStorageProvider(clientSecret, bucket, endpoint), nil
3737
case "Synology":
3838
return NewSynologyNasStorageProvider(clientId, clientSecret, endpoint), nil
39+
case "Casdoor":
40+
return NewCasdoorStorageProvider(providerType, clientId, clientSecret, region, bucket, endpoint, cert, content), nil
3941
}
4042

4143
return nil, nil

web/src/ProviderEditPage.js

+23-7
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ class ProviderEditPage extends React.Component {
843843
)
844844
}
845845
{
846-
this.state.provider.type !== "ADFS" && this.state.provider.type !== "AzureAD" && this.state.provider.type !== "AzureADB2C" && this.state.provider.type !== "Casdoor" && this.state.provider.type !== "Okta" ? null : (
846+
this.state.provider.type !== "ADFS" && this.state.provider.type !== "AzureAD" && this.state.provider.type !== "AzureADB2C" && (this.state.provider.type !== "Casdoor" && this.state.category !== "Storage") && this.state.provider.type !== "Okta" ? null : (
847847
<Row style={{marginTop: "20px"}} >
848848
<Col style={{marginTop: "5px"}} span={2}>
849849
{Setting.getLabel(i18next.t("provider:Domain"), i18next.t("provider:Domain - Tooltip"))} :
@@ -870,7 +870,7 @@ class ProviderEditPage extends React.Component {
870870
</Col>
871871
</Row>
872872
)}
873-
{["Custom HTTP SMS", "Local File System", "MinIO", "Tencent Cloud COS", "Google Cloud Storage", "Qiniu Cloud Kodo", "Synology"].includes(this.state.provider.type) ? null : (
873+
{["Custom HTTP SMS", "Local File System", "MinIO", "Tencent Cloud COS", "Google Cloud Storage", "Qiniu Cloud Kodo", "Synology", "Casdoor"].includes(this.state.provider.type) ? null : (
874874
<Row style={{marginTop: "20px"}} >
875875
<Col style={{marginTop: "5px"}} span={2}>
876876
{Setting.getLabel(i18next.t("provider:Endpoint (Intranet)"), i18next.t("provider:Region endpoint for Intranet"))} :
@@ -885,7 +885,9 @@ class ProviderEditPage extends React.Component {
885885
{["Custom HTTP SMS", "Local File System"].includes(this.state.provider.type) ? null : (
886886
<Row style={{marginTop: "20px"}} >
887887
<Col style={{marginTop: "5px"}} span={2}>
888-
{Setting.getLabel(i18next.t("provider:Bucket"), i18next.t("provider:Bucket - Tooltip"))} :
888+
{["Casdoor"].includes(this.state.provider.type) ?
889+
Setting.getLabel(i18next.t("general:Provider"), i18next.t("provider:Provider - Tooltip"))
890+
: Setting.getLabel(i18next.t("provider:Bucket"), i18next.t("provider:Bucket - Tooltip"))} :
889891
</Col>
890892
<Col span={22} >
891893
<Input value={this.state.provider.bucket} onChange={e => {
@@ -906,7 +908,7 @@ class ProviderEditPage extends React.Component {
906908
</Col>
907909
</Row>
908910
)}
909-
{["Custom HTTP SMS", "Qiniu Cloud Kodo", "Synology"].includes(this.state.provider.type) ? null : (
911+
{["Custom HTTP SMS", "Qiniu Cloud Kodo", "Synology", "Casdoor"].includes(this.state.provider.type) ? null : (
910912
<Row style={{marginTop: "20px"}} >
911913
<Col style={{marginTop: "5px"}} span={2}>
912914
{Setting.getLabel(i18next.t("provider:Domain"), i18next.t("provider:Domain - Tooltip"))} :
@@ -918,10 +920,24 @@ class ProviderEditPage extends React.Component {
918920
</Col>
919921
</Row>
920922
)}
921-
{["AWS S3", "Tencent Cloud COS", "Qiniu Cloud Kodo"].includes(this.state.provider.type) ? (
923+
{["Casdoor"].includes(this.state.provider.type) ? (
922924
<Row style={{marginTop: "20px"}} >
923925
<Col style={{marginTop: "5px"}} span={2}>
924-
{Setting.getLabel(i18next.t("provider:Region ID"), i18next.t("provider:Region ID - Tooltip"))} :
926+
{Setting.getLabel(i18next.t("general:Organization"), i18next.t("general:Organization - Tooltip"))} :
927+
</Col>
928+
<Col span={22} >
929+
<Input value={this.state.provider.content} onChange={e => {
930+
this.updateProviderField("content", e.target.value);
931+
}} />
932+
</Col>
933+
</Row>
934+
) : null}
935+
{["AWS S3", "Tencent Cloud COS", "Qiniu Cloud Kodo", "Casdoor"].includes(this.state.provider.type) ? (
936+
<Row style={{marginTop: "20px"}} >
937+
<Col style={{marginTop: "5px"}} span={2}>
938+
{["Casdoor"].includes(this.state.provider.type) ?
939+
Setting.getLabel(i18next.t("general:Application"), i18next.t("general:Application - Tooltip")) :
940+
Setting.getLabel(i18next.t("provider:Region ID"), i18next.t("provider:Region ID - Tooltip"))} :
925941
</Col>
926942
<Col span={22} >
927943
<Input value={this.state.provider.regionId} onChange={e => {
@@ -1298,7 +1314,7 @@ class ProviderEditPage extends React.Component {
12981314
) : null
12991315
}
13001316
{
1301-
(this.state.provider.type === "Alipay" || this.state.provider.type === "WeChat Pay") ? (
1317+
(this.state.provider.type === "Alipay" || this.state.provider.type === "WeChat Pay" || this.state.provider.type === "Casdoor") ? (
13021318
<Row style={{marginTop: "20px"}} >
13031319
<Col style={{marginTop: "5px"}} span={(Setting.isMobile()) ? 22 : 2}>
13041320
{Setting.getLabel(i18next.t("general:Cert"), i18next.t("general:Cert - Tooltip"))} :

web/src/Setting.js

+5
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ export const OtherProviderInfo = {
229229
logo: `${StaticBaseUrl}/img/social_synology.png`,
230230
url: "https://www.synology.com/en-global/dsm/feature/file_sharing",
231231
},
232+
"Casdoor": {
233+
logo: `${StaticBaseUrl}/img/casdoor.png`,
234+
url: "https://casdoor.org/docs/provider/storage/overview",
235+
},
232236
},
233237
SAML: {
234238
"Aliyun IDaaS": {
@@ -1062,6 +1066,7 @@ export function getProviderTypeOptions(category) {
10621066
{id: "Qiniu Cloud Kodo", name: "Qiniu Cloud Kodo"},
10631067
{id: "Google Cloud Storage", name: "Google Cloud Storage"},
10641068
{id: "Synology", name: "Synology"},
1069+
{id: "Casdoor", name: "Casdoor"},
10651070
]
10661071
);
10671072
} else if (category === "SAML") {

0 commit comments

Comments
 (0)