|
| 1 | +namespace net.openstack.Providers.Hp |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using net.openstack.Core.Caching; |
| 5 | + using net.openstack.Core.Domain; |
| 6 | + using Newtonsoft.Json.Linq; |
| 7 | + using CloudIdentityProvider = net.openstack.Providers.Rackspace.CloudIdentityProvider; |
| 8 | + using HttpMethod = JSIStudios.SimpleRESTServices.Client.HttpMethod; |
| 9 | + using IIdentityProvider = net.openstack.Core.Providers.IIdentityProvider; |
| 10 | + using IRestService = JSIStudios.SimpleRESTServices.Client.IRestService; |
| 11 | + using JsonRestServices = JSIStudios.SimpleRESTServices.Client.Json.JsonRestServices; |
| 12 | + |
| 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 | + |
| 35 | + /// <summary> |
| 36 | + /// Initializes a new instance of the <see cref="HpIdentityProvider"/> class |
| 37 | + /// with the specified default identity, the <see cref="PredefinedHpIdentityEndpoints.Default"/> base URL, and the default REST service |
| 38 | + /// implementation and token cache. |
| 39 | + /// </summary> |
| 40 | + /// <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> |
| 41 | + public HpIdentityProvider(CloudIdentity defaultIdentity) |
| 42 | + : this(PredefinedHpIdentityEndpoints.Default, defaultIdentity, null, null) |
| 43 | + { |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Initializes a new instance of the <see cref="HpIdentityProvider"/> class |
| 48 | + /// with no default identity, the <see cref="PredefinedHpIdentityEndpoints.Default"/> base URL, and the default REST service |
| 49 | + /// implementation, and token cache. |
| 50 | + /// </summary> |
| 51 | + /// <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> |
| 52 | + /// <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> |
| 53 | + public HpIdentityProvider(IRestService restService, ICache<UserAccess> tokenCache) |
| 54 | + : this(PredefinedHpIdentityEndpoints.Default, null, restService, tokenCache) |
| 55 | + { |
| 56 | + } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Initializes a new instance of the <see cref="HpIdentityProvider"/> class |
| 60 | + /// using the <see cref="PredefinedHpIdentityEndpoints.Default"/> base URL and provided values. |
| 61 | + /// </summary> |
| 62 | + /// <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> |
| 63 | + /// <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> |
| 64 | + /// <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> |
| 65 | + public HpIdentityProvider(CloudIdentity defaultIdentity, IRestService restService, ICache<UserAccess> tokenCache) |
| 66 | + : this(PredefinedHpIdentityEndpoints.Default, defaultIdentity, restService, tokenCache) |
| 67 | + { |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Initializes a new instance of the <see cref="HpIdentityProvider"/> class |
| 72 | + /// with no default identity, the specified base URL, and the default REST service |
| 73 | + /// implementation and token cache. |
| 74 | + /// </summary> |
| 75 | + /// <param name="urlBase">The base URL for the cloud instance. Predefined URLs are available in <see cref="PredefinedHpIdentityEndpoints"/>.</param> |
| 76 | + /// <exception cref="ArgumentNullException">If <paramref name="urlBase"/> is <see langword="null"/>.</exception> |
| 77 | + public HpIdentityProvider(Uri urlBase) |
| 78 | + : this(urlBase, null, null, null) |
| 79 | + { |
| 80 | + } |
| 81 | + |
| 82 | + /// <summary> |
| 83 | + /// Initializes a new instance of the <see cref="HpIdentityProvider"/> class |
| 84 | + /// with the specified default identity and base URL, and the default REST service |
| 85 | + /// implementation and token cache. |
| 86 | + /// </summary> |
| 87 | + /// <param name="urlBase">The base URL for the cloud instance. Predefined URLs are available in <see cref="PredefinedHpIdentityEndpoints"/>.</param> |
| 88 | + /// <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> |
| 89 | + /// <exception cref="ArgumentNullException">If <paramref name="urlBase"/> is <see langword="null"/>.</exception> |
| 90 | + public HpIdentityProvider(Uri urlBase, CloudIdentity defaultIdentity) |
| 91 | + : this(urlBase, defaultIdentity, null, null) |
| 92 | + { |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Initializes a new instance of the <see cref="HpIdentityProvider"/> class |
| 97 | + /// with no default identity, and the specified base URL, REST service |
| 98 | + /// implementation, and token cache. |
| 99 | + /// </summary> |
| 100 | + /// <param name="urlBase">The base URL for the cloud instance. Predefined URLs are available in <see cref="PredefinedHpIdentityEndpoints"/>.</param> |
| 101 | + /// <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> |
| 102 | + /// <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> |
| 103 | + /// <exception cref="ArgumentNullException">If <paramref name="urlBase"/> is <see langword="null"/>.</exception> |
| 104 | + public HpIdentityProvider(Uri urlBase, IRestService restService, ICache<UserAccess> tokenCache) |
| 105 | + : this(urlBase, null, restService, tokenCache) |
| 106 | + { |
| 107 | + } |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// Initializes a new instance of the <see cref="HpIdentityProvider"/> class |
| 111 | + /// using the provided values. |
| 112 | + /// </summary> |
| 113 | + /// <param name="urlBase">The base URL for the cloud instance. Predefined URLs are available in <see cref="PredefinedHpIdentityEndpoints"/>.</param> |
| 114 | + /// <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> |
| 115 | + /// <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> |
| 116 | + /// <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> |
| 117 | + /// <exception cref="ArgumentNullException">If <paramref name="urlBase"/> is <see langword="null"/>.</exception> |
| 118 | + public HpIdentityProvider(Uri urlBase, CloudIdentity defaultIdentity, IRestService restService, ICache<UserAccess> tokenCache) |
| 119 | + : base(defaultIdentity, restService, tokenCache, urlBase) |
| 120 | + { |
| 121 | + if (urlBase == null) |
| 122 | + throw new ArgumentNullException("urlBase"); |
| 123 | + } |
| 124 | + |
| 125 | + /// <inheritdoc/> |
| 126 | + public override UserAccess GetUserAccess(CloudIdentity identity, bool forceCacheRefresh = false) |
| 127 | + { |
| 128 | + if (identity == null) |
| 129 | + throw new ArgumentNullException("identity"); |
| 130 | + |
| 131 | + CloudIdentityWithProject identityWithProject = identity as CloudIdentityWithProject; |
| 132 | + if (identityWithProject == null) |
| 133 | + { |
| 134 | + if (identity.GetType() != typeof(CloudIdentity)) |
| 135 | + throw new NotSupportedException(string.Format("{0} does not support credentials of type {1}", GetType().Name, identity.GetType().Name)); |
| 136 | + } |
| 137 | + |
| 138 | + Func<UserAccess> refreshCallback = |
| 139 | + () => |
| 140 | + { |
| 141 | + JObject credentialsObject; |
| 142 | + if (!string.IsNullOrEmpty(identity.APIKey)) |
| 143 | + { |
| 144 | + credentialsObject = new JObject( |
| 145 | + new JProperty("apiAccessKeyCredentials", new JObject( |
| 146 | + new JProperty("accessKey", identity.APIKey), |
| 147 | + new JProperty("secretKey", identity.Password)))); |
| 148 | + } |
| 149 | + else |
| 150 | + { |
| 151 | + credentialsObject = new JObject( |
| 152 | + new JProperty("passwordCredentials", new JObject( |
| 153 | + new JProperty("username", identity.Username), |
| 154 | + new JProperty("password", identity.Password)))); |
| 155 | + } |
| 156 | + |
| 157 | + JObject authObject = new JObject(credentialsObject); |
| 158 | + if (identityWithProject != null && identityWithProject.ProjectId != null) |
| 159 | + authObject.Add("tenantId", JToken.FromObject(identityWithProject.ProjectId)); |
| 160 | + if (identityWithProject != null && !string.IsNullOrEmpty(identityWithProject.ProjectName)) |
| 161 | + authObject.Add("tenantName", JToken.FromObject(identityWithProject.ProjectName)); |
| 162 | + |
| 163 | + JObject requestBody = new JObject( |
| 164 | + new JProperty("auth", authObject)); |
| 165 | + |
| 166 | + var response = ExecuteRESTRequest<JObject>(identity, new Uri(UrlBase, "/v2.0/tokens"), HttpMethod.POST, requestBody, isTokenRequest: true); |
| 167 | + if (response == null || response.Data == null) |
| 168 | + return null; |
| 169 | + |
| 170 | + JToken userAccessObject = response.Data["access"]; |
| 171 | + if (userAccessObject == null) |
| 172 | + return null; |
| 173 | + |
| 174 | + UserAccess access = userAccessObject.ToObject<UserAccess>(); |
| 175 | + if (access == null || access.Token == null) |
| 176 | + return null; |
| 177 | + |
| 178 | + return access; |
| 179 | + }; |
| 180 | + string key = string.Format("{0}:{1}/{2}/{3}/{4}", UrlBase, identityWithProject != null ? identityWithProject.ProjectId : null, identity.Username, identity.APIKey, identity.Password); |
| 181 | + var userAccess = TokenCache.Get(key, refreshCallback, forceCacheRefresh); |
| 182 | + |
| 183 | + return userAccess; |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments