Skip to content

Commit

Permalink
Applying Code Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
olivier-spinelli authored and CKli committed Oct 7, 2024
1 parent a4a8ba1 commit 0f706c7
Show file tree
Hide file tree
Showing 61 changed files with 5,165 additions and 5,212 deletions.
38 changes: 32 additions & 6 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ root = true
[*]
charset = utf-8
indent_style = space
indent_size = 2
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true
dotnet_style_operator_placement_when_wrapping = beginning_of_line
tab_width = 4
end_of_line = crlf


[*.{cs,js,ts,sql,tql}]
Expand All @@ -21,15 +24,42 @@ csharp_space_between_method_call_parameter_list_parentheses = true
csharp_space_between_method_declaration_parameter_list_parentheses = true
csharp_space_after_keywords_in_control_flow_statements = false
csharp_space_between_parentheses = control_flow_statements
csharp_space_around_binary_operators = before_and_after
# Motive: May be weird at first, but it improves readability.

csharp_style_prefer_primary_constructors = false:suggestion
# Primary constructors should be used only for very simple classes. May be record is a good choice.

csharp_indent_labels = no_change
# When using goto, labels should be explicitly positioned based on the algorithm.

csharp_using_directive_placement = outside_namespace:silent
# Rather standard placement of using in C#.

csharp_indent_case_contents_when_block = false;
# switch case block don't need another indent.

csharp_prefer_braces = true:silent

csharp_style_prefer_method_group_conversion = true:silent
csharp_style_expression_bodied_methods = false:silent
csharp_style_expression_bodied_constructors = false:silent
csharp_style_expression_bodied_operators = false:silent
csharp_style_expression_bodied_properties = true:silent
csharp_style_expression_bodied_indexers = true:silent
csharp_style_expression_bodied_accessors = true:silent
csharp_style_expression_bodied_lambdas = true:silent

csharp_style_prefer_top_level_statements = true:suggestion
# Applies to Main().

csharp_style_namespace_declarations=file_scoped:suggestion
#Motive: Less useless space.

# internal and private fields should be _camelCase
dotnet_naming_rule.camel_case_for_private_internal_fields.severity = suggestion
dotnet_naming_rule.camel_case_for_private_internal_fields.symbols = private_internal_fields
dotnet_naming_rule.camel_case_for_private_internal_fields.style = camel_case_underscore_style
dotnet_naming_rule.camel_case_for_private_internal_fields.style = camel_case_underscore_style

dotnet_naming_symbols.private_internal_fields.applicable_kinds = field
dotnet_naming_symbols.private_internal_fields.applicable_accessibilities = private, internal
Expand All @@ -38,9 +68,6 @@ dotnet_naming_style.camel_case_underscore_style.required_prefix = _
dotnet_naming_style.camel_case_underscore_style.capitalization = camel_case
# Motive: It follow the C# style guideline.

csharp_style_namespace_declarations=file_scoped:suggestion
#Motive: Less useless space.

# CA1063: Implement IDisposable Correctly
dotnet_diagnostic.CA1063.severity = none
# CA1816: Dispose methods should call SuppressFinalize
Expand Down Expand Up @@ -118,6 +145,5 @@ dotnet_diagnostic.VSTHRD101.severity = error
# VSTHRD003: Avoid awaiting foreign Tasks
dotnet_diagnostic.VSTHRD003.severity = none


# /Signature-Code .editorconfig

45 changes: 22 additions & 23 deletions CK.AspNet.Auth/AuthenticationCookieMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,32 @@
using System.Collections.Generic;
using System.Text;

namespace CK.AspNet.Auth
{
namespace CK.AspNet.Auth;


/// <summary>
/// Describes the how the authentication cookie is managed.
/// </summary>
public enum AuthenticationCookieMode
{
/// <summary>
/// Describes the how the authentication cookie is managed.
/// The authentication cookie <see cref="CookieOptions.Path"/> is set on the <see cref="WebFrontAuthOptions.EntryPath"/>/c/.
/// This is the default mode.
/// </summary>
public enum AuthenticationCookieMode
{
/// <summary>
/// The authentication cookie <see cref="CookieOptions.Path"/> is set on the <see cref="WebFrontAuthOptions.EntryPath"/>/c/.
/// This is the default mode.
/// </summary>
WebFrontPath = 0,
WebFrontPath = 0,

/// <summary>
/// The authentication cookie <see cref="CookieOptions.Path"/> is set on the root path:
/// this enables the <see cref="WebFrontAuthService"/> to act as a standard Cookie authentication
/// service (applies to classical, server rendered, web site).
/// </summary>
RootPath = 1,
/// <summary>
/// The authentication cookie <see cref="CookieOptions.Path"/> is set on the root path:
/// this enables the <see cref="WebFrontAuthService"/> to act as a standard Cookie authentication
/// service (applies to classical, server rendered, web site).
/// </summary>
RootPath = 1,

/// <summary>
/// No authentication cookie is set (and no challenge is done).
/// This also forces the <see cref="WebFrontAuthOptions.UseLongTermCookie"/> to be false: this ensures that
/// the long term cookie is also removed.
/// </summary>
None = 2
/// <summary>
/// No authentication cookie is set (and no challenge is done).
/// This also forces the <see cref="WebFrontAuthOptions.UseLongTermCookie"/> to be false: this ensures that
/// the long term cookie is also removed.
/// </summary>
None = 2

}
}
196 changes: 97 additions & 99 deletions CK.AspNet.Auth/AuthenticationInfoTokenService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,114 +4,112 @@
using System;
using System.Diagnostics;

namespace CK.AspNet.Auth
{
/// <summary>
/// Simple singleton service that offers tokens creation and restoration functionalities.
/// <para>
/// This is not specific to the global DI container, it is available from all containers.
/// </para>
/// </summary>
public sealed class AuthenticationInfoTokenService : ISingletonAutoService
{
readonly IAuthenticationTypeSystem _typeSystem;
readonly IDataProtector _baseDataProtector;
readonly IDataProtector _tokenDataProtector;
readonly FrontAuthenticationInfoSecureDataFormat _frontTokenFormat;
namespace CK.AspNet.Auth;

public AuthenticationInfoTokenService( IAuthenticationTypeSystem typeSystem, IDataProtectionProvider dataProtectionProvider )
{
_typeSystem = typeSystem;
Throw.DebugAssert( typeof( WebFrontAuthHandler ).FullName == "CK.AspNet.Auth.WebFrontAuthHandler" );
_baseDataProtector = dataProtectionProvider.CreateProtector( "CK.AspNet.Auth.WebFrontAuthHandler" );
_tokenDataProtector = _baseDataProtector.CreateProtector( "Token", "v1" );
_frontTokenFormat = new FrontAuthenticationInfoSecureDataFormat( _typeSystem, _tokenDataProtector );
}
/// <summary>
/// Simple singleton service that offers tokens creation and restoration functionalities.
/// <para>
/// This is not specific to the global DI container, it is available from all containers.
/// </para>
/// </summary>
public sealed class AuthenticationInfoTokenService : ISingletonAutoService
{
readonly IAuthenticationTypeSystem _typeSystem;
readonly IDataProtector _baseDataProtector;
readonly IDataProtector _tokenDataProtector;
readonly FrontAuthenticationInfoSecureDataFormat _frontTokenFormat;

/// <summary>
/// Gets the type system service.
/// </summary>
public IAuthenticationTypeSystem TypeSystem => _typeSystem;
public AuthenticationInfoTokenService( IAuthenticationTypeSystem typeSystem, IDataProtectionProvider dataProtectionProvider )
{
_typeSystem = typeSystem;
Throw.DebugAssert( typeof( WebFrontAuthHandler ).FullName == "CK.AspNet.Auth.WebFrontAuthHandler" );
_baseDataProtector = dataProtectionProvider.CreateProtector( "CK.AspNet.Auth.WebFrontAuthHandler" );
_tokenDataProtector = _baseDataProtector.CreateProtector( "Token", "v1" );
_frontTokenFormat = new FrontAuthenticationInfoSecureDataFormat( _typeSystem, _tokenDataProtector );
}

/// <summary>
/// Gets the data protector to use for authentication tokens.
/// </summary>
public IDataProtector TokenDataProtector => _tokenDataProtector;
/// <summary>
/// Gets the type system service.
/// </summary>
public IAuthenticationTypeSystem TypeSystem => _typeSystem;

/// <summary>
/// Base data protector for authentication related protected data.
/// </summary>
public IDataProtector BaseDataProtector => _baseDataProtector;
/// <summary>
/// Gets the data protector to use for authentication tokens.
/// </summary>
public IDataProtector TokenDataProtector => _tokenDataProtector;

/// <summary>
/// Creates a token from a <see cref="FrontAuthenticationInfo"/>.
/// </summary>
/// <param name="info">The authentication info.</param>
/// <returns>The url-safe secured authentication token string.</returns>
public string ProtectFrontAuthenticationInfo( FrontAuthenticationInfo info )
{
Debug.Assert( info.Info != null );
return _frontTokenFormat.Protect( info );
}
/// <summary>
/// Base data protector for authentication related protected data.
/// </summary>
public IDataProtector BaseDataProtector => _baseDataProtector;

/// <summary>
/// Extracts a <see cref="FrontAuthenticationInfo"/> from a token previously created with <see cref="ProtectFrontAuthenticationInfo(FrontAuthenticationInfo)"/>.
/// <para>
/// By default, the expiration is checked based on <see cref="DateTime.UtcNow"/>.
/// If expiration check must be skipped, use <see cref="Util.UtcMaxValue"/> as the expiration date.
/// </para>
/// </summary>
/// <param name="data">The token.</param>
/// <param name="checkExpirationDate">Optional check expiration date. Defaults to <see cref="DateTime.UtcNow"/>.</param>
/// <returns>The information (possibly expired) or null if an error occurred.</returns>
public FrontAuthenticationInfo? UnprotectFrontAuthenticationInfo( string data, DateTime? checkExpirationDate = null )
{
Throw.CheckNotNullArgument( data );
var info = _frontTokenFormat.Unprotect( data )!;
if( info == null ) return null;
return info.SetInfo( info.Info.CheckExpiration( checkExpirationDate ?? DateTime.UtcNow ) );
}
/// <summary>
/// Creates a token from a <see cref="FrontAuthenticationInfo"/>.
/// </summary>
/// <param name="info">The authentication info.</param>
/// <returns>The url-safe secured authentication token string.</returns>
public string ProtectFrontAuthenticationInfo( FrontAuthenticationInfo info )
{
Debug.Assert( info.Info != null );
return _frontTokenFormat.Protect( info );
}

/// <summary>
/// Direct generation of an authentication token from any <see cref="IAuthenticationInfo"/>.
/// <see cref="IAuthenticationInfo.CheckExpiration(DateTime)"/> is called with <see cref="DateTime.UtcNow"/>.
/// <para>
/// By default, the expiration is checked based on <see cref="DateTime.UtcNow"/>.
/// If expiration check must be skipped, use <see cref="Util.UtcMaxValue"/> as the expiration date.
/// </para>
/// <para>
/// This is to be used with caution: the authentication token should never be sent to any client and should be
/// used only for secure server to server temporary authentication.
/// </para>
/// </summary>
/// <param name="info">The authentication info for which an authentication token must be obtained.</param>
/// <param name="checkExpirationDate">Optional check expiration date. Defaults to <see cref="DateTime.UtcNow"/>.</param>
/// <returns>The url-safe secured authentication token string.</returns>
public string UnsafeCreateAuthenticationToken( IAuthenticationInfo info, DateTime? checkExpirationDate = null )
{
Throw.CheckNotNullArgument( info );
info = info.CheckExpiration( checkExpirationDate ?? DateTime.UtcNow );
return ProtectFrontAuthenticationInfo( new FrontAuthenticationInfo( info, false ) );
}
/// <summary>
/// Extracts a <see cref="FrontAuthenticationInfo"/> from a token previously created with <see cref="ProtectFrontAuthenticationInfo(FrontAuthenticationInfo)"/>.
/// <para>
/// By default, the expiration is checked based on <see cref="DateTime.UtcNow"/>.
/// If expiration check must be skipped, use <see cref="Util.UtcMaxValue"/> as the expiration date.
/// </para>
/// </summary>
/// <param name="data">The token.</param>
/// <param name="checkExpirationDate">Optional check expiration date. Defaults to <see cref="DateTime.UtcNow"/>.</param>
/// <returns>The information (possibly expired) or null if an error occurred.</returns>
public FrontAuthenticationInfo? UnprotectFrontAuthenticationInfo( string data, DateTime? checkExpirationDate = null )
{
Throw.CheckNotNullArgument( data );
var info = _frontTokenFormat.Unprotect( data )!;
if( info == null ) return null;
return info.SetInfo( info.Info.CheckExpiration( checkExpirationDate ?? DateTime.UtcNow ) );
}

/// <summary>
/// Direct generation of an authentication token for a user.
/// <para>
/// This is to be used with caution: the authentication token should never be sent to any client and should be
/// used only for secure server to server temporary authentication.
/// </para>
/// </summary>
/// <param name="userId">The user identifier.</param>
/// <param name="userName">The user name.</param>
/// <param name="validity">The validity time span: the shorter the better.</param>
/// <returns>The url-safe secured authentication token string.</returns>
public string UnsafeCreateAuthenticationToken( int userId, string userName, TimeSpan validity )
{
var u = _typeSystem.UserInfo.Create( userId, userName );
var info = _typeSystem.AuthenticationInfo.Create( u, DateTime.UtcNow.Add( validity ) );
return ProtectFrontAuthenticationInfo( new FrontAuthenticationInfo( info, false ) );
}
/// <summary>
/// Direct generation of an authentication token from any <see cref="IAuthenticationInfo"/>.
/// <see cref="IAuthenticationInfo.CheckExpiration(DateTime)"/> is called with <see cref="DateTime.UtcNow"/>.
/// <para>
/// By default, the expiration is checked based on <see cref="DateTime.UtcNow"/>.
/// If expiration check must be skipped, use <see cref="Util.UtcMaxValue"/> as the expiration date.
/// </para>
/// <para>
/// This is to be used with caution: the authentication token should never be sent to any client and should be
/// used only for secure server to server temporary authentication.
/// </para>
/// </summary>
/// <param name="info">The authentication info for which an authentication token must be obtained.</param>
/// <param name="checkExpirationDate">Optional check expiration date. Defaults to <see cref="DateTime.UtcNow"/>.</param>
/// <returns>The url-safe secured authentication token string.</returns>
public string UnsafeCreateAuthenticationToken( IAuthenticationInfo info, DateTime? checkExpirationDate = null )
{
Throw.CheckNotNullArgument( info );
info = info.CheckExpiration( checkExpirationDate ?? DateTime.UtcNow );
return ProtectFrontAuthenticationInfo( new FrontAuthenticationInfo( info, false ) );
}

/// <summary>
/// Direct generation of an authentication token for a user.
/// <para>
/// This is to be used with caution: the authentication token should never be sent to any client and should be
/// used only for secure server to server temporary authentication.
/// </para>
/// </summary>
/// <param name="userId">The user identifier.</param>
/// <param name="userName">The user name.</param>
/// <param name="validity">The validity time span: the shorter the better.</param>
/// <returns>The url-safe secured authentication token string.</returns>
public string UnsafeCreateAuthenticationToken( int userId, string userName, TimeSpan validity )
{
var u = _typeSystem.UserInfo.Create( userId, userName );
var info = _typeSystem.AuthenticationInfo.Create( u, DateTime.UtcNow.Add( validity ) );
return ProtectFrontAuthenticationInfo( new FrontAuthenticationInfo( info, false ) );
}

}
45 changes: 22 additions & 23 deletions CK.AspNet.Auth/CKAspNetAuthHttpContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,33 @@
using System.Collections.Generic;
using System.Text;

namespace Microsoft.AspNetCore.Http
namespace Microsoft.AspNetCore.Http;

/// <summary>
/// Exposes <see cref="WebFrontAuthenticate"/> extension method on <see cref="HttpContext"/>.
/// </summary>
static public class CKAspNetAuthHttpContextExtensions
{
/// <summary>
/// Exposes <see cref="WebFrontAuthenticate"/> extension method on <see cref="HttpContext"/>.
/// Obtains the current <see cref="IAuthenticationInfo"/>, either because it is already
/// in <see cref="HttpContext.Items"/> or by extracting authentication from request.
/// It is never null, but can be <see cref="IAuthenticationInfoType.None"/>.
/// </summary>
static public class CKAspNetAuthHttpContextExtensions
/// <param name="this">This context.</param>
/// <returns>Never null, can be <see cref="IAuthenticationInfoType.None"/>.</returns>
static public IAuthenticationInfo GetAuthenticationInfo( this HttpContext @this )
{
/// <summary>
/// Obtains the current <see cref="IAuthenticationInfo"/>, either because it is already
/// in <see cref="HttpContext.Items"/> or by extracting authentication from request.
/// It is never null, but can be <see cref="IAuthenticationInfoType.None"/>.
/// </summary>
/// <param name="this">This context.</param>
/// <returns>Never null, can be <see cref="IAuthenticationInfoType.None"/>.</returns>
static public IAuthenticationInfo GetAuthenticationInfo( this HttpContext @this )
IAuthenticationInfo? authInfo;
if( @this.Items.TryGetValue( typeof( FrontAuthenticationInfo ), out var o ) && o != null )
{
authInfo = ((FrontAuthenticationInfo)o).Info;
}
else
{
IAuthenticationInfo? authInfo;
if( @this.Items.TryGetValue( typeof( FrontAuthenticationInfo ), out var o ) && o != null )
{
authInfo = ((FrontAuthenticationInfo)o).Info;
}
else
{
IActivityMonitor? monitor = null;
var s = @this.RequestServices.GetRequiredService<WebFrontAuthService>();
authInfo = s.ReadAndCacheAuthenticationHeader( @this, ref monitor ).Info;
}
return authInfo;
IActivityMonitor? monitor = null;
var s = @this.RequestServices.GetRequiredService<WebFrontAuthService>();
authInfo = s.ReadAndCacheAuthenticationHeader( @this, ref monitor ).Info;
}
return authInfo;
}
}
Loading

0 comments on commit 0f706c7

Please sign in to comment.