class.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 oauth2
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. api "yunion.io/x/onecloud/pkg/apis/identity"
  20. "yunion.io/x/onecloud/pkg/httperrors"
  21. "yunion.io/x/onecloud/pkg/keystone/driver"
  22. "yunion.io/x/onecloud/pkg/keystone/models"
  23. "yunion.io/x/onecloud/pkg/mcclient"
  24. )
  25. type SOAuth2DriverClass struct{}
  26. func (self *SOAuth2DriverClass) IsSso() bool {
  27. return true
  28. }
  29. func (self *SOAuth2DriverClass) ForceSyncUser() bool {
  30. return false
  31. }
  32. func (self *SOAuth2DriverClass) GetDefaultIconUri(tmpName string) string {
  33. switch tmpName {
  34. case api.IdpTemplateDingtalk:
  35. return "https://img.alicdn.com/tfs/TB13Bxnd3oQMeJjy0FoXXcShVXa-80-80.png"
  36. case api.IdpTemplateFeishu:
  37. return "https://sf1-ttcdn-tos.pstatp.com/obj/suite-public-file-cn/feishu-share-icon.png"
  38. case api.IdpTemplateAlipay:
  39. return "https://gw.alipayobjects.com/mdn/member_frontWeb/afts/img/A*h7o9Q4g2KiUAAAAAAAAAAABkARQnAQ"
  40. case api.IdpTemplateWechat:
  41. return "https://open.weixin.qq.com/zh_CN/htmledition/res/assets/res-design-download/icon64_appwx_logo.png"
  42. case api.IdpTemplateQywechat:
  43. return "http://yunioniso.oss-cn-beijing.aliyuncs.com/icons/qywechat_logo.png"
  44. }
  45. return "https://st.fbk.eu/sites/st.fbk.eu/files/styles/threshold-1382/public/oauth2-logo.jpg"
  46. }
  47. func (self *SOAuth2DriverClass) SingletonInstance() bool {
  48. return false
  49. }
  50. func (self *SOAuth2DriverClass) SyncMethod() string {
  51. return api.IdentityProviderSyncOnAuth
  52. }
  53. func (self *SOAuth2DriverClass) NewDriver(idpId, idpName, template, targetDomainId string, conf api.TConfigs) (driver.IIdentityBackend, error) {
  54. return NewOAuth2Driver(idpId, idpName, template, targetDomainId, conf)
  55. }
  56. func (self *SOAuth2DriverClass) Name() string {
  57. return api.IdentityDriverOAuth2
  58. }
  59. func (self *SOAuth2DriverClass) ValidateConfig(ctx context.Context, userCred mcclient.TokenCredential, template string, tconf api.TConfigs, idpId, domainId string) (api.TConfigs, error) {
  60. conf := api.SOAuth2IdpConfigOptions{}
  61. confJson := jsonutils.Marshal(tconf[api.IdentityDriverOAuth2])
  62. err := confJson.Unmarshal(&conf)
  63. if err != nil {
  64. return tconf, errors.Wrap(err, "unmarshal config")
  65. }
  66. if len(conf.AppId) == 0 {
  67. return tconf, errors.Wrap(httperrors.ErrInputParameter, "empty app_id")
  68. }
  69. if len(conf.Secret) == 0 {
  70. return tconf, errors.Wrap(httperrors.ErrInputParameter, "empty secret")
  71. }
  72. // validate uniqueness
  73. unique, err := models.IdentityProviderManager.CheckUniqueness(idpId, domainId, api.IdentityDriverOAuth2, template, api.IdentityDriverOAuth2, "app_id", jsonutils.NewString(conf.AppId))
  74. if err != nil {
  75. return tconf, errors.Wrap(err, "IdentityProviderManager.CheckUniqueness")
  76. }
  77. if !unique {
  78. return tconf, errors.Wrapf(httperrors.ErrDuplicateResource, "app_id %s has been registered", conf.AppId)
  79. }
  80. factory := findDriverFactory(template)
  81. if factory == nil {
  82. return nil, errors.Wrapf(httperrors.ErrNotSupported, "template %s not supported", template)
  83. }
  84. err = factory.ValidateConfig(conf)
  85. if err != nil {
  86. return nil, errors.Wrap(err, "factory.ValidateConfig")
  87. }
  88. nconf := make(map[string]jsonutils.JSONObject)
  89. err = confJson.Unmarshal(&nconf)
  90. if err != nil {
  91. return tconf, errors.Wrap(err, "Unmarshal old config")
  92. }
  93. err = jsonutils.Marshal(conf).Unmarshal(&nconf)
  94. if err != nil {
  95. return tconf, errors.Wrap(err, "Unmarshal new config")
  96. }
  97. tconf[api.IdentityDriverOAuth2] = nconf
  98. return tconf, nil
  99. }
  100. func init() {
  101. driver.RegisterDriverClass(&SOAuth2DriverClass{})
  102. }