コード例 #1
0
        public async Task PhoneVCodeCreateToken()
        {
            var user  = fillTestDbHelper.Users.FirstOrDefault();
            var vcode = await verificationCodesService.CreateVerificationCodeAsync(DateTime.UtcNow.ToUnixTime(), user.Id.ToString(), user.Id);

            await Assert.ThrowsAsync <WrongVerificationCodeException>(async() => await tokensService.PhoneVCodeCreateTokenPairAsync(user.Phones.FirstOrDefault().PhoneNumber, -1000, null));

            Assert.NotNull(await tokensService.PhoneVCodeCreateTokenPairAsync(user.Phones.FirstOrDefault().PhoneNumber, vcode, null));
            await Assert.ThrowsAsync <UserNotFoundException>(async() => await tokensService.PhoneVCodeCreateTokenPairAsync("+19996660000", 0, null));
        }
コード例 #2
0
        public async Task <Response> CreateResponseAsync()
        {
            try
            {
                clientConnection.FileAccessToken = RandomExtensions.NextString(64);
                TokensResponse response = default;
                if (request.DeviceTokenId != null && !string.IsNullOrWhiteSpace(request.DeviceTokenId))
                {
                    await tokensService.SetDeviceTokenIdNullAsync(request.DeviceTokenId).ConfigureAwait(false);
                }

                UserVm user = null;
                switch (request.LoginType)
                {
                case LoginType.VerificationCode:
                {
                    TokenVm token = null;
                    if (request.UidType == UidType.Phone)
                    {
                        token = await tokensService.PhoneVCodeCreateTokenPairAsync(request.Uid, request.VCode.GetValueOrDefault(), request.DeviceTokenId).ConfigureAwait(false);
                    }
                    else if (request.UidType == UidType.Email)
                    {
                        token = await tokensService.EmailVCodeCreateTokenPairAsync(request.Uid, request.VCode.GetValueOrDefault(), request.DeviceTokenId).ConfigureAwait(false);
                    }
                    else if (request.UidType == UidType.UserId)
                    {
                        token = await tokensService.UserIdVCodeCreateTokenPairAsync(Convert.ToInt64(request.Uid), request.VCode.GetValueOrDefault(), request.DeviceTokenId).ConfigureAwait(false);
                    }
                    clientConnection.UserId = token.UserId;
                    token = await tokensService.UpdateTokenDataAsync(request.OSName, request.DeviceName, request.AppName, clientConnection.ClientIP.ToString(), token).ConfigureAwait(false);

                    clientConnection.CurrentToken = token;
                    user = await loadUsersService.GetUserInformationAsync(token.UserId).ConfigureAwait(false);

                    response = new TokensResponse(request.RequestId, token, clientConnection.FileAccessToken, null, user);
                    noticeService.SendNewSessionNoticeAsync(clientConnection);
                }
                break;

                case LoginType.AccessTokenAndUserId:
                {
                    TokenVm token = new TokenVm
                    {
                        UserId        = request.Token.UserId,
                        AccessToken   = request.Token.AccessToken,
                        RefreshToken  = request.Token.RefreshToken,
                        DeviceTokenId = request.DeviceTokenId
                    };
                    token = await tokensService.CheckTokenAsync(token, NodeSettings.Configs.Node.Id).ConfigureAwait(false);

                    clientConnection.UserId = token.UserId;
                    token = await tokensService.UpdateTokenDataAsync(request.OSName, request.DeviceName, request.AppName, clientConnection.ClientIP.ToString(), token).ConfigureAwait(false);

                    clientConnection.CurrentToken = token;
                    user = await loadUsersService.GetUserInformationAsync(token.UserId).ConfigureAwait(false);

                    response = new TokensResponse(request.RequestId, token, clientConnection.FileAccessToken, null, user);
                }
                break;

                case LoginType.Password:
                {
                    ValuePair <TokenVm, string> tokenPasswordPair;
                    if (request.UidType == UidType.Phone)
                    {
                        tokenPasswordPair = await tokensService.PhonePasswordCreateTokenPairAsync(request.Uid, request.Password, request.DeviceTokenId).ConfigureAwait(false);
                    }
                    else if (request.UidType == UidType.Email)
                    {
                        tokenPasswordPair = await tokensService.EmailPasswordCreateTokenPairAsync(request.Uid, request.Password, request.DeviceTokenId).ConfigureAwait(false);
                    }
                    else if (request.UidType == UidType.UserId)
                    {
                        tokenPasswordPair = await tokensService.UserIdPasswordCreateTokenPairAsync(Convert.ToInt64(request.Uid), request.Password, request.DeviceTokenId).ConfigureAwait(false);
                    }
                    else
                    {
                        var errorObject = new
                        {
                            UidType = "Unknown UidType."
                        };
                        return(new ResultResponse(request.RequestId, ObjectSerializer.ObjectToJson(errorObject), ErrorCode.WrongArgumentError));
                    }
                    clientConnection.UserId      = tokenPasswordPair.FirstValue.UserId;
                    tokenPasswordPair.FirstValue =
                        await tokensService.UpdateTokenDataAsync(request.OSName, request.DeviceName, request.AppName, clientConnection.ClientIP.ToString(), tokenPasswordPair.FirstValue)
                        .ConfigureAwait(false);

                    clientConnection.CurrentToken = tokenPasswordPair.FirstValue;
                    user = await loadUsersService.GetUserInformationAsync(clientConnection.UserId.Value).ConfigureAwait(false);

                    response = new TokensResponse(
                        request.RequestId,
                        tokenPasswordPair.FirstValue,
                        clientConnection.FileAccessToken,
                        tokenPasswordPair.SecondValue,
                        user);
                    noticeService.SendNewSessionNoticeAsync(clientConnection);
                }
                break;
                }
                clientConnection.Confirmed = user.Confirmed;
                clientConnection.Banned    = user.Banned;
                connectionsService.AddOrUpdateUserConnection(clientConnection.UserId.Value, clientConnection);
                SendPendingMessagesAsync();
                return(response);
            }
            catch (InvalidTokenException ex)
            {
                Logger.WriteLog(ex, request);
                return(new ResultResponse(request.RequestId, "Invalid token.", ErrorCode.InvalidAccessToken));
            }
            catch (WrongVerificationCodeException ex)
            {
                Logger.WriteLog(ex, request);
                return(new ResultResponse(request.RequestId, "Invalid verification code.", ErrorCode.WrongVerificationCode));
            }
            catch (UserNotFoundException ex)
            {
                Logger.WriteLog(ex, request);
                return(new ResultResponse(request.RequestId, "User not found.", ErrorCode.UserNotFound));
            }
            catch (CreateTokenPairException ex)
            {
                Logger.WriteLog(ex, request);
                return(new ResultResponse(request.RequestId, "Login failed.", ErrorCode.AuthorizationProblem));
            }
            catch (TokensTimeoutException ex)
            {
                Logger.WriteLog(ex, request);
                await noticeService.SendNeedLoginNoticeAsync(clientConnection).ConfigureAwait(false);

                return(new ResultResponse(request.RequestId, "Refresh token expired.", ErrorCode.RefreshTokenTimeout));
            }
            catch (UserFromAnotherNodeException ex)
            {
                await MetricsHelper.Instance.SetCrossNodeApiInvolvedAsync(request.RequestId).ConfigureAwait(false);

                var userToken = await nodeRequestSender.CheckTokenAsync(
                    request.Token.UserId,
                    request.Token,
                    ex.NodeId.GetValueOrDefault()).ConfigureAwait(false);

                if (userToken != null)
                {
                    clientConnection.UserId             = userToken.FirstValue.UserId;
                    clientConnection.ProxyNodeWebSocket = connectionsService.GetNodeConnection(ex.NodeId.GetValueOrDefault()).NodeWebSocket;
                    connectionsService.AddOrUpdateUserConnection(clientConnection.UserId.Value, clientConnection);
                    return(new TokensResponse(request.RequestId, userToken.FirstValue, clientConnection.FileAccessToken, null, userToken.SecondValue));
                }
                return(new ResultResponse(request.RequestId, "Login failed.", ErrorCode.AuthorizationProblem));
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex, request);
                return(new ResultResponse(request.RequestId, null, ErrorCode.UnknownError));
            }
        }