Skip to content

Commit

Permalink
Merge branch 'release/1.0.0-beta1'
Browse files Browse the repository at this point in the history
  • Loading branch information
albx committed Aug 21, 2017
2 parents df4f23f + b789070 commit bd8edde
Show file tree
Hide file tree
Showing 30 changed files with 1,185 additions and 6 deletions.
21 changes: 21 additions & 0 deletions README.md
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.
116 changes: 116 additions & 0 deletions Wilcommerce.Auth.Test/Models/UserTokenTest.cs
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"));
}
}
}
19 changes: 19 additions & 0 deletions Wilcommerce.Auth.Test/Properties/AssemblyInfo.cs
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")]
72 changes: 72 additions & 0 deletions Wilcommerce.Auth.Test/Services/IdentityFactoryTest.cs
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);
}
}
}
22 changes: 22 additions & 0 deletions Wilcommerce.Auth.Test/Wilcommerce.Auth.Test.xproj
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>
26 changes: 26 additions & 0 deletions Wilcommerce.Auth.Test/project.json
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"
]
}
}
}
9 changes: 9 additions & 0 deletions Wilcommerce.Auth.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Wilcommerce.Auth", "src\Wilcommerce.Auth\Wilcommerce.Auth.xproj", "{44A956D2-E32F-49D9-A4D1-3380C0F6DCF7}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{D9B968BF-5C00-485C-90F8-105B2B70C349}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Wilcommerce.Auth.Test", "Wilcommerce.Auth.Test\Wilcommerce.Auth.Test.xproj", "{0A3668D7-C428-442A-9254-3DD5683ECB31}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -23,11 +27,16 @@ Global
{44A956D2-E32F-49D9-A4D1-3380C0F6DCF7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{44A956D2-E32F-49D9-A4D1-3380C0F6DCF7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{44A956D2-E32F-49D9-A4D1-3380C0F6DCF7}.Release|Any CPU.Build.0 = Release|Any CPU
{0A3668D7-C428-442A-9254-3DD5683ECB31}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0A3668D7-C428-442A-9254-3DD5683ECB31}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0A3668D7-C428-442A-9254-3DD5683ECB31}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0A3668D7-C428-442A-9254-3DD5683ECB31}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{44A956D2-E32F-49D9-A4D1-3380C0F6DCF7} = {D3477DCE-8BF1-426D-988D-E635FA87D1E6}
{0A3668D7-C428-442A-9254-3DD5683ECB31} = {D9B968BF-5C00-485C-90F8-105B2B70C349}
EndGlobalSection
EndGlobal
35 changes: 35 additions & 0 deletions src/Wilcommerce.Auth/AuthenticationDefaults.cs
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;
}
}
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>
{
}
}
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>
{
}
}
Loading

0 comments on commit bd8edde

Please sign in to comment.