Beispiel #1
0
        private async Task _applyImplicitToken(ApplyTokenResponseContext context)
        {
            if (!String.IsNullOrWhiteSpace(context.Response.AccessToken))
            {
                var at = context.Response.AccessToken;
                var ei = context.Response.ExpiresIn ?? 0;

                var authResult = await context.HttpContext.AuthenticateAsync(OpenIdConnectServerDefaults.AuthenticationScheme);

                string clientid = authResult.Principal.Claims.Where(x => x.Type == "client_id").First().Value;
                string userid   = authResult.Principal.Claims.Where(x => x.Type == "sub").First().Value;


                // Replace the old Tokens with the new ones
                // If the user has an existing refresh or access token from the same application, they are both erased in favor of the one new Access token.
                MockifyDbContext DatabaseContext = context.HttpContext.RequestServices.GetRequiredService <MockifyDbContext>();
                ApplicationUser  au = await DatabaseContext.ApplicationUser.Include(x => x.UserApplicationTokens).Where(x => x.Id == userid).FirstOrDefaultAsync();

                foreach (UserApplicationToken old in au.UserApplicationTokens.Where(x => x.ClientId == clientid))
                {
                    au.UserApplicationTokens.Remove(old);
                }
                au.UserApplicationTokens.Add(new UserApplicationToken()
                {
                    ClientId = clientid, TokenType = "access_token", TokenValue = at, ExpiresAt = DateTime.UtcNow.AddSeconds(ei)
                });

                await DatabaseContext.SaveChangesAsync();

                _stripUnnecessaryResponseParameters(context);
            }
        }
Beispiel #2
0
        private async Task _applyClientCredentialsToken(ApplyTokenResponseContext context)
        {
            if (!String.IsNullOrWhiteSpace(context.Response.AccessToken))
            {
                var    at       = context.Response.AccessToken;
                string clientid = context.Request.ClientId;
                var    ei       = context.Response.ExpiresIn ?? 0;

                // Write this Client Access Token to the database, replacing any old one that may be in use.
                MockifyDbContext      DatabaseContext = context.HttpContext.RequestServices.GetRequiredService <MockifyDbContext>();
                RegisteredApplication ra = await DatabaseContext.Applications.Include(x => x.ClientCredentialToken).FirstOrDefaultAsync(x => x.ClientId == clientid);

                if (ra == null)
                {
                    // ??
                    return;
                }
                else
                {
                    ra.ClientCredentialToken = new UserApplicationToken()
                    {
                        ClientId = clientid, TokenType = "client_credential", TokenValue = at, ExpiresAt = DateTime.UtcNow.AddSeconds(ei)
                    };
                    await DatabaseContext.SaveChangesAsync();

                    _stripUnnecessaryResponseParameters(context);
                }
            }
        }
Beispiel #3
0
 public AuthorizeController(UserManager <ApplicationUser> userManager, SignInManager <ApplicationUser> signInManager, ILogger <AccountController> logger, MockifyDbContext mc)
 {
     _userManager    = userManager;
     _signInManager  = signInManager;
     _logger         = logger;
     _mockifyContext = mc;
 }
Beispiel #4
0
        private async Task _applyRefreshToken(ApplyTokenResponseContext context)
        {
            if (!String.IsNullOrWhiteSpace(context.Response.AccessToken))
            {
                var at = context.Response.AccessToken;

                var authResult = await context.HttpContext.AuthenticateAsync(OpenIdConnectServerDefaults.AuthenticationScheme);

                string clientid = authResult.Principal.Claims.Where(x => x.Type == "client_id").First().Value;
                string userid   = authResult.Principal.Claims.Where(x => x.Type == "sub").First().Value;


                // Replace the old Tokens with the new ones
                MockifyDbContext DatabaseContext = context.HttpContext.RequestServices.GetRequiredService <MockifyDbContext>();
                ApplicationUser  au = await DatabaseContext.ApplicationUser.Include(x => x.UserApplicationTokens).Where(x => x.Id == userid).FirstOrDefaultAsync();

                foreach (UserApplicationToken old in au.UserApplicationTokens.Where(x => x.ClientId == clientid && x.TokenType == "access_token").ToList())
                {
                    au.UserApplicationTokens.Remove(old);
                }
                au.UserApplicationTokens.Add(new UserApplicationToken()
                {
                    ClientId = clientid, TokenType = "access_token", TokenValue = at
                });

                await DatabaseContext.SaveChangesAsync();

                _stripUnnecessaryResponseParameters(context);
            }
        }
Beispiel #5
0
 protected override async Task ExecuteAsync(CancellationToken cancellationToken)
 {
     while (!cancellationToken.IsCancellationRequested)
     {
         try {
             using (IServiceScope scope = scopeFactory.CreateScope()) {
                 MockifyDbContext        mc            = scope.ServiceProvider.GetRequiredService <MockifyDbContext>();
                 IQueryable <RateLimits> ExpiredLimits = mc.RateLimits.Where(x => (x.WindowStartTime + x.RateWindow) <= DateTime.UtcNow);
                 if (ExpiredLimits.Any())
                 {
                     /* All Expired Rate Limits */
                     foreach (RateLimits rl in ExpiredLimits)
                     {
                         rl.CurrentCalls = 0;
                     }
                     await mc.SaveChangesAsync();
                 }
             }
         }
         catch (Exception e) {
             _logger.LogError("Failed to clear rate limits", e);
         }
         await Task.Delay(ts, cancellationToken);
     }
 }
Beispiel #6
0
 public AccountController(
     UserManager <ApplicationUser> userManager,
     SignInManager <ApplicationUser> signInManager,
     MockifyDbContext mockifyContext,
     IEmailSender emailSender,
     ILogger <AccountController> logger)
 {
     _userManager   = userManager;
     _signInManager = signInManager;
     _emailSender   = emailSender;
     _logger        = logger;
     _mc            = mockifyContext;
 }
Beispiel #7
0
 public PublicAPIController(MockifyDbContext mc, ILogger <PublicAPIController> logger)
 {
     this._mc     = mc;
     this._logger = logger;
     if (ServerSettings.Settings == null)
     {
         try {
             this._serverSettings    = _mc.ServerSettings.Include(x => x.RateLimits).Include(x => x.Endpoints).First();
             ServerSettings.Settings = this._serverSettings;
         }
         catch (InvalidOperationException e) {
             //Make a default Server Settings
             this._serverSettings    = ServerSettings.DEFAULT;
             ServerSettings.Settings = this._serverSettings;
         }
     }
     else
     {
         this._serverSettings = ServerSettings.Settings;
     }
 }
 public RegisteredApplicationController(MockifyDbContext context)
 {
     this._context = context;
 }
 public ValidationAuthorizationService(MockifyDbContext mc, UserManager <ApplicationUser> um, ILogger <ValidationAuthorizationService> logger)
 {
     this._mc     = mc;
     this._um     = um;
     this._logger = logger;
 }
Beispiel #10
0
 public ServerSettingsController(UserManager <ApplicationUser> userManager, SignInManager <ApplicationUser> signInManager, MockifyDbContext mockifyContext, ILogger <AccountController> logger)
 {
     _logger = logger;
     _mc     = mockifyContext;
 }
Beispiel #11
0
 public RateLimitService(MockifyDbContext mc, ILogger <RateLimitService> logger)
 {
     this._mc     = mc;
     this._logger = logger;
 }
Beispiel #12
0
 public ApplicationsController(MockifyDbContext context)
 {
     this._context = context;
 }