Ejemplo n.º 1
0
        public virtual async Task <TModel> Insert(TInsert request)
        {
            var entity = _mapper.Map <TDatabase>(request);

            _context.Set <TDatabase>().Add(entity);
            await _context.SaveChangesAsync();

            return(_mapper.Map <TModel>(entity));
        }
Ejemplo n.º 2
0
        public async Task <bool> AddPerformer(int eventId, int performerId)
        {
            var entity = new EventPerformer
            {
                EventID     = eventId,
                PerformerID = performerId
            };

            try
            {
                await _context.EventPerformers.AddAsync(entity);

                await _context.SaveChangesAsync();

                return(true);
            }
            catch
            {
                return(false);
            }
        }
Ejemplo n.º 3
0
        public async override Task <ClientResponse> Update(int id, ClientUpdateRequest request)
        {
            var entity = await _context.Clients
                         .Include(i => i.Person)
                         .ThenInclude(i => i.Account)
                         .Where(i => i.ID == id)
                         .SingleOrDefaultAsync();

            if (entity != null)
            {
                _context.Attach(entity);
                _context.Update(entity);

                _mapper.Map(request, entity);
                _mapper.Map(request, entity.Person);
                _mapper.Map(request, entity.Person.Account);

                await _context.SaveChangesAsync();
            }

            return(_mapper.Map <ClientResponse>(entity));
        }
Ejemplo n.º 4
0
        public async Task <AuthenticationResult> RefreshTokenAsync(RefreshTokenRequest request)
        {
            var validatedToken = GetPrincipalFromToken(request.Token);

            if (validatedToken == null)
            {
                return(new AuthenticationResult {
                    Errors = new[] { "Invalid Token" }
                });
            }

            var expiryDateUnix =
                long.Parse(validatedToken.Claims.Single(x => x.Type == JwtRegisteredClaimNames.Exp).Value);

            var expiryDateTimeUtc = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
                                    .AddSeconds(expiryDateUnix);

            if (expiryDateTimeUtc > DateTime.UtcNow)
            {
                return(new AuthenticationResult {
                    Errors = new[] { "This token hasn't expired yet" }
                });
            }

            var jti = validatedToken.Claims.Single(x => x.Type == JwtRegisteredClaimNames.Jti).Value;

            var storedRefreshToken = await _context.RefreshTokens.SingleOrDefaultAsync(x => x.Token == request.RefreshToken);

            if (storedRefreshToken == null)
            {
                return(new AuthenticationResult {
                    Errors = new[] { "This refresh token does not exist" }
                });
            }

            if (DateTime.UtcNow > storedRefreshToken.ExpiryDate)
            {
                return(new AuthenticationResult {
                    Errors = new[] { "This refresh token has expired" }
                });
            }

            if (storedRefreshToken.Invalidated)
            {
                return(new AuthenticationResult {
                    Errors = new[] { "This refresh token has been invalidated" }
                });
            }

            if (storedRefreshToken.Used)
            {
                return(new AuthenticationResult {
                    Errors = new[] { "This refresh token has been used" }
                });
            }

            if (storedRefreshToken.JwtId != jti)
            {
                return(new AuthenticationResult {
                    Errors = new[] { "This refresh token does not match this JWT" }
                });
            }

            storedRefreshToken.Used = true;
            _context.RefreshTokens.Update(storedRefreshToken);
            await _context.SaveChangesAsync();

            var account = await _context.RefreshTokens
                          .AsNoTracking()
                          .Include(i => i.Account)
                          .Where(i => i.Token == request.Token)
                          .Select(i => i.Account)
                          .SingleOrDefaultAsync();

            return(await GenerateAuthenticationResultForAccountAsync(account));
        }