Skip to content

Commit

Permalink
Release 2.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
wparad committed Oct 8, 2024
1 parent f19f3f3 commit 3f82a6f
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 97 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
This is the changelog for [Authress SDK](readme.md).

## 2.0 ##
* Renamed `AccessRecordStatements` and other models that end with `S` but aren't actually plural to be `AccessRecordStatement` (without the `S`).
* All APIs are now part of sub instance properties of the `AuthressClient` class, `AccessClient.AccessRecords` and `AccessClient.ServiceClients`, etc..
* `ApiBasePath` has been renamed to `AuthressApiUrl`.
* `HttpClientSettings` Has been removed in favor of `AuthressSettings` Class.
* [Breaking] `UserPermissions.GetUserResources()` no longer returns the property `AccessToAllSubResources`. When a user only has access to parent resources, the list will always be empty unless the `CollectionConfigurationEnum` property is specified.

## 1.5 ##
* Fix `DateTimeOffset` type assignments, properties that were incorrectly defined as `DateTime` are now correctly `DateTimeOffsets`.
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ The recommended solution is to use the C# built in OpenID provider by Microsoft.
using Authress.SDK;

// Get an authress custom domain: https://authress.io/app/#/settings?focus=domain
var authressSettings = new AuthressSettings { ApiBasePath = "https://authress.company.com", };
var authressSettings = new AuthressSettings { AuthressApiUrl = "https://authress.company.com", };
var authressClient = new AuthressClient(tokenProvider, authressSettings)

var verifiedUserIdentity = await authressClient.VerifyToken(jwtToken);
Expand Down Expand Up @@ -60,7 +60,7 @@ namespace Microservice
return accessToken;
});
// Get an authress custom domain: https://authress.io/app/#/settings?focus=domain
var authressSettings = new AuthressSettings { ApiBasePath = "https://authress.company.com", };
var authressSettings = new AuthressSettings { AuthressApiUrl = "https://authress.company.com", };
var authressClient = new AuthressClient(tokenProvider, authressSettings);

// 2. At runtime attempt to Authorize the user for the resource
Expand All @@ -86,7 +86,7 @@ namespace Microservice
// automatically populate forward the users token
// 1. instantiate all the necessary classes
var tokenProvider = new ManualTokenProvider();
var authressSettings = new AuthressSettings { ApiBasePath = "https://DOMAIN.api.authress.io", };
var authressSettings = new AuthressSettings { AuthressApiUrl = "https://DOMAIN.api.authress.io", };
var authressClient = new AuthressClient(tokenProvider, authressSettings);

// 2. At runtime attempt to Authorize the user for the resource
Expand Down Expand Up @@ -117,7 +117,7 @@ namespace Microservice
var decodedAccessKey = decrypt(accessKey);
var tokenProvider = new AuthressClientTokenProvider(decodedAccessKey);
// Get an authress custom domain: https://authress.io/app/#/settings?focus=domain
var authressSettings = new AuthressSettings { ApiBasePath = "https://authress.company.com", };
var authressSettings = new AuthressSettings { AuthressApiUrl = "https://authress.company.com", };
var authressClient = new AuthressClient(tokenProvider, authressSettings);

// Attempt to Authorize the user for the resource
Expand Down
2 changes: 1 addition & 1 deletion src/Authress.SDK/Api/IUserPermissionsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Authress.SDK.Api
public interface IUserPermissionsApi
{
/// <summary>
/// Get the users resources. Get the users resources. This result is a list of resource uris that a user has an explicit permission to, a user with * access to all sub resources will return an empty list and {AccessToAllSubResources} will be populated. The list will be paginated.
/// Get the users resources. This result is a list of resource uris that a user has an permission to. By default only the top level matching resources are returned. To get a user's list of deeply nested resources, set the collectionConfiguration to be INCLUDE_NESTED. This collection is paginated.
/// </summary>
/// <param name="userId">The user to check permissions on</param>
/// <param name="resourceCollectionUri">The uri path of a collection resource to fetch permissions for.</param>
Expand Down
60 changes: 13 additions & 47 deletions src/Authress.SDK/Api/UserPermissionsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public async Task AuthorizeUser (string userId, string resourceUri, string permi
}

/// <summary>
/// Get the users resources. Get the users resources. This result is a list of resource uris that a user has an explicit permission to, a user with * access to all sub resources will return an empty list and {AccessToAllSubResources} will be populated. The list will be paginated.
/// Get the users resources. This result is a list of resource uris that a user has an permission to. By default only the top level matching resources are returned. To get a user's list of deeply nested resources, set the collectionConfiguration to be INCLUDE_NESTED. This collection is paginated.
/// </summary>
/// <param name="userId">The user to check permissions on</param>
/// <param name="resourceCollectionUri">The uri path of a collection resource to fetch permissions for.</param>
Expand All @@ -152,56 +152,22 @@ public async Task<UserResources> GetUserResources(string userId, string resource
throw new ArgumentNullException("Missing required parameter 'userId'.");
}

if (collectionConfiguration == CollectionConfigurationEnum.INCLUDE_NESTED)
var queryParams = new Dictionary<string, string>
{
var queryParams = new Dictionary<string, string>
{
{ "resourceUri", resourceCollectionUri },
{ "permissions", permission },
{ "collectionConfiguration", collectionConfiguration.ToString() }
};
{ "resourceUri", resourceCollectionUri },
{ "permissions", permission },
{ "collectionConfiguration", collectionConfiguration.ToString() }
};

var queryString = queryParams.Where(pair => !string.IsNullOrEmpty(pair.Value))
.Select(pair => $"{pair.Key}={System.Web.HttpUtility.UrlEncode(pair.Value)}").Aggregate((next, total) => $"{total}&{next}");
var path = $"/v1/users/{System.Web.HttpUtility.UrlEncode(userId)}/resources?{queryString}";
var queryString = queryParams.Where(pair => !string.IsNullOrEmpty(pair.Value))
.Select(pair => $"{pair.Key}={System.Web.HttpUtility.UrlEncode(pair.Value)}").Aggregate((next, total) => $"{total}&{next}");
var path = $"/v1/users/{System.Web.HttpUtility.UrlEncode(userId)}/resources?{queryString}";

var client = await authressHttpClientProvider.GetHttpClientAsync();
using (var response = await client.GetAsync(path))
{
await response.ThrowIfNotSuccessStatusCode();
return await response.Content.ReadAsAsync<UserResources>();
}
}
else
var client = await authressHttpClientProvider.GetHttpClientAsync();
using (var response = await client.GetAsync(path))
{

var queryParams = new Dictionary<string, string>
{
{ "resourceUri", resourceCollectionUri },
{ "permissions", permission }
};

var queryString = queryParams.Where(pair => !string.IsNullOrEmpty(pair.Value))
.Select(pair => $"{pair.Key}={System.Web.HttpUtility.UrlEncode(pair.Value)}").Aggregate((next, total) => $"{total}&{next}");
var path = $"/v1/users/{System.Web.HttpUtility.UrlEncode(userId)}/resources?{queryString}";

var client = await authressHttpClientProvider.GetHttpClientAsync();

var authorizeUserAsync = AuthorizeUser(userId, resourceCollectionUri, permission);
using (var response = await client.GetAsync(path))
{
try
{
await authorizeUserAsync;
return new UserResources { UserId = userId, AccessToAllSubResources = true, Resources = null };
}
catch (Exception)
{
/* Ignore if the user doesn't have permission or if there is a problem, instead fallback to looking up explicit resources by permission */
}
await response.ThrowIfNotSuccessStatusCode();
return await response.Content.ReadAsAsync<UserResources>();
}
await response.ThrowIfNotSuccessStatusCode();
return await response.Content.ReadAsAsync<UserResources>();
}
}
}
Expand Down
16 changes: 1 addition & 15 deletions src/Authress.SDK/Client/AuthressClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,7 @@ public AuthressClient(ITokenProvider tokenProvider, AuthressSettings settings, I
throw new ArgumentNullException("Missing required parameter AuthressSettings");
}
authressHttpClientProvider = new HttpClientProvider(settings, tokenProvider, customHttpClientHandlerFactory);
tokenVerifier = new TokenVerifier(settings.ApiBasePath, authressHttpClientProvider);
}

/// <summary>
/// Deprecated Constructor
/// </summary>
public AuthressClient(ITokenProvider tokenProvider, HttpClientSettings settings, IHttpClientHandlerFactory customHttpClientHandlerFactory = null)
{
if (settings == null) {
throw new ArgumentNullException("Missing required parameter HttpClientSettings");
}
authressHttpClientProvider = new HttpClientProvider(
new AuthressSettings { ApiBasePath = settings.ApiBasePath, RequestTimeout = settings.RequestTimeout },
tokenProvider, customHttpClientHandlerFactory);
tokenVerifier = new TokenVerifier(settings.ApiBasePath, authressHttpClientProvider);
tokenVerifier = new TokenVerifier(settings.AuthressApiUrl, authressHttpClientProvider);
}

/// <summary>
Expand Down
30 changes: 7 additions & 23 deletions src/Authress.SDK/Client/HttpClientProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,19 @@ public interface IHttpClientHandlerFactory
HttpClientHandler Create();
}

/// <summary>
/// Authress Domain Host: https://authress.company.com (Get an authress custom domain: https://authress.io/app/#/settings?focus=domain)
/// </summary>
public class HttpClientSettings
{
/// <summary>
/// Authress Domain Host: https://authress.company.com (Get an authress custom domain: https://authress.io/app/#/settings?focus=domain)
/// </summary>
public string ApiBasePath { get; set; } = "https://api.authress.io";

/// <summary>
/// Timeout for requests to Authress. Default is unset.
/// </summary>
public TimeSpan? RequestTimeout { get; set; } = null;
}

/// <summary>
/// Authress Domain Host: https://authress.company.com (Get an authress custom domain: https://authress.io/app/#/settings?focus=domain)
/// </summary>
public class AuthressSettings
{
private string apiBasePath = "https://api.authress.io";
private string authressApiUrl = "https://api.authress.io";
/// <summary>
/// Authress Domain Host: https://authress.company.com (Get an authress custom domain: https://authress.io/app/#/settings?focus=domain)
/// </summary>
public string ApiBasePath {
get { return apiBasePath; }
public string AuthressApiUrl {
get { return authressApiUrl; }
set {
apiBasePath = Sanitizers.SanitizeUrl(value);
authressApiUrl = Sanitizers.SanitizeUrl(value);
}
}

Expand Down Expand Up @@ -116,8 +100,8 @@ public async Task<HttpClient> GetHttpClientAsync()

// List of Handlers that never need to be retried
outermostHandler = new OptimisticPerformanceHandler(outermostHandler, settings.CacheFallbackNormTimeout);
outermostHandler = new RewriteBaseUrlHandler(outermostHandler, settings.ApiBasePath);
outermostHandler = new AddAuthorizationHeaderHandler(outermostHandler, tokenProvider, settings.ApiBasePath);
outermostHandler = new RewriteBaseUrlHandler(outermostHandler, settings.AuthressApiUrl);
outermostHandler = new AddAuthorizationHeaderHandler(outermostHandler, tokenProvider, settings.AuthressApiUrl);
outermostHandler = new AddUserAgentHeaderHandler(outermostHandler);
/**** ⌃ Called First ⌃ ******/

Expand All @@ -128,7 +112,7 @@ public async Task<HttpClient> GetHttpClientAsync()
clientProxy.Timeout = settings.RequestTimeout.Value;
}

clientProxy.BaseAddress = new Uri(settings.ApiBasePath);
clientProxy.BaseAddress = new Uri(settings.AuthressApiUrl);
return clientProxy;
}
finally
Expand Down
5 changes: 0 additions & 5 deletions src/Authress.SDK/Model/UserResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,5 @@ public class UserResources : IPaginationDto
[DataMember(Name = "links", EmitDefaultValue = false)]
[JsonProperty(PropertyName = "links")]
public Links Links { get; set; }

/// <summary>
/// If the user has access to all sub-resources, then instead of resources being a list, this property will be populated `true`.
/// </summary>
public bool AccessToAllSubResources { get; set; } = false;
}
}

0 comments on commit 3f82a6f

Please sign in to comment.