예제 #1
0
        public async Task <IActionResult> RefreshToken([FromBody] RefreshRegistrationTokenDto tokenRefresh)
        {
            if (tokenRefresh is null)
            {
                return(StatusCode(new ErrorMessage
                {
                    ErrorCode = "TRANSACTION.REGISTRATION.REFRESH.NULL",
                    Message = "Please send valid data",
                    StatusCode = System.Net.HttpStatusCode.BadRequest
                }));
            }

            var user = _credentialsGetter.GetCuurentUser();

            if (user == null)
            {
                return(StatusCode(new ErrorMessage
                {
                    ErrorCode = "TRANSACTION.REGISTRATION.REFRESH.USER.UNKOWN",
                    Message = "Please login",
                    StatusCode = System.Net.HttpStatusCode.Unauthorized
                }));
            }
            var registrations = from r in _registrationsRepo.Table
                                where r.Object.OriginalObjectId == tokenRefresh.ObjectId && r.RecipientLogin.UserId == Guid.Parse(user.UserId) &&
                                r.Status == ObjectRegistrationStatus.OK && r.ExpiresAtUtc > DateTime.UtcNow && r.ObjectReceiving == null
                                select r;

            if (!registrations.Any())
            {
                return(StatusCode(new ErrorMessage
                {
                    ErrorCode = "TRANSACTION.REGISTRATION.REFRESH.NOT.VALID",
                    Message = "You have no valid registration",
                    StatusCode = System.Net.HttpStatusCode.BadRequest
                }));
            }

            var token = await _tokenManager.GenerateToken(registrations.FirstOrDefault().ObjectRegistrationId, TokenType.Receiving);

            return(Ok(new RegistrationTokenResultDto
            {
                CreatedAtUtc = token.IssuedAtUtc,
                RegistrationToken = token.Token,
                UseBeforeUtc = token.UseBeforeUtc
            }));
        }
        public async Task <IActionResult> RefreshToken([FromBody] RefreshRegistrationTokenDto tokenRefresh)
        {
            var result = await _tokenRefresher.RefreshToken(tokenRefresh.ObjectId);

            return(StatusCode(result));
        }