Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
ProjectsStatusManagement
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
hasan.bahjat
ProjectsStatusManagement
Commits
5904697d
Commit
5904697d
authored
Jul 13, 2024
by
hasan khaddour
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Restructure infrastructure layer.
parent
5e798685
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
148 additions
and
3 deletions
+148
-3
DependencyInjection.cs
PSManagement.Infrastructure/DI/DependencyInjection.cs
+10
-1
PSManagement.Infrastructure.csproj
...agement.Infrastructure/PSManagement.Infrastructure.csproj
+9
-2
AppDbContext.cs
PSManagement.Infrastructure/Persistence/AppDbContext.cs
+0
-0
AuthenticationService.cs
...tructure/Services/Authentication/AuthenticationService.cs
+46
-0
DateTimeProvider.cs
PSManagement.Infrastructure/Services/DateTimeProvider.cs
+14
-0
JwtSetting.cs
PSManagement.Infrastructure/Tokens/JwtSetting.cs
+18
-0
JwtTokenGenerator.cs
PSManagement.Infrastructure/Tokens/JwtTokenGenerator.cs
+51
-0
No files found.
PSManagement.Infrastructure/DI/DependencyInjection.cs
View file @
5904697d
using
Microsoft.Extensions.Configuration
;
using
Microsoft.Extensions.Configuration
;
using
Microsoft.Extensions.DependencyInjection
;
using
Microsoft.Extensions.DependencyInjection
;
using
PSManagement.Application.Common.Services
;
using
PSManagement.Application.Contracts.Authentication
;
using
PSManagement.Infrastructure.Authentication
;
using
PSManagement.Infrastructure.Services
;
using
PSManagement.Infrastructure.Services.Authentication
;
namespace
PSManagement.Infrastructure.DI
namespace
PSManagement.Infrastructure.DI
{
{
public
static
class
DependencyInjection
public
static
class
DependencyInjection
{
{
public
static
IServiceCollection
AddInfrastructure
(
this
IServiceCollection
services
)
public
static
IServiceCollection
AddInfrastructure
(
this
IServiceCollection
services
,
IConfiguration
configuration
)
{
{
services
.
Configure
<
JwtSetting
>(
configuration
.
GetSection
(
JwtSetting
.
Section
));
services
.
AddSingleton
<
IJwtTokenGenerator
,
JwtTokenGenerator
>();
services
.
AddSingleton
<
IDateTimeProvider
,
DateTimeProvider
>();
services
.
AddScoped
<
IAuthenticationService
,
AuthenticationService
>();
return
services
;
return
services
;
}
}
...
...
PSManagement.Infrastructure/PSManagement.Infrastructure.csproj
View file @
5904697d
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
...
@@ -6,12 +6,19 @@
...
@@ -6,12 +6,19 @@
<ItemGroup>
<ItemGroup>
<Folder Include="Migrations\" />
<Folder Include="Migrations\" />
<Folder Include="EntitiesConfiguration\" />
<Folder Include="
Persistence\
EntitiesConfiguration\" />
</ItemGroup>
</ItemGroup>
<ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.2" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.2" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="5.0.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.7.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PSManagement.Application\PSManagement.Application.csproj" />
<ProjectReference Include="..\PSManagement.Contracts\PSManagement.Contracts.csproj" />
</ItemGroup>
</ItemGroup>
</Project>
</Project>
PSManagement.Infrastructure/
Common/
Persistence/AppDbContext.cs
→
PSManagement.Infrastructure/Persistence/AppDbContext.cs
View file @
5904697d
File moved
PSManagement.Infrastructure/Services/Authentication/AuthenticationService.cs
0 → 100644
View file @
5904697d
using
PSManagement.Application.Contracts.Authentication
;
using
System
;
using
System.Threading.Tasks
;
namespace
PSManagement.Infrastructure.Services.Authentication
{
public
class
AuthenticationService
:
IAuthenticationService
{
private
readonly
IJwtTokenGenerator
_jwtTokenGenerator
;
public
AuthenticationService
(
IJwtTokenGenerator
jwtTokenGenerator
)
{
_jwtTokenGenerator
=
jwtTokenGenerator
;
}
public
async
Task
<
AuthenticationResult
>
Login
(
String
email
,
String
password
)
{
return
new
AuthenticationResult
{
Id
=
Guid
.
NewGuid
(),
Email
=
email
,
FirstName
=
"First name "
,
LastName
=
"last Name "
,
Token
=
"token"
};
}
public
async
Task
<
AuthenticationResult
>
Register
(
String
email
,
String
firstName
,
String
lastName
,
String
password
)
{
// check if the user exist
Guid
userId
=
Guid
.
NewGuid
();
// generate token
String
token
=
_jwtTokenGenerator
.
GenerateToken
(
userId
,
firstName
,
lastName
,
email
);
return
new
AuthenticationResult
{
Id
=
Guid
.
NewGuid
(),
Email
=
email
,
FirstName
=
firstName
,
LastName
=
lastName
,
Token
=
token
};
}
}
}
PSManagement.Infrastructure/Services/DateTimeProvider.cs
0 → 100644
View file @
5904697d
using
PSManagement.Application.Common.Services
;
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Text
;
using
System.Threading.Tasks
;
namespace
PSManagement.Infrastructure.Services
{
public
class
DateTimeProvider
:
IDateTimeProvider
{
public
DateTime
UtcNow
=>
DateTime
.
UtcNow
;
}
}
PSManagement.Infrastructure/Tokens/JwtSetting.cs
0 → 100644
View file @
5904697d
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Text
;
using
System.Threading.Tasks
;
namespace
PSManagement.Infrastructure.Authentication
{
public
class
JwtSetting
{
public
const
string
Section
=
"JwtSettings"
;
public
string
Audience
{
get
;
set
;
}
=
null
!;
public
string
Issuer
{
get
;
set
;
}
=
null
!;
public
string
Secret
{
get
;
set
;
}
=
null
!;
public
int
ExpireMinutes
{
get
;
set
;
}
}
}
PSManagement.Infrastructure/Tokens/JwtTokenGenerator.cs
0 → 100644
View file @
5904697d
using
Microsoft.Extensions.Options
;
using
Microsoft.IdentityModel.Tokens
;
using
PSManagement.Application.Common.Services
;
using
PSManagement.Application.Contracts.Authentication
;
using
System
;
using
System.Collections.Generic
;
using
System.IdentityModel.Tokens.Jwt
;
using
System.Linq
;
using
System.Security.Claims
;
using
System.Text
;
using
System.Threading.Tasks
;
namespace
PSManagement.Infrastructure.Authentication
{
public
class
JwtTokenGenerator
:
IJwtTokenGenerator
{
private
readonly
IDateTimeProvider
_dateTimeProvider
;
private
readonly
JwtSetting
_jwtSetting
;
public
JwtTokenGenerator
(
IDateTimeProvider
dateTimeProvider
,
IOptions
<
JwtSetting
>
jwtOptions
)
{
_dateTimeProvider
=
dateTimeProvider
;
_jwtSetting
=
jwtOptions
.
Value
;
}
public
string
GenerateToken
(
Guid
id
,
string
firstName
,
string
lastName
,
string
email
)
{
var
signingCredentials
=
new
SigningCredentials
(
new
SymmetricSecurityKey
(
Encoding
.
UTF8
.
GetBytes
(
_jwtSetting
.
Secret
)),
SecurityAlgorithms
.
HmacSha256
);
var
claims
=
new
[]
{
new
Claim
(
JwtRegisteredClaimNames
.
Sub
,
id
.
ToString
()),
new
Claim
(
JwtRegisteredClaimNames
.
Jti
,
Guid
.
NewGuid
().
ToString
()),
new
Claim
(
JwtRegisteredClaimNames
.
Email
,
email
),
new
Claim
(
JwtRegisteredClaimNames
.
GivenName
,
firstName
),
new
Claim
(
JwtRegisteredClaimNames
.
FamilyName
,
lastName
)
};
var
securityToken
=
new
JwtSecurityToken
(
issuer
:
_jwtSetting
.
Issuer
,
audience
:
_jwtSetting
.
Audience
,
expires
:
_dateTimeProvider
.
UtcNow
.
AddMinutes
(
_jwtSetting
.
ExpireMinutes
),
claims
:
claims
,
signingCredentials
:
signingCredentials
);
return
new
JwtSecurityTokenHandler
().
WriteToken
(
securityToken
);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment