Skip to content

Commit a6b825f

Browse files
committed
Authenticate with Firebase
1 parent fcb3ed1 commit a6b825f

10 files changed

Lines changed: 31 additions & 203 deletions

File tree

RequestTests.paw

-1.8 KB
Binary file not shown.

src/SimpleToDoService/Common/Common.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
22
namespace SimpleToDoService
33
{
4-
class EmailExistedException : Exception { }
5-
64
public class ServiceError
75
{
86
public string Message { get; set; }

src/SimpleToDoService/Controllers/RootController.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using Microsoft.AspNetCore.Mvc;
1+
using Microsoft.AspNetCore.Mvc;
32

43
namespace SimpleToDoService
54
{

src/SimpleToDoService/Controllers/TasksController.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
namespace SimpleToDoService
1212
{
1313
[Authorize]
14-
//[MiddlewareFilter(typeof(BasicAuthMiddleware))]
1514
[MiddlewareFilter(typeof(CheckUserMiddleware))]
1615
[Route("api/v1/[controller]")]
1716
public class TasksController : Controller
@@ -36,11 +35,6 @@ public TasksController(IToDoRepository repository)
3635
[HttpGet("{uuid:Guid?}", Name = "GetTask")]
3736
public IEnumerable<Task> Get(Guid? uuid)
3837
{
39-
// Get the user's ID
40-
var userId = HttpContext.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value;
41-
//var userIdGuid = Guid.ParseExact(userId, "N");
42-
//string email = HttpContext.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email).Value;
43-
var id = CurrentUserUuid;
4438
var entries = repository.Tasks(CurrentUserUuid).OrderBy(o => o.CreationDate).Where(o => !o.Completed);
4539

4640
if (uuid != null)

src/SimpleToDoService/Controllers/UserController.cs

Lines changed: 0 additions & 91 deletions
This file was deleted.

src/SimpleToDoService/Database/Repositiory.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,12 @@ public bool DeleteTask(Guid uuid)
9191

9292
public User CreateUser(User user)
9393
{
94-
if (Users().Where(o => o.Email == user.Email).Count() > 0)
95-
throw new EmailExistedException();
96-
9794
user.Uuid = new Guid();
9895
user.CreationDate = DateTime.Now.ToUniversalTime();
9996
var entity = context.Users.Add(user);
10097

101-
if (context.SaveChanges() == 1)
102-
return entity.Entity;
103-
104-
return null;
98+
context.SaveChanges();
99+
return entity.Entity;
105100
}
106101

107102
public bool DeleteUser(User user)

src/SimpleToDoService/Middleware/BasicAuthMiddleware.cs

Lines changed: 0 additions & 71 deletions
This file was deleted.

src/SimpleToDoService/Middleware/CheckUserMiddleware.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using SimpleToDoService.Context;
1010
using Microsoft.AspNetCore.Builder;
1111
using System.Security.Claims;
12+
using SimpleToDoService.Entities;
1213

1314
namespace SimpleToDoService.Middleware
1415
{
@@ -30,14 +31,35 @@ public void Configure(IApplicationBuilder applicationBuilder)
3031
applicationBuilder.UseMiddleware<CheckUserMiddleware>();
3132
}
3233

33-
public async Task Invoke(HttpContext context)
34+
public async System.Threading.Tasks.Task Invoke(HttpContext context)
3435
{
35-
var test = context.Items["UserUuid"];
36-
var userId = context.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier)?.Value;
37-
context.Items.Add("UserUuid", userId);
38-
36+
if (context.User.Identity.IsAuthenticated && !context.Items.Keys.Contains("UserUuid"))
37+
{
38+
context.Items.Add("UserUuid", LoadUserGuid(context));
39+
}
3940
await next.Invoke(context);
4041
}
42+
43+
private Guid LoadUserGuid(HttpContext context)
44+
{
45+
var firebaseId = context.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value;
46+
var currentUser = repository.Users().Where(o => o.FirebaseId == firebaseId).FirstOrDefault();
47+
48+
if (currentUser != null)
49+
{
50+
return currentUser.Uuid;
51+
}
52+
53+
var user = new User()
54+
{
55+
FirebaseId = firebaseId,
56+
Email = context.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email).Value,
57+
FirstName = "",
58+
LastName = ""
59+
};
60+
61+
return repository.CreateUser(user).Uuid;
62+
}
4163
}
4264

4365
}

src/SimpleToDoService/SimpleToDoService.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@
7575
<None Include="AppSettings.json">
7676
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
7777
</None>
78-
<None Include="DbScripts\CreateTables.sql" />
79-
<None Include="DbScripts\CreateUser.sql" />
78+
<None Remove="Controllers\UserController.cs" />
8079
</ItemGroup>
8180
<ItemGroup>
8281
<Folder Include="Controllers\" />

src/SimpleToDoService/Startup.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
2-
using Microsoft.AspNetCore.Authentication.JwtBearer;
32
using Microsoft.AspNetCore.Builder;
43
using Microsoft.AspNetCore.Hosting;
5-
using Microsoft.AspNetCore.Mvc.Formatters;
64
using Microsoft.EntityFrameworkCore;
75
using Microsoft.Extensions.Configuration;
86
using Microsoft.Extensions.DependencyInjection;
@@ -11,9 +9,6 @@
119
using SimpleToDoService.Context;
1210
using SimpleToDoService.Middleware;
1311
using SimpleToDoService.Repository;
14-
using System.Linq;
15-
using System.Security.Claims;
16-
using System.Threading.Tasks;
1712

1813
namespace SimpleToDoService
1914
{
@@ -61,20 +56,8 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
6156
app.UseDeveloperExceptionPage();
6257
}
6358

64-
//app.UseMiddleware<BasicAuthMiddleware>();
65-
6659
app.UseJwtBearerAuthentication(new JwtBearerOptions
6760
{
68-
//Events = new JwtBearerEvents()
69-
//{
70-
// OnTokenValidated = (arg) =>
71-
// {
72-
73-
// var userId = arg.HttpContext.User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.NameIdentifier).Value;
74-
// arg.HttpContext.Items.Add("UserUuid", userId);
75-
// return Task.FromResult(0);
76-
// }
77-
//},
7861
AutomaticAuthenticate = true,
7962
Authority = "https://securetoken.google.com/simpletaskmanager-9c565",
8063
TokenValidationParameters = new TokenValidationParameters

0 commit comments

Comments
 (0)