class.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 cas
  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 SCASDriverClass struct{}
  28. func (self *SCASDriverClass) IsSso() bool {
  29. return true
  30. }
  31. func (self *SCASDriverClass) ForceSyncUser() bool {
  32. return false
  33. }
  34. func (self *SCASDriverClass) GetDefaultIconUri(tmpName string) string {
  35. return "https://www.apereo.org/sites/default/files/styles/project_logo/public/projects/logos/cas_max_logo_0.png"
  36. }
  37. func (self *SCASDriverClass) SingletonInstance() bool {
  38. return false
  39. }
  40. func (self *SCASDriverClass) SyncMethod() string {
  41. return api.IdentityProviderSyncOnAuth
  42. }
  43. func (self *SCASDriverClass) NewDriver(idpId, idpName, template, targetDomainId string, conf api.TConfigs) (driver.IIdentityBackend, error) {
  44. return NewCASDriver(idpId, idpName, template, targetDomainId, conf)
  45. }
  46. func (self *SCASDriverClass) Name() string {
  47. return api.IdentityDriverCAS
  48. }
  49. func (self *SCASDriverClass) ValidateConfig(ctx context.Context, userCred mcclient.TokenCredential, template string, tconf api.TConfigs, idpId, domainId string) (api.TConfigs, error) {
  50. conf := api.SCASIdpConfigOptions{}
  51. confJson := jsonutils.Marshal(tconf[api.IdentityDriverCAS])
  52. err := confJson.Unmarshal(&conf)
  53. if err != nil {
  54. return tconf, errors.Wrap(err, "unmarshal config")
  55. }
  56. if len(conf.CASServerURL) == 0 {
  57. return tconf, errors.Wrap(httperrors.ErrInputParameter, "empty cas_server_url")
  58. }
  59. _, err = url.Parse(conf.CASServerURL)
  60. if err != nil {
  61. return tconf, errors.Wrap(httperrors.ErrInputParameter, "invalid cas_server_url")
  62. }
  63. // validate uniqueness
  64. unique, err := models.IdentityProviderManager.CheckUniqueness(idpId, domainId, api.IdentityDriverCAS, template, api.IdentityDriverCAS, "cas_server_url", jsonutils.NewString(conf.CASServerURL))
  65. if err != nil {
  66. return tconf, errors.Wrap(err, "IdentityProviderManager.CheckUniqueness")
  67. }
  68. if !unique {
  69. return tconf, errors.Wrapf(httperrors.ErrDuplicateResource, "cas_server_url %s has been registered", conf.CASServerURL)
  70. }
  71. conf.SIdpAttributeOptions, err = utils.ValidateConfig(ctx, conf.SIdpAttributeOptions, userCred)
  72. if err != nil {
  73. return tconf, errors.Wrap(err, "ValidateConfig")
  74. }
  75. nconf := make(map[string]jsonutils.JSONObject)
  76. err = confJson.Unmarshal(&nconf)
  77. if err != nil {
  78. return tconf, errors.Wrap(err, "Unmarshal old config")
  79. }
  80. err = jsonutils.Marshal(conf).Unmarshal(&nconf)
  81. if err != nil {
  82. return tconf, errors.Wrap(err, "Unmarshal new config")
  83. }
  84. tconf[api.IdentityDriverCAS] = nconf
  85. return tconf, nil
  86. }
  87. func init() {
  88. driver.RegisterDriverClass(&SCASDriverClass{})
  89. }