class.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 ldap
  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/models"
  24. "yunion.io/x/onecloud/pkg/mcclient"
  25. )
  26. type SLDAPDriverClass struct{}
  27. func (self *SLDAPDriverClass) IsSso() bool {
  28. return false
  29. }
  30. func (self *SLDAPDriverClass) ForceSyncUser() bool {
  31. return true
  32. }
  33. func (self *SLDAPDriverClass) GetDefaultIconUri(tmpName string) string {
  34. return ""
  35. }
  36. func (self *SLDAPDriverClass) SingletonInstance() bool {
  37. return false
  38. }
  39. func (self *SLDAPDriverClass) SyncMethod() string {
  40. return api.IdentityProviderSyncFull
  41. }
  42. func (self *SLDAPDriverClass) NewDriver(idpId, idpName, template, targetDomainId string, conf api.TConfigs) (driver.IIdentityBackend, error) {
  43. return NewLDAPDriver(idpId, idpName, template, targetDomainId, conf)
  44. }
  45. func (self *SLDAPDriverClass) Name() string {
  46. return api.IdentityDriverLDAP
  47. }
  48. func (self *SLDAPDriverClass) ValidateConfig(ctx context.Context, userCred mcclient.TokenCredential, template string, tconf api.TConfigs, idpId, domainId string) (api.TConfigs, error) {
  49. conf := api.SLDAPIdpConfigOptions{}
  50. confJson := jsonutils.Marshal(tconf[api.IdentityDriverLDAP])
  51. err := confJson.Unmarshal(&conf)
  52. if err != nil {
  53. return tconf, errors.Wrap(err, "unmarshal config")
  54. }
  55. if len(conf.Url) == 0 {
  56. return tconf, errors.Wrap(httperrors.ErrInputParameter, "empty url")
  57. }
  58. _, err = url.Parse(conf.Url)
  59. if err != nil {
  60. return tconf, errors.Wrap(httperrors.ErrInputParameter, "invalid url")
  61. }
  62. if len(conf.Suffix) == 0 {
  63. return tconf, errors.Wrap(httperrors.ErrInputParameter, "empty suffix")
  64. }
  65. // validate uniqueness
  66. unique, err := models.IdentityProviderManager.CheckUniqueness(idpId, domainId, api.IdentityDriverLDAP, template, api.IdentityDriverLDAP, "url", jsonutils.NewString(conf.Url))
  67. if err != nil {
  68. return tconf, errors.Wrap(err, "IdentityProviderManager.CheckUniqueness")
  69. }
  70. if !unique {
  71. return tconf, errors.Wrapf(httperrors.ErrDuplicateResource, "ldap URL %s has been registered", conf.Url)
  72. }
  73. return tconf, nil
  74. }
  75. func init() {
  76. driver.RegisterDriverClass(&SLDAPDriverClass{})
  77. }