PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/redistributable/openstack.net/src/corelib/Providers/Hp/HpIdentityProvider.cs

https://gitlab.com/rekby-archive/onlyoffice-CommunityServer
C# | 199 lines | 106 code | 23 blank | 70 comment | 28 complexity | 0058dcd4e275aedb11498234f1fd035b MD5 | raw file
  1. using OpenStack.Authentication;
  2. namespace net.openstack.Providers.Hp
  3. {
  4. using System;
  5. using net.openstack.Core.Caching;
  6. using net.openstack.Core.Domain;
  7. using Newtonsoft.Json.Linq;
  8. using CloudIdentityProvider = net.openstack.Providers.Rackspace.CloudIdentityProvider;
  9. using HttpMethod = JSIStudios.SimpleRESTServices.Client.HttpMethod;
  10. using IIdentityProvider = net.openstack.Core.Providers.IIdentityProvider;
  11. using IRestService = JSIStudios.SimpleRESTServices.Client.IRestService;
  12. using JsonRestServices = JSIStudios.SimpleRESTServices.Client.Json.JsonRestServices;
  13. /// <summary>
  14. /// Provides an implementation of <see cref="IIdentityProvider"/> for operating with
  15. /// HP's Cloud Identity product. This provider supports authentication using a username/password
  16. /// combination or an access key/secret key combinatiton, and supports scoped tokens
  17. /// when credentials are represented with <see cref="CloudIdentityWithProject"/>.
  18. /// </summary>
  19. /// <seealso href="http://docs.openstack.org/api/openstack-identity-service/2.0/content/">OpenStack Identity Service API v2.0 Reference</seealso>
  20. /// <seealso href="http://docs.hpcloud.com/api/identity/">HP Cloud v12.12 Identity Services API</seealso>
  21. /// <threadsafety static="true" instance="false"/>
  22. /// <preliminary/>
  23. public class HpIdentityProvider : CloudIdentityProvider
  24. {
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="HpIdentityProvider"/> class
  27. /// with no default identity, the <see cref="PredefinedHpIdentityEndpoints.Default"/> base URL, and the default REST service
  28. /// implementation and token cache.
  29. /// </summary>
  30. public HpIdentityProvider()
  31. : this(PredefinedHpIdentityEndpoints.Default, null, null, null)
  32. {
  33. }
  34. /// <summary>
  35. /// Initializes a new instance of the <see cref="HpIdentityProvider"/> class
  36. /// with the specified default identity, the <see cref="PredefinedHpIdentityEndpoints.Default"/> base URL, and the default REST service
  37. /// implementation and token cache.
  38. /// </summary>
  39. /// <param name="defaultIdentity">The default identity to use for calls that do not explicitly specify an identity. If this value is <see langword="null"/>, no default identity is available so all calls must specify an explicit identity.</param>
  40. public HpIdentityProvider(CloudIdentity defaultIdentity)
  41. : this(PredefinedHpIdentityEndpoints.Default, defaultIdentity, null, null)
  42. {
  43. }
  44. /// <summary>
  45. /// Initializes a new instance of the <see cref="HpIdentityProvider"/> class
  46. /// with no default identity, the <see cref="PredefinedHpIdentityEndpoints.Default"/> base URL, and the default REST service
  47. /// implementation, and token cache.
  48. /// </summary>
  49. /// <param name="restService">The implementation of <see cref="IRestService"/> to use for executing REST requests. If this value is <see langword="null"/>, the provider will use a new instance of <see cref="JsonRestServices"/>.</param>
  50. /// <param name="tokenCache">The cache to use for caching user access tokens. If this value is <see langword="null"/>, the provider will use <see cref="UserAccessCache.Instance"/>.</param>
  51. public HpIdentityProvider(IRestService restService, ICache<UserAccess> tokenCache)
  52. : this(PredefinedHpIdentityEndpoints.Default, null, restService, tokenCache)
  53. {
  54. }
  55. /// <summary>
  56. /// Initializes a new instance of the <see cref="HpIdentityProvider"/> class
  57. /// using the <see cref="PredefinedHpIdentityEndpoints.Default"/> base URL and provided values.
  58. /// </summary>
  59. /// <param name="defaultIdentity">The default identity to use for calls that do not explicitly specify an identity. If this value is <see langword="null"/>, no default identity is available so all calls must specify an explicit identity.</param>
  60. /// <param name="restService">The implementation of <see cref="IRestService"/> to use for executing REST requests. If this value is <see langword="null"/>, the provider will use a new instance of <see cref="JsonRestServices"/>.</param>
  61. /// <param name="tokenCache">The cache to use for caching user access tokens. If this value is <see langword="null"/>, the provider will use <see cref="UserAccessCache.Instance"/>.</param>
  62. public HpIdentityProvider(CloudIdentity defaultIdentity, IRestService restService, ICache<UserAccess> tokenCache)
  63. : this(PredefinedHpIdentityEndpoints.Default, defaultIdentity, restService, tokenCache)
  64. {
  65. }
  66. /// <summary>
  67. /// Initializes a new instance of the <see cref="HpIdentityProvider"/> class
  68. /// with no default identity, the specified base URL, and the default REST service
  69. /// implementation and token cache.
  70. /// </summary>
  71. /// <param name="urlBase">The base URL for the cloud instance. Predefined URLs are available in <see cref="PredefinedHpIdentityEndpoints"/>.</param>
  72. /// <exception cref="ArgumentNullException">If <paramref name="urlBase"/> is <see langword="null"/>.</exception>
  73. public HpIdentityProvider(Uri urlBase)
  74. : this(urlBase, null, null, null)
  75. {
  76. }
  77. /// <summary>
  78. /// Initializes a new instance of the <see cref="HpIdentityProvider"/> class
  79. /// with the specified default identity and base URL, and the default REST service
  80. /// implementation and token cache.
  81. /// </summary>
  82. /// <param name="urlBase">The base URL for the cloud instance. Predefined URLs are available in <see cref="PredefinedHpIdentityEndpoints"/>.</param>
  83. /// <param name="defaultIdentity">The default identity to use for calls that do not explicitly specify an identity. If this value is <see langword="null"/>, no default identity is available so all calls must specify an explicit identity.</param>
  84. /// <exception cref="ArgumentNullException">If <paramref name="urlBase"/> is <see langword="null"/>.</exception>
  85. public HpIdentityProvider(Uri urlBase, CloudIdentity defaultIdentity)
  86. : this(urlBase, defaultIdentity, null, null)
  87. {
  88. }
  89. /// <summary>
  90. /// Initializes a new instance of the <see cref="HpIdentityProvider"/> class
  91. /// with no default identity, and the specified base URL, REST service
  92. /// implementation, and token cache.
  93. /// </summary>
  94. /// <param name="urlBase">The base URL for the cloud instance. Predefined URLs are available in <see cref="PredefinedHpIdentityEndpoints"/>.</param>
  95. /// <param name="restService">The implementation of <see cref="IRestService"/> to use for executing REST requests. If this value is <see langword="null"/>, the provider will use a new instance of <see cref="JsonRestServices"/>.</param>
  96. /// <param name="tokenCache">The cache to use for caching user access tokens. If this value is <see langword="null"/>, the provider will use <see cref="UserAccessCache.Instance"/>.</param>
  97. /// <exception cref="ArgumentNullException">If <paramref name="urlBase"/> is <see langword="null"/>.</exception>
  98. public HpIdentityProvider(Uri urlBase, IRestService restService, ICache<UserAccess> tokenCache)
  99. : this(urlBase, null, restService, tokenCache)
  100. {
  101. }
  102. /// <summary>
  103. /// Initializes a new instance of the <see cref="HpIdentityProvider"/> class
  104. /// using the provided values.
  105. /// </summary>
  106. /// <param name="urlBase">The base URL for the cloud instance. Predefined URLs are available in <see cref="PredefinedHpIdentityEndpoints"/>.</param>
  107. /// <param name="defaultIdentity">The default identity to use for calls that do not explicitly specify an identity. If this value is <see langword="null"/>, no default identity is available so all calls must specify an explicit identity.</param>
  108. /// <param name="restService">The implementation of <see cref="IRestService"/> to use for executing REST requests. If this value is <see langword="null"/>, the provider will use a new instance of <see cref="JsonRestServices"/>.</param>
  109. /// <param name="tokenCache">The cache to use for caching user access tokens. If this value is <see langword="null"/>, the provider will use <see cref="UserAccessCache.Instance"/>.</param>
  110. /// <exception cref="ArgumentNullException">If <paramref name="urlBase"/> is <see langword="null"/>.</exception>
  111. public HpIdentityProvider(Uri urlBase, CloudIdentity defaultIdentity, IRestService restService, ICache<UserAccess> tokenCache)
  112. : base(defaultIdentity, restService, tokenCache, urlBase)
  113. {
  114. if (urlBase == null)
  115. throw new ArgumentNullException("urlBase");
  116. }
  117. /// <inheritdoc/>
  118. public override UserAccess GetUserAccess(CloudIdentity identity, bool forceCacheRefresh = false)
  119. {
  120. if (identity == null)
  121. throw new ArgumentNullException("identity");
  122. CloudIdentityWithProject identityWithProject = identity as CloudIdentityWithProject;
  123. if (identityWithProject == null)
  124. {
  125. if (identity.GetType() != typeof(CloudIdentity))
  126. throw new NotSupportedException(string.Format("{0} does not support credentials of type {1}", GetType().Name, identity.GetType().Name));
  127. }
  128. Func<UserAccess> refreshCallback =
  129. () =>
  130. {
  131. JObject credentialsObject;
  132. if (!string.IsNullOrEmpty(identity.APIKey))
  133. {
  134. credentialsObject = new JObject(
  135. new JProperty("apiAccessKeyCredentials", new JObject(
  136. new JProperty("accessKey", identity.APIKey),
  137. new JProperty("secretKey", identity.Password))));
  138. }
  139. else
  140. {
  141. credentialsObject = new JObject(
  142. new JProperty("passwordCredentials", new JObject(
  143. new JProperty("username", identity.Username),
  144. new JProperty("password", identity.Password))));
  145. }
  146. JObject authObject = new JObject(credentialsObject);
  147. if (identityWithProject != null && identityWithProject.ProjectId != null)
  148. authObject.Add("tenantId", JToken.FromObject(identityWithProject.ProjectId));
  149. if (identityWithProject != null && !string.IsNullOrEmpty(identityWithProject.ProjectName))
  150. authObject.Add("tenantName", JToken.FromObject(identityWithProject.ProjectName));
  151. JObject requestBody = new JObject(
  152. new JProperty("auth", authObject));
  153. var response = ExecuteRESTRequest<JObject>(identity, new Uri(UrlBase, "/v2.0/tokens"), HttpMethod.POST, requestBody, isTokenRequest: true);
  154. if (response == null || response.Data == null)
  155. return null;
  156. JToken userAccessObject = response.Data["access"];
  157. if (userAccessObject == null)
  158. return null;
  159. UserAccess access = userAccessObject.ToObject<UserAccess>();
  160. if (access == null || access.Token == null)
  161. return null;
  162. return access;
  163. };
  164. string key = string.Format("{0}:{1}/{2}/{3}/{4}", UrlBase, identityWithProject != null ? identityWithProject.ProjectId : null, identity.Username, identity.APIKey, identity.Password);
  165. var userAccess = TokenCache.Get(key, refreshCallback, forceCacheRefresh);
  166. return userAccess;
  167. }
  168. /// <inheritdoc />
  169. protected override string LookupServiceTypeKey(IServiceType serviceType)
  170. {
  171. if (ServiceType.ContentDeliveryNetwork.Equals(serviceType))
  172. return "hpext:cdn";
  173. return serviceType.Type;
  174. }
  175. }
  176. }