Ignore:
Timestamp:
09/18/22 18:09:53 (21 months ago)
Author:
Danilo <danilo.najkov@…>
Branches:
master
Parents:
49b0bbd
Message:

vip functionallity + menu fields + alergens filtering + google/fb login + email queueing

File:
1 edited

Legend:

Unmodified
Added
Removed
  • resTools_backend/backend/Helpers/JwtMiddleware.cs

    r49b0bbd r13f1472  
    66using backend.Services;
    77using backend.Helpers;
     8using Google.Apis.Auth;
     9using System.Text.Json;
     10using Newtonsoft.Json;
    811
    912public class JwtMiddleware
     
    1114    private readonly RequestDelegate _next;
    1215    private readonly AppSettings _appSettings;
     16    private static readonly HttpClient client = new HttpClient();
     17
    1318
    1419    public JwtMiddleware(RequestDelegate next, IOptions<AppSettings> appSettings)
     
    2227        var token = context.Request.Headers["Authorization"].FirstOrDefault()?.Split(" ").Last();
    2328
    24         if (token != null)
    25             attachUserToContext(context, userService, token);
     29        if (token != null && token != "null")
     30            await attachUserToContext(context, userService, token);
    2631
    2732        await _next(context);
    2833    }
    2934
    30     private void attachUserToContext(HttpContext context, IUserService userService, string token)
     35    private async Task attachUserToContext(HttpContext context, IUserService userService, string token)
    3136    {
    3237        try
    3338        {
     39            var fbResult = await this.ValidateFacebookToken(token);
     40            if (fbResult != null)
     41            {
     42                context.Items["User"] = fbResult;
     43                return;
     44            }
     45
    3446            var tokenHandler = new JwtSecurityTokenHandler();
    3547            var key = System.Text.Encoding.ASCII.GetBytes(_appSettings.Secret);
     
    4759
    4860            context.Items["User"] = userId;
     61
    4962        }
    5063        catch
    5164        {
    52             // do nothing if jwt validation fails
     65            try
     66            {
     67                var result = await GoogleJsonWebSignature.ValidateAsync(token);
     68                context.Items["User"] = result.Email;
     69            }
     70            catch
     71            {
     72                // do nothing
     73            }
    5374        }
    5475    }
     76
     77    private async Task<string> ValidateFacebookToken(string token)
     78    {
     79        try
     80        {
     81            var stringTask = await client.GetStringAsync("https://graph.facebook.com/me?fields=email&access_token=" + token);
     82            var obj = JsonConvert.DeserializeObject<FacebookResult>(stringTask);
     83            return obj.email;
     84        }
     85        catch
     86        {
     87            return null;
     88        }
     89    }
     90
     91    internal class FacebookResult
     92    {
     93        public string email { get; set; }
     94        public string id { get; set; }
     95    }
    5596}
Note: See TracChangeset for help on using the changeset viewer.