samldriver.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package models
  15. import (
  16. "context"
  17. "io"
  18. "os"
  19. "path"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/pkg/util/httputils"
  23. "yunion.io/x/pkg/util/samlutils"
  24. "yunion.io/x/onecloud/pkg/cloudid/options"
  25. "yunion.io/x/onecloud/pkg/mcclient"
  26. "yunion.io/x/onecloud/pkg/util/samlutils/idp"
  27. )
  28. type SamlInstance func() *idp.SSAMLIdpInstance
  29. var (
  30. SamlIdpInstance SamlInstance = nil
  31. )
  32. type ICloudSAMLLoginDriver interface {
  33. GetEntityID() string
  34. GetMetadataFilename() string
  35. GetMetadataUrl() string
  36. GetIdpInitiatedLoginData(ctx context.Context, userCred mcclient.TokenCredential, cloudAccountId string, sp *idp.SSAMLServiceProvider, redirectUrl string) (samlutils.SSAMLIdpInitiatedLoginData, error)
  37. GetSpInitiatedLoginData(ctx context.Context, userCred mcclient.TokenCredential, cloudAccoutId string, sp *idp.SSAMLServiceProvider) (samlutils.SSAMLSpInitiatedLoginData, error)
  38. }
  39. var (
  40. driverTable = make(map[string]ICloudSAMLLoginDriver)
  41. )
  42. func Register(driver ICloudSAMLLoginDriver) {
  43. driverTable[driver.GetEntityID()] = driver
  44. }
  45. func UnRegister(entityId string) {
  46. delete(driverTable, entityId)
  47. }
  48. func FindDriver(entityId string) ICloudSAMLLoginDriver {
  49. if driver, ok := driverTable[entityId]; ok {
  50. return driver
  51. }
  52. return nil
  53. }
  54. func AllDrivers() map[string]ICloudSAMLLoginDriver {
  55. return driverTable
  56. }
  57. func GetMetadata(driver ICloudSAMLLoginDriver) ([]byte, error) {
  58. filePath := path.Join(options.Options.CloudSAMLMetadataPath, driver.GetMetadataFilename())
  59. metaBytes, err := os.ReadFile(filePath)
  60. if err != nil || len(metaBytes) == 0 {
  61. metaUrl := driver.GetMetadataUrl()
  62. if len(metaUrl) > 0 {
  63. log.Debugf("[%s] metadata file load failed, try download from %s", driver.GetEntityID(), metaUrl)
  64. httpcli := httputils.GetDefaultClient()
  65. resp, err := httpcli.Get(metaUrl)
  66. if err != nil {
  67. return nil, errors.Wrapf(err, "http get %s fail", metaUrl)
  68. }
  69. metaBytes, err = io.ReadAll(resp.Body)
  70. if err != nil {
  71. return nil, errors.Wrapf(err, "read body %s fail", metaUrl)
  72. }
  73. os.WriteFile(filePath, metaBytes, 0644)
  74. } else {
  75. return nil, errors.Wrapf(err, "read file %s fail", filePath)
  76. }
  77. }
  78. return metaBytes, nil
  79. }