driver.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 qcloud
  15. import (
  16. "context"
  17. "fmt"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/pkg/util/samlutils"
  20. "yunion.io/x/onecloud/pkg/cloudid/models"
  21. "yunion.io/x/onecloud/pkg/mcclient"
  22. "yunion.io/x/onecloud/pkg/util/samlutils/idp"
  23. )
  24. func (d *SQcloudSAMLDriver) GetIdpInitiatedLoginData(ctx context.Context, userCred mcclient.TokenCredential, managerId string, sp *idp.SSAMLServiceProvider, redirectUrl string) (samlutils.SSAMLIdpInitiatedLoginData, error) {
  25. data := samlutils.SSAMLIdpInitiatedLoginData{}
  26. provider, err := models.CloudproviderManager.FetchProvier(managerId)
  27. if err != nil {
  28. return data, err
  29. }
  30. account, err := provider.GetCloudaccount()
  31. if err != nil {
  32. return data, errors.Wrapf(err, "GetCloudaccount")
  33. }
  34. role, err := provider.GetRole(ctx, userCred.GetUserId())
  35. if err != nil {
  36. return data, err
  37. }
  38. samlProvider, err := provider.GetSamlProvider()
  39. if err != nil {
  40. return data, err
  41. }
  42. roleStr := fmt.Sprintf("qcs::cam::uin/%s:roleName/%s,qcs::cam::uin/%s:saml-provider/%s", account.AccountId, role.ExternalId, account.AccountId, samlProvider.ExternalId)
  43. data.NameId = role.Name
  44. data.NameIdFormat = samlutils.NAME_ID_FORMAT_TRANSIENT
  45. data.AudienceRestriction = "https://cloud.tencent.com"
  46. for _, v := range []struct {
  47. name string
  48. friendlyName string
  49. value string
  50. }{
  51. {
  52. name: "https://cloud.tencent.com/SAML/Attributes/Role",
  53. friendlyName: "RoleEntitlement",
  54. value: roleStr,
  55. },
  56. {
  57. name: "https://cloud.tencent.com/SAML/Attributes/RoleSessionName",
  58. friendlyName: "RoleSessionName",
  59. value: data.NameId,
  60. },
  61. } {
  62. data.Attributes = append(data.Attributes, samlutils.SSAMLResponseAttribute{
  63. Name: v.name,
  64. FriendlyName: v.friendlyName,
  65. Values: []string{v.value},
  66. })
  67. }
  68. if len(redirectUrl) == 0 {
  69. redirectUrl = "https://console.cloud.tencent.com/"
  70. }
  71. data.RelayState = redirectUrl
  72. return data, nil
  73. }
  74. func (d *SQcloudSAMLDriver) GetSpInitiatedLoginData(ctx context.Context, userCred mcclient.TokenCredential, managerId string, sp *idp.SSAMLServiceProvider) (samlutils.SSAMLSpInitiatedLoginData, error) {
  75. data := samlutils.SSAMLSpInitiatedLoginData{}
  76. provider, err := models.CloudproviderManager.FetchProvier(managerId)
  77. if err != nil {
  78. return data, err
  79. }
  80. account, err := provider.GetCloudaccount()
  81. if err != nil {
  82. return data, errors.Wrapf(err, "GetCloudaccount")
  83. }
  84. role, err := provider.GetRole(ctx, userCred.GetUserId())
  85. if err != nil {
  86. return data, err
  87. }
  88. samlProvider, err := provider.GetSamlProvider()
  89. if err != nil {
  90. return data, err
  91. }
  92. roleStr := fmt.Sprintf("qcs::cam::uin/%s:roleName/%s,qcs::cam::uin/%s:saml-provider/%s", account.AccountId, role.ExternalId, account.AccountId, samlProvider.ExternalId)
  93. data.NameId = role.Name
  94. data.NameIdFormat = samlutils.NAME_ID_FORMAT_TRANSIENT
  95. data.AudienceRestriction = "https://cloud.tencent.com"
  96. for _, v := range []struct {
  97. name string
  98. friendlyName string
  99. value string
  100. }{
  101. {
  102. name: "https://cloud.tencent.com/SAML/Attributes/Role",
  103. friendlyName: "RoleEntitlement",
  104. value: roleStr,
  105. },
  106. {
  107. name: "https://cloud.tencent.com/SAML/Attributes/RoleSessionName",
  108. friendlyName: "RoleSessionName",
  109. value: role.Name,
  110. },
  111. } {
  112. data.Attributes = append(data.Attributes, samlutils.SSAMLResponseAttribute{
  113. Name: v.name,
  114. FriendlyName: v.friendlyName,
  115. Values: []string{v.value},
  116. })
  117. }
  118. return data, nil
  119. }