class.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 oidc
  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/driver/utils"
  23. "yunion.io/x/onecloud/pkg/keystone/models"
  24. "yunion.io/x/onecloud/pkg/mcclient"
  25. "yunion.io/x/onecloud/pkg/util/oidcutils/client"
  26. )
  27. type SOIDCDriverClass struct{}
  28. func (self *SOIDCDriverClass) IsSso() bool {
  29. return true
  30. }
  31. func (self *SOIDCDriverClass) ForceSyncUser() bool {
  32. return false
  33. }
  34. func (self *SOIDCDriverClass) GetDefaultIconUri(tmpName string) string {
  35. switch tmpName {
  36. case api.IdpTemplateDex:
  37. return "https://raw.githubusercontent.com/dexidp/dex/master/Documentation/logos/dex-glyph-color.png"
  38. case api.IdpTemplateGithub:
  39. return "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png"
  40. case api.IdpTemplateAzureOAuth2:
  41. return "https://upload.wikimedia.org/wikipedia/commons/a/a8/Microsoft_Azure_Logo.svg"
  42. }
  43. return "https://openid.net/wordpress-content/uploads/2011/09/OPENID_CONNECT_NEW-Logo-1024x474.jpg"
  44. }
  45. func (self *SOIDCDriverClass) SingletonInstance() bool {
  46. return false
  47. }
  48. func (self *SOIDCDriverClass) SyncMethod() string {
  49. return api.IdentityProviderSyncOnAuth
  50. }
  51. func (self *SOIDCDriverClass) NewDriver(idpId, idpName, template, targetDomainId string, conf api.TConfigs) (driver.IIdentityBackend, error) {
  52. return NewOIDCDriver(idpId, idpName, template, targetDomainId, conf)
  53. }
  54. func (self *SOIDCDriverClass) Name() string {
  55. return api.IdentityDriverOIDC
  56. }
  57. func (self *SOIDCDriverClass) ValidateConfig(ctx context.Context, userCred mcclient.TokenCredential, template string, tconf api.TConfigs, idpId, domainId string) (api.TConfigs, error) {
  58. conf := api.SOIDCIdpConfigOptions{}
  59. confJson := jsonutils.Marshal(tconf[api.IdentityDriverOIDC])
  60. err := confJson.Unmarshal(&conf)
  61. if err != nil {
  62. return tconf, errors.Wrap(err, "unmarshal config")
  63. }
  64. switch template {
  65. case api.IdpTemplateAzureOAuth2:
  66. if tid, ok := tconf[api.IdentityDriverOIDC]["tenant_id"]; !ok || tid == nil {
  67. return tconf, errors.Wrap(httperrors.ErrInputParameter, "empty tenant_id")
  68. } else {
  69. tidStr, _ := tid.GetString()
  70. if len(tidStr) == 0 {
  71. return tconf, errors.Wrap(httperrors.ErrInputParameter, "empty tenant_id")
  72. }
  73. }
  74. }
  75. if len(conf.ClientId) == 0 {
  76. return tconf, errors.Wrap(httperrors.ErrInputParameter, "empty client_id")
  77. }
  78. if len(conf.ClientSecret) == 0 {
  79. return tconf, errors.Wrap(httperrors.ErrInputParameter, "empty client_secret")
  80. }
  81. if len(conf.Endpoint) > 0 {
  82. cli := client.NewOIDCClient(conf.ClientId, conf.ClientSecret, 30, false)
  83. err := cli.FetchConfiguration(ctx, conf.Endpoint)
  84. if err != nil {
  85. return tconf, errors.Wrapf(err, "invalid endpoint %s", conf.Endpoint)
  86. }
  87. }
  88. // validate uniqueness
  89. unique, err := models.IdentityProviderManager.CheckUniqueness(idpId, domainId, api.IdentityDriverOIDC, template, api.IdentityDriverOIDC, "client_id", jsonutils.NewString(conf.ClientId))
  90. if err != nil {
  91. return tconf, errors.Wrap(err, "IdentityProviderManager.CheckUniqueness")
  92. }
  93. if !unique {
  94. return tconf, errors.Wrapf(httperrors.ErrDuplicateResource, "client_id %s has been registered", conf.ClientId)
  95. }
  96. conf.SIdpAttributeOptions, err = utils.ValidateConfig(ctx, conf.SIdpAttributeOptions, userCred)
  97. if err != nil {
  98. return tconf, errors.Wrap(err, "ValidateConfig")
  99. }
  100. nconf := make(map[string]jsonutils.JSONObject)
  101. err = confJson.Unmarshal(&nconf)
  102. if err != nil {
  103. return tconf, errors.Wrap(err, "Unmarshal old config")
  104. }
  105. err = jsonutils.Marshal(conf).Unmarshal(&nconf)
  106. if err != nil {
  107. return tconf, errors.Wrap(err, "Unmarshal new config")
  108. }
  109. tconf[api.IdentityDriverOIDC] = nconf
  110. return tconf, nil
  111. }
  112. func init() {
  113. driver.RegisterDriverClass(&SOIDCDriverClass{})
  114. }