saml.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. "encoding/base64"
  18. "fmt"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/log"
  21. "yunion.io/x/pkg/errors"
  22. "yunion.io/x/pkg/util/httputils"
  23. "yunion.io/x/pkg/util/samlutils"
  24. api "yunion.io/x/onecloud/pkg/apis/identity"
  25. "yunion.io/x/onecloud/pkg/httperrors"
  26. "yunion.io/x/onecloud/pkg/keystone/driver"
  27. "yunion.io/x/onecloud/pkg/keystone/models"
  28. "yunion.io/x/onecloud/pkg/keystone/saml"
  29. "yunion.io/x/onecloud/pkg/mcclient"
  30. "yunion.io/x/onecloud/pkg/util/samlutils/sp"
  31. )
  32. // SAML 2.0 Service Provider Driver
  33. type SSAMLDriver struct {
  34. driver.SBaseIdentityDriver
  35. samlConfig *api.SSAMLIdpConfigOptions
  36. isDebug bool
  37. }
  38. func NewSAMLDriver(idpId, idpName, template, targetDomainId string, conf api.TConfigs) (driver.IIdentityBackend, error) {
  39. base, err := driver.NewBaseIdentityDriver(idpId, idpName, template, targetDomainId, conf)
  40. if err != nil {
  41. return nil, errors.Wrap(err, "NewBaseIdentityDriver")
  42. }
  43. drv := SSAMLDriver{SBaseIdentityDriver: base}
  44. drv.SetVirtualObject(&drv)
  45. err = drv.prepareConfig()
  46. if err != nil {
  47. return nil, errors.Wrap(err, "prepareConfig")
  48. }
  49. return &drv, nil
  50. }
  51. func (self *SSAMLDriver) prepareConfig() error {
  52. if self.samlConfig == nil {
  53. confJson := jsonutils.Marshal(self.Config["saml"])
  54. conf := api.SSAMLIdpConfigOptions{}
  55. switch self.Template {
  56. case api.IdpTemplateSAMLTest:
  57. conf = SAMLTestTemplate
  58. case api.IdpTemplateAzureADSAML:
  59. conf = AzureADTemplate
  60. tenantId, _ := confJson.GetString("tenant_id")
  61. conf.EntityId = fmt.Sprintf("https://sts.windows.net/%s/", tenantId)
  62. conf.RedirectSSOUrl = fmt.Sprintf("https://login.microsoftonline.com/%s/saml2", tenantId)
  63. }
  64. err := confJson.Unmarshal(&conf)
  65. if err != nil {
  66. return errors.Wrap(err, "json.Unmarshal")
  67. }
  68. log.Debugf("%s %s %#v", self.Config, confJson, self.samlConfig)
  69. self.samlConfig = &conf
  70. }
  71. return nil
  72. }
  73. func (self *SSAMLDriver) GetSsoCallbackUri(callbackUrl string) string {
  74. if self.samlConfig.AllowIdpInit != nil && *self.samlConfig.AllowIdpInit {
  75. callbackUrl = httputils.JoinPath(callbackUrl, self.IdpId)
  76. }
  77. return callbackUrl
  78. }
  79. func (self *SSAMLDriver) GetSsoRedirectUri(ctx context.Context, callbackUrl, state string) (string, error) {
  80. spLoginFunc := func(ctx context.Context, idp *sp.SSAMLIdentityProvider) (sp.SSAMLSpInitiatedLoginRequest, error) {
  81. result := sp.SSAMLSpInitiatedLoginRequest{}
  82. result.RequestID = samlutils.GenerateSAMLId()
  83. result.RelayState = state
  84. return result, nil
  85. }
  86. spInst := sp.NewSpInstance(saml.SAMLInstance(), self.IdpName, nil, spLoginFunc)
  87. spInst.SetAssertionConsumerUri(callbackUrl)
  88. err := spInst.AddIdp(self.samlConfig.EntityId, self.samlConfig.RedirectSSOUrl)
  89. if err != nil {
  90. return "", errors.Wrap(err, "Invalid SAMLIdentityProvider")
  91. }
  92. input := samlutils.SSpInitiatedLoginInput{
  93. EntityID: self.samlConfig.EntityId,
  94. }
  95. redir, err := spInst.ProcessSpInitiatedLogin(ctx, input)
  96. if err != nil {
  97. return "", errors.Wrap(err, "ProcessSpInitiatedLogin")
  98. }
  99. return redir, nil
  100. }
  101. func (self *SSAMLDriver) Authenticate(ctx context.Context, ident mcclient.SAuthenticationIdentity) (*api.SUserExtended, error) {
  102. samlRespBytes, err := base64.StdEncoding.DecodeString(ident.SAMLAuth.Response)
  103. if err != nil {
  104. return nil, errors.Wrap(err, "base64.StdEncoding.DecodeString")
  105. }
  106. resp, err := saml.SAMLInstance().UnmarshalResponse(samlRespBytes)
  107. if err != nil {
  108. return nil, errors.Wrap(err, "decode SAMLResponse error")
  109. }
  110. if !resp.IsSuccess() {
  111. return nil, errors.Wrap(httperrors.ErrInvalidCredential, "SAML auth unsuccess")
  112. }
  113. attrs := resp.FetchAttribtues()
  114. var domainId, domainName, usrId, usrName string
  115. if v, ok := attrs[self.samlConfig.DomainIdAttribute]; ok && len(v) > 0 {
  116. domainId = v[0]
  117. }
  118. if v, ok := attrs[self.samlConfig.DomainNameAttribute]; ok && len(v) > 0 {
  119. domainName = v[0]
  120. }
  121. if v, ok := attrs[self.samlConfig.UserIdAttribute]; ok && len(v) > 0 {
  122. usrId = v[0]
  123. }
  124. if v, ok := attrs[self.samlConfig.UserNameAttribute]; ok && len(v) > 0 {
  125. usrName = v[0]
  126. }
  127. idp, err := models.IdentityProviderManager.FetchIdentityProviderById(self.IdpId)
  128. if err != nil {
  129. return nil, errors.Wrap(err, "self.GetIdentityProvider")
  130. }
  131. domain, usr, err := idp.SyncOrCreateDomainAndUser(ctx, domainId, domainName, usrId, usrName)
  132. if err != nil {
  133. return nil, errors.Wrap(err, "idp.SyncOrCreateDomainAndUser")
  134. }
  135. extUser, err := models.UserManager.FetchUserExtended(usr.Id, "", "", "")
  136. if err != nil {
  137. return nil, errors.Wrap(err, "models.UserManager.FetchUserExtended")
  138. }
  139. idp.TryUserJoinProject(self.samlConfig.SIdpAttributeOptions, ctx, usr, domain.Id, attrs)
  140. extUser.AuditIds = []string{resp.ID}
  141. return extUser, nil
  142. }
  143. func (self *SSAMLDriver) Sync(ctx context.Context) error {
  144. return nil
  145. }
  146. func (self *SSAMLDriver) Probe(ctx context.Context) error {
  147. return nil
  148. }