public void TestGetUserAccess() { IIdentityProvider provider = Bootstrapper.CreateIdentityProvider(); UserAccess userAccess = provider.GetUserAccess(); Assert.IsNotNull(userAccess); Assert.IsNotNull(userAccess.Token); // ensure the provider is caching the access token UserAccess cachedUserAccess = provider.GetUserAccess(); Assert.AreSame(userAccess, cachedUserAccess); // ensure that the provider refreshes the userAccess upon request UserAccess newUserAccess = provider.GetUserAccess(forceCacheRefresh: true); Assert.AreNotSame(userAccess, newUserAccess); // ensure the the refresh was applied to the cache UserAccess newCachedUserAccess = provider.GetUserAccess(); Assert.AreSame(newUserAccess, newCachedUserAccess); }
protected Endpoint GetServiceEndpoint(CloudIdentity identity, string serviceName, string region = null) { identity = GetDefaultIdentity(identity); var userAccess = IdentityProvider.GetUserAccess(identity); if (userAccess == null || userAccess.ServiceCatalog == null) { throw new UserAuthenticationException("Unable to authenticate user and retrieve authorized service endpoints."); } var serviceDetails = userAccess.ServiceCatalog.FirstOrDefault(sc => sc.Name == serviceName); if (serviceDetails == null || serviceDetails.Endpoints == null || serviceDetails.Endpoints.Length == 0) { throw new UserAuthorizationException("The user does not have access to the requested service."); } if (string.IsNullOrWhiteSpace(region)) { var isLondon = IsLondonIdentity(identity); region = string.IsNullOrWhiteSpace(userAccess.User.DefaultRegion) ? isLondon ? "LON" : null : userAccess.User.DefaultRegion; if (string.IsNullOrWhiteSpace(region)) { throw new NoDefaultRegionSetException("No region was provided and there is no default region set for the user's account."); } } var endpoint = serviceDetails.Endpoints.FirstOrDefault(e => e.Region.Equals(region, StringComparison.OrdinalIgnoreCase)) ?? serviceDetails.Endpoints.FirstOrDefault(e => string.IsNullOrWhiteSpace(e.Region)); if (endpoint == null) { throw new UserAuthorizationException("The user does not have access to the requested service or region."); } return(endpoint); }
public void TestGetRolesByUser() { IIdentityProvider provider = Bootstrapper.CreateIdentityProvider(); UserAccess userAccess = provider.GetUserAccess(); Assert.IsNotNull(userAccess); Assert.IsNotNull(userAccess.User); IEnumerable <Role> roles = provider.GetRolesByUser(userAccess.User.Id); Assert.IsNotNull(roles); Assert.IsTrue(roles.Any()); foreach (Role role in roles) { Console.WriteLine("Role \"{0}\" (id: {1})", role.Name, role.Id); if (!string.IsNullOrEmpty(role.Description)) { Console.WriteLine(" Description: {0}", role.Description); } } }