Skip to content

Commit 26933ef

Browse files
authored
Merge pull request #70 from PasinduPrabhashitha/async-messaging
Async messaging
2 parents ae271f3 + 474bfd8 commit 26933ef

File tree

16 files changed

+353
-121
lines changed

16 files changed

+353
-121
lines changed

src/services/Florage.Authentication/ConfigurationsRegistration/JwtConfiguration.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,9 @@ public static class JwtConfiguration
88
{
99
public static IServiceCollection JwtConfigurationRegistration(this IServiceCollection services, IConfiguration configuration)
1010
{
11-
services.AddAuthentication(options =>
12-
{
13-
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
14-
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
15-
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
16-
})
17-
18-
// Adding Jwt Bearer
11+
services.AddAuthentication(defaultScheme: JwtBearerDefaults.AuthenticationScheme)
1912
.AddJwtBearer(options =>
2013
{
21-
options.SaveToken = true;
22-
options.RequireHttpsMetadata = false;
2314
options.TokenValidationParameters = new TokenValidationParameters()
2415
{
2516
ValidateIssuer = true,

src/services/Florage.Authentication/ConfigurationsRegistration/RegisterIdentity.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
using AspNetCore.Identity.Mongo;
2-
using AspNetCore.Identity.Mongo.Model;
1+
using Florage.Authentication.Models;
2+
using Microsoft.AspNetCore.Identity;
33

44
namespace Florage.Authentication.ConfigurationsRegistration
55
{
66
public static class RegisterIdentity
77
{
88
public static IServiceCollection ConfigureIdentityServices(this IServiceCollection services, IConfiguration configuration)
9-
{
10-
services.AddIdentityMongoDbProvider<MongoUser, MongoRole>(identity =>
11-
{
12-
identity.Password.RequiredLength = 8;
13-
},
14-
mongo =>
15-
{
16-
mongo.ConnectionString = configuration.GetConnectionString("DefaultConnection");
17-
});
9+
{
10+
services.AddIdentity<ApplicationUser, ApplicationRole>()
11+
.AddMongoDbStores<ApplicationUser, ApplicationRole, Guid>
12+
(
13+
configuration.GetConnectionString("DefaultConnection"),
14+
"Auth"
15+
)
16+
.AddDefaultTokenProviders();
1817

1918
return services;
2019
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Florage.Authentication.Dtos;
2+
using Florage.Authentication.Responses;
3+
using Microsoft.AspNetCore.Identity;
4+
5+
namespace Florage.Authentication.Contracts
6+
{
7+
public interface IAuthService
8+
{
9+
public Task<IdentityResult> RegisterAsync(UserRegisterDto userRegisterDto);
10+
public Task<UserTokenResponse> LoginAsync(UserLoginDto userLoginDto);
11+
public bool ValidateToken(string token);
12+
}
13+
}
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
using Florage.Authentication.Dtos;
2-
using Florage.Authentication.Responses;
3-
using Microsoft.AspNetCore.Identity;
1+
using Microsoft.AspNetCore.Identity;
42

53
namespace Florage.Authentication.Contracts
64
{
75
public interface IUserService
86
{
9-
public Task<IdentityResult> RegisterAsync(UserRegisterDto userRegisterDto);
10-
public Task<UserTokenResponse> LoginAsync(UserLoginDto userLoginDto);
11-
public bool ValidateToken(string token);
7+
Task<IdentityResult> AddAdminRole();
8+
Task<IdentityResult> AddUserToAdminRole(string userId);
129
}
1310
}

src/services/Florage.Authentication/Controllers/AuthController.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@
77
namespace Florage.Authentication.Controllers
88
{
99
[Route("api/[controller]")]
10-
[ApiController]
10+
[ApiController]
1111
public class AuthController : ControllerBase
1212
{
1313

14-
private readonly IUserService _userService;
1514

16-
public AuthController(IUserService userService)
15+
//[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Roles = ("admin"))]
16+
17+
private readonly IAuthService _userService;
18+
19+
public AuthController(IAuthService userService)
1720
{
1821
_userService = userService;
19-
}
22+
}
2023

2124
[HttpPost]
2225
[Route("register")]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using Florage.Authentication.Contracts;
2+
using Microsoft.AspNetCore.Authorization;
3+
using Microsoft.AspNetCore.Identity;
4+
using Microsoft.AspNetCore.Mvc;
5+
6+
namespace Florage.Authentication.Controllers
7+
{
8+
[Route("api/users")]
9+
[ApiController]
10+
public class UserController : ControllerBase
11+
{
12+
private readonly IUserService _userService;
13+
14+
public UserController(IUserService userService)
15+
{
16+
_userService = userService;
17+
}
18+
19+
[HttpPost]
20+
[Route("add-admin")]
21+
public async Task<ActionResult<IdentityResult>> AddRole()
22+
{
23+
IdentityResult identityResult = await _userService.AddAdminRole();
24+
25+
if(identityResult != null && identityResult.Succeeded)
26+
{
27+
return Ok(identityResult);
28+
}
29+
30+
return BadRequest(identityResult);
31+
}
32+
33+
[Authorize]
34+
[HttpPost]
35+
[Route("assign-admin")]
36+
public async Task<ActionResult<IdentityResult>> AssignAdminRoleToUser(string userId)
37+
{
38+
IdentityResult identityResult = await _userService.AddUserToAdminRole(userId);
39+
40+
if (identityResult.Succeeded)
41+
{
42+
return Ok(identityResult);
43+
}
44+
45+
return BadRequest(identityResult);
46+
}
47+
}
48+
}

src/services/Florage.Authentication/Florage.Authentication.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="AspNetCore.Identity.Mongo" Version="8.3.3" />
13+
<PackageReference Include="AspNetCore.Identity.MongoDbCore" Version="3.1.2" />
1414
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.15" />
1515
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />
1616
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.15.1" />
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using AspNetCore.Identity.MongoDbCore.Models;
2+
3+
namespace Florage.Authentication.Models
4+
{
5+
public class ApplicationRole : MongoIdentityRole<Guid>
6+
{
7+
public ApplicationRole() : base()
8+
{
9+
}
10+
11+
public ApplicationRole(string roleName) : base(roleName)
12+
{
13+
}
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using AspNetCore.Identity.MongoDbCore.Models;
2+
3+
namespace Florage.Authentication.Models
4+
{
5+
public class ApplicationUser: MongoIdentityUser<Guid>
6+
{
7+
public ApplicationUser(): base()
8+
{
9+
}
10+
11+
public ApplicationUser(string userName, string email) : base(userName, email)
12+
{
13+
}
14+
}
15+
}

src/services/Florage.Authentication/Program.cs

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,54 @@
11
using Florage.Authentication.Configurations;
22
using Florage.Authentication.ConfigurationsRegistration;
33
using Florage.Authentication.Contracts;
4-
using Florage.Authentication.Services;
4+
using Florage.Authentication.Services;
5+
using Microsoft.OpenApi.Models;
56

67
var builder = WebApplication.CreateBuilder(args);
78

89
builder.Services.AddControllers();
910
builder.Services.AddEndpointsApiExplorer();
10-
builder.Services.AddSwaggerGen();
11-
builder.Services.AddScoped<IUserService, UserService>();
11+
builder.Services.AddSwaggerGen();
12+
builder.Services.AddScoped<IAuthService, AuthService>();
13+
builder.Services.AddTransient<IUserService, UserService>();
14+
15+
builder.Services.AddSwaggerGen(option =>
16+
{
17+
option.SwaggerDoc("v1", new OpenApiInfo { Title = "Florage Auth API", Version = "v1" });
18+
option.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
19+
{
20+
Name = "Authorization",
21+
Type = SecuritySchemeType.ApiKey,
22+
Scheme = "Bearer",
23+
BearerFormat = "JWT",
24+
In = ParameterLocation.Header,
25+
Description = "JWT Authorization header using the Bearer scheme. \r\n\r\n Enter 'Bearer' [space] and then your token in the text input below.\r\n\r\nExample: \"Bearer 1safsfsdfdfd\"",
26+
});
27+
option.AddSecurityRequirement(new OpenApiSecurityRequirement
28+
{
29+
{
30+
new OpenApiSecurityScheme
31+
{
32+
Reference = new OpenApiReference
33+
{
34+
Type=ReferenceType.SecurityScheme,
35+
Id="Bearer"
36+
}
37+
},
38+
new string[]{}
39+
}
40+
});
41+
});
1242

1343
JwtConfiguration.JwtConfigurationRegistration(builder.Services, builder.Configuration);
1444
RegisterIdentity.ConfigureIdentityServices(builder.Services, builder.Configuration);
1545

1646
var app = builder.Build();
1747

48+
app.UseAuthentication();
49+
app.UseAuthorization();
50+
51+
1852
if (app.Environment.IsDevelopment())
1953
{
2054
app.UseSwagger();
@@ -23,7 +57,6 @@
2357

2458
app.UseHttpsRedirection();
2559

26-
app.UseAuthorization();
2760

2861
app.MapControllers();
2962

0 commit comments

Comments
 (0)