-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
1,185 additions
and
6 deletions.
There are no files selected for viewing
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,2 +1,23 @@ | ||
# Wilcommerce.Auth | ||
Wilcommerce Authentication and Authorization package | ||
|
||
## Installation | ||
Nuget package available here [https://www.nuget.org/packages/Wilcommerce.Auth](https://www.nuget.org/packages/Wilcommerce.Auth) | ||
|
||
## Models | ||
The **Models** namespace contains all the classes representing the components used for creating authentication tokens. | ||
|
||
## Read models | ||
This namespace contains the interface which gives a readonly access to the components. | ||
|
||
## Services | ||
The **Services** namespace contains a set of components which gives a simple access to the features of this package. | ||
|
||
## Commands | ||
**Commands** namespace contains all the actions available on this package. | ||
|
||
## Repository | ||
This namespace contains the interface which defines the persistence of the components. | ||
|
||
## Events | ||
In the **Events** namespace are defined all the events that could happen after an action made. |
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,116 @@ | ||
using System; | ||
using Wilcommerce.Auth.Models; | ||
using Wilcommerce.Core.Common.Domain.Models; | ||
using Xunit; | ||
|
||
namespace Wilcommerce.Auth.Test.Models | ||
{ | ||
public class UserTokenTest | ||
{ | ||
[Fact] | ||
public void PasswordRecovery_Should_Throw_ArgumentNullException_If_User_IsNull() | ||
{ | ||
var ex = Assert.Throws<ArgumentNullException>(() => UserToken.PasswordRecovery(null, "", DateTime.Now)); | ||
Assert.Equal("user", ex.ParamName); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null)] | ||
[InlineData("")] | ||
public void PasswordRecovery_Should_Throw_ArgumentNullException_If_Token_IsEmpty(string value) | ||
{ | ||
var user = User.CreateAsAdministrator("Admin", "[email protected]", "password"); | ||
|
||
var ex = Assert.Throws<ArgumentNullException>(() => UserToken.PasswordRecovery(user, value, DateTime.Now)); | ||
Assert.Equal("token", ex.ParamName); | ||
} | ||
|
||
[Fact] | ||
public void PasswordRecovery_Should_Throw_ArgumentException_If_ExpirationDate_IsPreviousThan_Now() | ||
{ | ||
var user = User.CreateAsAdministrator("Admin", "[email protected]", "password"); | ||
string token = "token"; | ||
|
||
var ex = Assert.Throws<ArgumentException>(() => UserToken.PasswordRecovery(user, token, DateTime.Now.AddDays(-1))); | ||
Assert.Equal("expirationDate", ex.ParamName); | ||
} | ||
|
||
[Fact] | ||
public void PasswordRecovery_Should_Create_A_PasswordRecovery_Token() | ||
{ | ||
var user = User.CreateAsAdministrator("Admin", "[email protected]", "password"); | ||
string token = "token"; | ||
var expirationDate = DateTime.Now.AddDays(10); | ||
|
||
var userToken = UserToken.PasswordRecovery(user, token, expirationDate); | ||
Assert.Equal(TokenTypes.PasswordRecovery, userToken.TokenType); | ||
} | ||
|
||
[Fact] | ||
public void Registration_Should_Throw_ArgumentNullException_If_User_IsNull() | ||
{ | ||
var ex = Assert.Throws<ArgumentNullException>(() => UserToken.Registration(null, "", DateTime.Now)); | ||
Assert.Equal("user", ex.ParamName); | ||
} | ||
|
||
[Theory] | ||
[InlineData(null)] | ||
[InlineData("")] | ||
public void Registration_Should_Throw_ArgumentNullException_If_Token_IsEmpty(string value) | ||
{ | ||
var user = User.CreateAsAdministrator("Admin", "[email protected]", "password"); | ||
|
||
var ex = Assert.Throws<ArgumentNullException>(() => UserToken.Registration(user, value, DateTime.Now)); | ||
Assert.Equal("token", ex.ParamName); | ||
} | ||
|
||
[Fact] | ||
public void Registration_Should_Throw_ArgumentException_If_ExpirationDate_IsPreviousThan_Now() | ||
{ | ||
var user = User.CreateAsAdministrator("Admin", "[email protected]", "password"); | ||
string token = "token"; | ||
|
||
var ex = Assert.Throws<ArgumentException>(() => UserToken.Registration(user, token, DateTime.Now.AddDays(-1))); | ||
Assert.Equal("expirationDate", ex.ParamName); | ||
} | ||
|
||
[Fact] | ||
public void Registration_Should_Create_A_Registration_Token() | ||
{ | ||
var user = User.CreateAsAdministrator("Admin", "[email protected]", "password"); | ||
string token = "token"; | ||
var expirationDate = DateTime.Now.AddDays(10); | ||
|
||
var userToken = UserToken.Registration(user, token, expirationDate); | ||
Assert.Equal(TokenTypes.Registration, userToken.TokenType); | ||
} | ||
|
||
[Fact] | ||
public void SetAsExpired_Should_Throw_InvalidOperationException_If_Token_Is_Already_Expired() | ||
{ | ||
var user = User.CreateAsAdministrator("Admin", "[email protected]", "password"); | ||
string token = "token"; | ||
var expirationDate = DateTime.Now.AddDays(10); | ||
|
||
var userToken = UserToken.Registration(user, token, expirationDate); | ||
userToken.SetAsExpired(); | ||
|
||
var ex = Assert.Throws<InvalidOperationException>(() => userToken.SetAsExpired()); | ||
Assert.Equal($"Token already expired on {userToken.ExpirationDate.ToString()}", ex.Message); | ||
} | ||
|
||
[Fact] | ||
public void SetAsExpired_Should_Set_ExpirationDate_To_Today() | ||
{ | ||
var user = User.CreateAsAdministrator("Admin", "[email protected]", "password"); | ||
string token = "token"; | ||
var expirationDate = DateTime.Now.AddDays(10); | ||
|
||
var userToken = UserToken.Registration(user, token, expirationDate); | ||
userToken.SetAsExpired(); | ||
|
||
Assert.Equal(true, userToken.IsExpired); | ||
Assert.Equal(DateTime.Now.ToString("yyyy-MM-dd"), userToken.ExpirationDate.ToString("yyyy-MM-dd")); | ||
} | ||
} | ||
} |
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,19 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("Wilcommerce.Auth.Test")] | ||
[assembly: AssemblyTrademark("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// The following GUID is for the ID of the typelib if this project is exposed to COM | ||
[assembly: Guid("0a3668d7-c428-442a-9254-3dd5683ecb31")] |
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,72 @@ | ||
using System.Security.Claims; | ||
using Wilcommerce.Auth.Services; | ||
using Wilcommerce.Auth.Services.Interfaces; | ||
using Wilcommerce.Core.Common.Domain.Models; | ||
using Xunit; | ||
|
||
namespace Wilcommerce.Auth.Test.Services | ||
{ | ||
public class IdentityFactoryTest | ||
{ | ||
private IIdentityFactory _factory; | ||
|
||
public IdentityFactoryTest() | ||
{ | ||
_factory = new IdentityFactory(); | ||
} | ||
|
||
[Fact] | ||
public void CreateIdentity_Identity_Name_Must_Match_User_Email() | ||
{ | ||
var user = User.CreateAsAdministrator("User", "[email protected]", "1234"); | ||
var principal = _factory.CreateIdentity(user); | ||
|
||
Assert.Equal(user.Email, principal.Identity.Name); | ||
} | ||
|
||
[Fact] | ||
public void AdministratorUser_Must_Have_Administrator_As_Role() | ||
{ | ||
var user = User.CreateAsAdministrator("User", "[email protected]", "1234"); | ||
var principal = _factory.CreateIdentity(user); | ||
|
||
Assert.True(principal.IsInRole(AuthenticationDefaults.AdministratorRole)); | ||
} | ||
|
||
[Fact] | ||
public void CustomerUser_Must_Have_Customer_As_Role() | ||
{ | ||
var user = User.CreateAsCustomer("Customer", "[email protected]", "1234"); | ||
var principal = _factory.CreateIdentity(user); | ||
|
||
Assert.True(principal.IsInRole(AuthenticationDefaults.CustomerRole)); | ||
} | ||
|
||
[Fact] | ||
public void CreateIdentity_NameIdentifier_Must_Match_User_Id() | ||
{ | ||
var user = User.CreateAsAdministrator("User", "[email protected]", "1234"); | ||
var principal = _factory.CreateIdentity(user); | ||
|
||
Assert.Equal(principal.FindFirstValue(ClaimTypes.NameIdentifier), user.Id.ToString()); | ||
} | ||
|
||
[Fact] | ||
public void CreateIdentity_Email_Must_Match_User_Email() | ||
{ | ||
var user = User.CreateAsAdministrator("User", "[email protected]", "1234"); | ||
var principal = _factory.CreateIdentity(user); | ||
|
||
Assert.Equal(principal.FindFirstValue(ClaimTypes.Email), user.Email); | ||
} | ||
|
||
[Fact] | ||
public void CreateIdentity_GivenName_Must_Match_User_Name() | ||
{ | ||
var user = User.CreateAsAdministrator("User", "[email protected]", "1234"); | ||
var principal = _factory.CreateIdentity(user); | ||
|
||
Assert.Equal(principal.FindFirstValue(ClaimTypes.GivenName), user.Name); | ||
} | ||
} | ||
} |
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,22 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion> | ||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> | ||
</PropertyGroup> | ||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" /> | ||
<PropertyGroup Label="Globals"> | ||
<ProjectGuid>0a3668d7-c428-442a-9254-3dd5683ecb31</ProjectGuid> | ||
<RootNamespace>Wilcommerce.Auth.Test</RootNamespace> | ||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath> | ||
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath> | ||
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" /> | ||
</ItemGroup> | ||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" /> | ||
</Project> |
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,26 @@ | ||
{ | ||
"version": "1.0.0-*", | ||
|
||
"testRunner": "xunit", | ||
"dependencies": { | ||
"NETStandard.Library": "1.6.1", | ||
"Wilcommerce.Auth": "1.0.0-beta1", | ||
"xunit": "2.1.0", | ||
"dotnet-test-xunit": "2.2.0-preview2-build1029" | ||
}, | ||
|
||
"frameworks": { | ||
"netcoreapp1.1": { | ||
"dependencies": { | ||
"Microsoft.NETCore.App": { | ||
"type": "platform", | ||
"version": "1.1.0" | ||
} | ||
}, | ||
"imports": [ | ||
"dnxcore50", | ||
"portable-net45+win8" | ||
] | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using Microsoft.AspNetCore.Authentication.Cookies; | ||
|
||
namespace Wilcommerce.Auth | ||
{ | ||
/// <summary> | ||
/// Defines the authentication's options default values | ||
/// </summary> | ||
public static class AuthenticationDefaults | ||
{ | ||
/// <summary> | ||
/// Get the Authentication scheme used | ||
/// </summary> | ||
public static string AuthenticationScheme => CookieAuthenticationDefaults.AuthenticationScheme; | ||
|
||
/// <summary> | ||
/// Get the string representing the customer role | ||
/// </summary> | ||
public static string CustomerRole => "Customer"; | ||
|
||
/// <summary> | ||
/// Get the string representing the administrator role | ||
/// </summary> | ||
public static string AdministratorRole => "Administrator"; | ||
|
||
/// <summary> | ||
/// Get the cookie prefix | ||
/// </summary> | ||
public static string CookiePrefix => ".Wilcommerce."; | ||
|
||
/// <summary> | ||
/// Get the number of days after which expires the generated tokens | ||
/// </summary> | ||
public static int ExpirationDays => 1; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/Wilcommerce.Auth/Commands/Handlers/Interfaces/IRecoverPasswordCommandHandler.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,8 @@ | ||
using Wilcommerce.Core.Infrastructure; | ||
|
||
namespace Wilcommerce.Auth.Commands.Handlers.Interfaces | ||
{ | ||
public interface IRecoverPasswordCommandHandler : ICommandHandlerAsync<RecoverPasswordCommand> | ||
{ | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/Wilcommerce.Auth/Commands/Handlers/Interfaces/IValidatePasswordRecoveryCommandHandler.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,8 @@ | ||
using Wilcommerce.Core.Infrastructure; | ||
|
||
namespace Wilcommerce.Auth.Commands.Handlers.Interfaces | ||
{ | ||
public interface IValidatePasswordRecoveryCommandHandler : ICommandHandlerAsync<ValidatePasswordRecoveryCommand> | ||
{ | ||
} | ||
} |
Oops, something went wrong.