-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ticket #771 : Can resolve trust chain
Can validate trust chain Add some UTS to check the exceptions Start to support automatic registration
- Loading branch information
Showing
66 changed files
with
3,562 additions
and
2,894 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
src/IdServer/SimpleIdServer.IdServer.Federation/Helpers/FederationClientHelper.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright (c) SimpleIdServer. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. | ||
|
||
using Microsoft.Extensions.Logging; | ||
using SimpleIdServer.IdServer.Domains; | ||
using SimpleIdServer.IdServer.Exceptions; | ||
|
||
|
||
|
||
// Copyright (c) SimpleIdServer. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. | ||
|
||
using SimpleIdServer.IdServer.Helpers; | ||
using SimpleIdServer.IdServer.Stores; | ||
using SimpleIdServer.OpenidFederation.Clients; | ||
using SimpleIdServer.OpenidFederation.Stores; | ||
using System.Text.Json; | ||
|
||
namespace SimpleIdServer.IdServer.Federation.Helpers; | ||
|
||
public class FederationClientHelper : StandardClientHelper | ||
{ | ||
private readonly ILogger<FederationClientHelper> _logger; | ||
private readonly IFederationEntityStore _federationEntityStore; | ||
|
||
public FederationClientHelper( | ||
IdServer.Helpers.IHttpClientFactory httpClientFactory, | ||
IClientRepository clientRepository, | ||
ILogger<FederationClientHelper> logger, | ||
IFederationEntityStore federationEntityStore) : base(httpClientFactory, clientRepository) | ||
{ | ||
_logger = logger; | ||
_federationEntityStore = federationEntityStore; | ||
} | ||
|
||
/// <summary> | ||
/// Resolve the client. | ||
/// Two scenarios are supported : | ||
/// 1). Returns the client from the database. | ||
/// 2). Resolve the trust chain and returns the client. | ||
/// </summary> | ||
/// <param name="realm"></param> | ||
/// <param name="clientId"></param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns></returns> | ||
/// <exception cref="OAuthException"></exception> | ||
public override async Task<Client> ResolveClient(string realm, string clientId, CancellationToken cancellationToken) | ||
{ | ||
var result = await base.ResolveClient(realm, clientId, cancellationToken); | ||
if (result == null) | ||
result = await ResolveClientByOpenidFederation(realm, clientId, cancellationToken); | ||
|
||
return result; | ||
} | ||
|
||
private async Task<Client> ResolveClientByOpenidFederation(string realm, string clientId, CancellationToken cancellationToken) | ||
{ | ||
const string metadataName = "openid_relying_party"; | ||
var resolver = TrustChainResolver.New(); | ||
List<OpenidTrustChain> trustChains = null; | ||
try | ||
{ | ||
trustChains = await resolver.ResolveTrustChains(clientId, cancellationToken); | ||
} | ||
catch (Exception ex) | ||
{ | ||
var message = ex.ToString(); | ||
_logger.LogError(message); | ||
throw new OAuthException(ErrorCodes.INVALID_REQUEST, message); | ||
} | ||
|
||
var allAuthorities = await _federationEntityStore.GetAllAuthorities(realm, cancellationToken); | ||
var allTrustAnchors = trustChains.Select(c => c.TrustAnchor); | ||
var filteredTrustChain = trustChains.FirstOrDefault(tc => allAuthorities.Any(at => at.Sub == tc.TrustAnchor.FederationResult.Sub)); | ||
if (filteredTrustChain == null) | ||
throw new OAuthException(ErrorCodes.MISSING_TRUST_ANCHOR, Resources.Global.NoTrustAnchorCanBeResolved); | ||
|
||
|
||
var federationResult = filteredTrustChain.EntityStatements.First().FederationResult; | ||
if (federationResult.Metadata == null || | ||
federationResult.Metadata.OtherParameters == null || | ||
!federationResult.Metadata.OtherParameters.ContainsKey(metadataName)) | ||
throw new OAuthException(ErrorCodes.INVALID_REQUEST, Resources.Global.MissingOpenidRpInEntityStatement); | ||
var client = JsonSerializer.Deserialize<Client>(federationResult.Metadata.OtherParameters[metadataName]); | ||
return client; | ||
} | ||
} |
This file contains 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
18 changes: 18 additions & 0 deletions
18
src/IdServer/SimpleIdServer.IdServer.Federation/Resources/Global.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,48 @@ | ||
// Copyright (c) SimpleIdServer. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. | ||
namespace SimpleIdServer.IdServer | ||
namespace SimpleIdServer.IdServer; | ||
|
||
public static class ErrorCodes | ||
{ | ||
public static class ErrorCodes | ||
{ | ||
public const string INVALID_REQUEST = "invalid_request"; | ||
public const string INVALID_CLIENT = "invalid_client"; | ||
public const string INVALID_REDIRECT_URI = "invalid_redirect_uri"; | ||
public const string INVALID_CLIENT_METADATA = "invalid_client_metadata"; | ||
public const string INVALID_CLIENT_METADATA_URI = "invalid_client_metadata_uri"; | ||
public const string INVALID_SOFTWARE_STATEMENT = "invalid_software_statement"; | ||
public const string INVALID_NETWORK = "invalid_network"; | ||
public const string INVALID_GRANT = "invalid_grant"; | ||
public const string INVALID_TOKEN = "invalid_token"; | ||
public const string INVALID_SCOPE = "invalid_scope"; | ||
public const string INVALID_DEVICE_CODE = "invalid_device_code"; | ||
public const string INVALID_DPOP_PROOF = "invalid_dpop_proof"; | ||
public const string INVALID_REQUEST_OBJECT = "invalid_request_object"; | ||
public const string UNSUPPORTED_GRANT_TYPE = "unsupported_grant_type"; | ||
public const string UNSUPPORTED_TOKEN_TYPE = "unsupported_token_type"; | ||
public const string UNSUPPORTED_RESPONSE_MODE = "unsupported_response_mode"; | ||
public const string UNSUPPORTED_RESPONSE_TYPE = "unsupported_response_type"; | ||
public const string LOGIN_REQUIRED = "login_required"; | ||
public const string ACCESS_DENIED = "access_denied"; | ||
public const string INVALID_BINDING_MESSAGE = "invalid_binding_message"; | ||
public const string EXPIRED_LOGIN_HINT_TOKEN = "expired_login_hint_token"; | ||
public const string UNKNOWN_USER_ID = "unknown_user_id"; | ||
public const string AUTHORIZATION_PENDING = "authorization_pending"; | ||
public const string EXPIRED_TOKEN = "expired_token"; | ||
public const string SLOW_DOWN = "slow_down"; | ||
public const string UNKNOWN_USER = "unknown_user"; | ||
public const string UNKNOWN_ACR = "unknown_acr"; | ||
public const string UNKNOWN_AMR = "unknown_amr"; | ||
public const string INVALID_CREDENTIALS = "invalid_credentials"; | ||
public const string INVALID_TARGET = "invalid_target"; | ||
public const string REQUEST_DENIED = "request_denied"; | ||
public const string INVALID_GRANT_ID = "invalid_grant_id"; | ||
public const string INVALID_RESOURCE_ID = "invalid_resource_id"; | ||
public const string NOT_FOUND = "not_found"; | ||
public const string INVALID_AUTHORIZATION_DETAILS = "invalid_authorization_details"; | ||
public const string UNEXPECTED_ERROR = "unexpected_error"; | ||
public const string USE_DPOP_NONCE = "use_dpop_nonce"; | ||
public const string INACTIVE_SESSION = "inactive_session"; | ||
public const string NO_ACTIVE_OTP = "no_active_otp"; | ||
} | ||
public const string INVALID_REQUEST = "invalid_request"; | ||
public const string INVALID_CLIENT = "invalid_client"; | ||
public const string INVALID_REDIRECT_URI = "invalid_redirect_uri"; | ||
public const string INVALID_CLIENT_METADATA = "invalid_client_metadata"; | ||
public const string INVALID_CLIENT_METADATA_URI = "invalid_client_metadata_uri"; | ||
public const string INVALID_SOFTWARE_STATEMENT = "invalid_software_statement"; | ||
public const string INVALID_NETWORK = "invalid_network"; | ||
public const string INVALID_GRANT = "invalid_grant"; | ||
public const string INVALID_TOKEN = "invalid_token"; | ||
public const string INVALID_SCOPE = "invalid_scope"; | ||
public const string INVALID_DEVICE_CODE = "invalid_device_code"; | ||
public const string INVALID_DPOP_PROOF = "invalid_dpop_proof"; | ||
public const string INVALID_REQUEST_OBJECT = "invalid_request_object"; | ||
public const string UNSUPPORTED_GRANT_TYPE = "unsupported_grant_type"; | ||
public const string UNSUPPORTED_TOKEN_TYPE = "unsupported_token_type"; | ||
public const string UNSUPPORTED_RESPONSE_MODE = "unsupported_response_mode"; | ||
public const string UNSUPPORTED_RESPONSE_TYPE = "unsupported_response_type"; | ||
public const string LOGIN_REQUIRED = "login_required"; | ||
public const string ACCESS_DENIED = "access_denied"; | ||
public const string INVALID_BINDING_MESSAGE = "invalid_binding_message"; | ||
public const string EXPIRED_LOGIN_HINT_TOKEN = "expired_login_hint_token"; | ||
public const string UNKNOWN_USER_ID = "unknown_user_id"; | ||
public const string AUTHORIZATION_PENDING = "authorization_pending"; | ||
public const string EXPIRED_TOKEN = "expired_token"; | ||
public const string SLOW_DOWN = "slow_down"; | ||
public const string UNKNOWN_USER = "unknown_user"; | ||
public const string UNKNOWN_ACR = "unknown_acr"; | ||
public const string UNKNOWN_AMR = "unknown_amr"; | ||
public const string INVALID_CREDENTIALS = "invalid_credentials"; | ||
public const string INVALID_TARGET = "invalid_target"; | ||
public const string REQUEST_DENIED = "request_denied"; | ||
public const string INVALID_GRANT_ID = "invalid_grant_id"; | ||
public const string INVALID_RESOURCE_ID = "invalid_resource_id"; | ||
public const string NOT_FOUND = "not_found"; | ||
public const string INVALID_AUTHORIZATION_DETAILS = "invalid_authorization_details"; | ||
public const string UNEXPECTED_ERROR = "unexpected_error"; | ||
public const string USE_DPOP_NONCE = "use_dpop_nonce"; | ||
public const string INACTIVE_SESSION = "inactive_session"; | ||
public const string NO_ACTIVE_OTP = "no_active_otp"; | ||
public const string VALIDATION_FAILED = "validation_failed"; | ||
public const string MISSING_TRUST_ANCHOR = "missing_trust_anchor"; | ||
} |
This file contains 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
9 changes: 9 additions & 0 deletions
9
src/IdServer/SimpleIdServer.IdServer/Resources/Global.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.