class.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 saml
  15. import (
  16. "context"
  17. "net/url"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/pkg/errors"
  20. api "yunion.io/x/onecloud/pkg/apis/identity"
  21. "yunion.io/x/onecloud/pkg/httperrors"
  22. "yunion.io/x/onecloud/pkg/keystone/driver"
  23. "yunion.io/x/onecloud/pkg/keystone/driver/utils"
  24. "yunion.io/x/onecloud/pkg/keystone/models"
  25. "yunion.io/x/onecloud/pkg/mcclient"
  26. )
  27. type SSAMLDriverClass struct{}
  28. func (self *SSAMLDriverClass) IsSso() bool {
  29. return true
  30. }
  31. func (self *SSAMLDriverClass) GetDefaultIconUri(tmpName string) string {
  32. switch tmpName {
  33. case api.IdpTemplateAzureADSAML:
  34. return "https://upload.wikimedia.org/wikipedia/commons/a/a8/Microsoft_Azure_Logo.svg"
  35. }
  36. return "https://www.oasis-open.org/committees/download.php/29723/draft-saml-logo-03.png"
  37. }
  38. func (self *SSAMLDriverClass) ForceSyncUser() bool {
  39. return false
  40. }
  41. func (self *SSAMLDriverClass) SingletonInstance() bool {
  42. return false
  43. }
  44. func (self *SSAMLDriverClass) SyncMethod() string {
  45. return api.IdentityProviderSyncOnAuth
  46. }
  47. func (self *SSAMLDriverClass) NewDriver(idpId, idpName, template, targetDomainId string, conf api.TConfigs) (driver.IIdentityBackend, error) {
  48. return NewSAMLDriver(idpId, idpName, template, targetDomainId, conf)
  49. }
  50. func (self *SSAMLDriverClass) Name() string {
  51. return api.IdentityDriverSAML
  52. }
  53. func (self *SSAMLDriverClass) ValidateConfig(ctx context.Context, userCred mcclient.TokenCredential, template string, tconf api.TConfigs, idpId, domainId string) (api.TConfigs, error) {
  54. conf := api.SSAMLIdpConfigOptions{}
  55. confJson := jsonutils.Marshal(tconf[api.IdentityDriverSAML])
  56. err := confJson.Unmarshal(&conf)
  57. if err != nil {
  58. return tconf, errors.Wrap(err, "unmarshal config")
  59. }
  60. switch template {
  61. case api.IdpTemplateAzureADSAML:
  62. if tid, ok := tconf[api.IdentityDriverSAML]["tenant_id"]; !ok || tid == nil {
  63. return tconf, errors.Wrap(httperrors.ErrInputParameter, "empty tenant_id")
  64. } else {
  65. tidStr, _ := tid.GetString()
  66. if len(tidStr) == 0 {
  67. return tconf, errors.Wrap(httperrors.ErrInputParameter, "empty tenant_id")
  68. }
  69. // validate uniqueness
  70. unique, err := models.IdentityProviderManager.CheckUniqueness(idpId, domainId, api.IdentityDriverSAML, template, api.IdentityDriverSAML, "tenant_id", jsonutils.NewString(tidStr))
  71. if err != nil {
  72. return tconf, errors.Wrap(err, "IdentityProviderManager.CheckUniqueness")
  73. }
  74. if !unique {
  75. return tconf, errors.Wrapf(httperrors.ErrDuplicateResource, "tenant_id %s has been registered", tidStr)
  76. }
  77. }
  78. case api.IdpTemplateSAMLTest:
  79. // validate uniqueness
  80. unique, err := models.IdentityProviderManager.CheckUniqueness(idpId, domainId, api.IdentityDriverSAML, template, "", "", nil)
  81. if err != nil {
  82. return tconf, errors.Wrap(err, "IdentityProviderManager.CheckUniqueness")
  83. }
  84. if !unique {
  85. return tconf, errors.Wrap(httperrors.ErrDuplicateResource, "SAMLTest has been registered")
  86. }
  87. default:
  88. if len(conf.EntityId) == 0 {
  89. return tconf, errors.Wrap(httperrors.ErrInputParameter, "empty entity_id")
  90. }
  91. if len(conf.RedirectSSOUrl) == 0 {
  92. return tconf, errors.Wrap(httperrors.ErrInputParameter, "empty redirect_sso_url")
  93. }
  94. _, err = url.Parse(conf.RedirectSSOUrl)
  95. if err != nil {
  96. return tconf, errors.Wrap(httperrors.ErrInputParameter, "invalid redirect_sso_url")
  97. }
  98. // validate uniqueness
  99. unique, err := models.IdentityProviderManager.CheckUniqueness(idpId, domainId, api.IdentityDriverSAML, template, api.IdentityDriverSAML, "entity_id", jsonutils.NewString(conf.EntityId))
  100. if err != nil {
  101. return tconf, errors.Wrap(err, "IdentityProviderManager.CheckUniqueness")
  102. }
  103. if !unique {
  104. return tconf, errors.Wrapf(httperrors.ErrDuplicateResource, "entity_id %s has been registered", conf.EntityId)
  105. }
  106. }
  107. conf.SIdpAttributeOptions, err = utils.ValidateConfig(ctx, conf.SIdpAttributeOptions, userCred)
  108. if err != nil {
  109. return tconf, errors.Wrap(err, "ValidateConfig")
  110. }
  111. nconf := make(map[string]jsonutils.JSONObject)
  112. err = confJson.Unmarshal(&nconf)
  113. if err != nil {
  114. return tconf, errors.Wrap(err, "Unmarshal old config")
  115. }
  116. err = jsonutils.Marshal(conf).Unmarshal(&nconf)
  117. if err != nil {
  118. return tconf, errors.Wrap(err, "Unmarshal new config")
  119. }
  120. nconf["allow_idp_init"] = jsonutils.JSONTrue
  121. tconf[api.IdentityDriverSAML] = nconf
  122. return tconf, nil
  123. }
  124. func init() {
  125. driver.RegisterDriverClass(&SSAMLDriverClass{})
  126. }