Created
November 21, 2013 14:48
-
-
Save sharwell/7582848 to your computer and use it in GitHub Desktop.
This example shows a `CustomIdentityProvider` class which can perform OpenStack authentication with the `tenantId` and/or `tenantName` properties which are not supported by the default `CloudIdentityProvider`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CloudIdentityWithProject : CloudIdentity | |
{ | |
/// <summary> | |
/// Gets or sets the project ID for this identity. | |
/// </summary> | |
/// <value> | |
/// The project ID for this identity. The value may be <c>null</c> if the particular | |
/// provider supports authenticating without a project ID. | |
/// </value> | |
public ProjectId ProjectId | |
{ | |
get; | |
set; | |
} | |
/// <summary> | |
/// Gets or sets the project name for this identity. | |
/// </summary> | |
/// <value> | |
/// The project name for this identity. The value may be <c>null</c> if the particular | |
/// provider supports authenticating without a project name. | |
/// </value> | |
public string ProjectName | |
{ | |
get; | |
set; | |
} | |
} | |
public class CustomIdentityProvider : CloudIdentityProvider | |
{ | |
public override UserAccess GetUserAccess(CloudIdentity identity, bool forceCacheRefresh = false) | |
{ | |
CloudIdentityWithProject identityWithProject = identity as CloudIdentityWithProject; | |
if (identityWithProject == null) | |
return base.GetUserAccess(identityWithProject, forceCacheRefresh); | |
Func<UserAccess> refreshCallback = | |
() => | |
{ | |
JObject requestBody = new JObject( | |
new JProperty("auth", new JObject( | |
new JProperty("passwordCredentials", new JObject( | |
new JProperty("username", JValue.CreateString(identityWithProject.Username)), | |
new JProperty("password", JValue.CreateString(identityWithProject.Password)))), | |
new JProperty("tenantName", JToken.FromObject(identityWithProject.ProjectName)), | |
new JProperty("tenantId", JToken.FromObject(identityWithProject.ProjectId))))); | |
var response = ExecuteRESTRequest<JObject>(identity, new Uri(UrlBase, "/v2.0/tokens"), HttpMethod.POST, requestBody, isTokenRequest: true); | |
if (response == null || response.Data == null) | |
return null; | |
JToken userAccessObject = response.Data["access"]; | |
if (userAccessObject == null) | |
return null; | |
UserAccess access = userAccessObject.ToObject<UserAccess>(); | |
if (access == null || access.Token == null) | |
return null; | |
return access; | |
}; | |
string key = string.Format("{0}:{1}/{2}", UrlBase, identityWithProject.ProjectId, identityWithProject.Username); | |
var userAccess = TokenCache.Get(key, refreshCallback, forceCacheRefresh); | |
return userAccess; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment