Exemplo n.º 1
0
        public async Task <Response <CheckOneTimeCodeResult, CheckOneTimeCodeStatus> > CheckOneTimeCodeAsync(string longCode, string clientNonce)
        {
            _logger.LogTrace("Checking long code");

            if (string.IsNullOrEmpty(longCode) || longCode.Length > PasswordlessLoginConstants.OneTimeCode.LongCodeMaxLength)
            {
                _logger.LogError("The long code provided had an invalid format.");
                return(new Response <CheckOneTimeCodeResult, CheckOneTimeCodeStatus>(
                           CheckOneTimeCodeStatus.Error(_localizer["The one time code had an invalid format."], CheckOneTimeCodeStatusCode.CodeIncorrect)));
            }

            var response = await _oneTimeCodeStore.GetOneTimeCodeByLongCodeAsync(longCode);

            if (response.HasError)
            {
                return(new Response <CheckOneTimeCodeResult, CheckOneTimeCodeStatus>(
                           CheckOneTimeCodeStatus.Error(_localizer["One time code not found."], CheckOneTimeCodeStatusCode.NotFound)));
            }
            var otc = response.Result;

            if (otc.ExpiresUTC < DateTime.UtcNow)
            {
                _logger.LogDebug("The one time code has expired.");
                return(new Response <CheckOneTimeCodeResult, CheckOneTimeCodeStatus>(
                           new CheckOneTimeCodeResult(otc),
                           CheckOneTimeCodeStatus.Error(_localizer["The one time code has expired."], CheckOneTimeCodeStatusCode.Expired)));
            }
            return(await ExpireTokenAndValidateNonceAsync(otc, clientNonce));
        }
        public async Task <CheckOneTimeCodeResponse> CheckOneTimeCodeAsync(string longCode)
        {
            if (string.IsNullOrEmpty(longCode) || longCode.Length > 36)
            {
                return(new CheckOneTimeCodeResponse(CheckOneTimeCodeResult.CodeIncorrect));
            }

            var longCodeHash = GetFastHash(longCode);
            var otc          = await _oneTimeCodeStore.GetOneTimeCodeByLongCodeAsync(longCodeHash);

            if (otc == null)
            {
                return(new CheckOneTimeCodeResponse(CheckOneTimeCodeResult.NotFound));
            }
            if (otc.ExpiresUTC < DateTime.UtcNow)
            {
                return(new CheckOneTimeCodeResponse(CheckOneTimeCodeResult.Expired));
            }

            await _oneTimeCodeStore.ExpireOneTimeCodeAsync(otc.SentTo);

            return(new CheckOneTimeCodeResponse(CheckOneTimeCodeResult.Verified, otc.SentTo, otc.RedirectUrl));
        }