-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthService.cs
32 lines (27 loc) · 1 KB
/
AuthService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System.Diagnostics;
using FirebaseAdmin.Auth;
namespace Menza.Server;
public class AuthService
{
private readonly HttpContext _context;
public AuthService(IHttpContextAccessor accessor) => _context = accessor.HttpContext!;
public async Task<string?> Authenticate()
{
if (!_context.Request.Headers.Authorization.Any()) return null;
string header = _context.Request.Headers.Authorization!;
if (!header.StartsWith("Bearer ")) return null;
string idToken = header.Split(' ')[1];
try
{
if (FirebaseAuth.DefaultInstance == null) return null;
FirebaseToken decodedToken = await FirebaseAuth.DefaultInstance.VerifyIdTokenAsync(idToken);
Debug.WriteLine(decodedToken.Uid);
string? email = decodedToken.Claims["email"]?.ToString();
return email?.EndsWith("@eotvos-tata.org") != true ? null : email;
}
catch (FirebaseAuthException)
{
return null;
}
}
}