Exemplo n.º 1
0
        public async Task <AuthResult> ExecuteAsync(AddConnectionRequest request)
        {
            var cacheItem = await _cacheItemRepository.GetAsync(request.Context);

            if (cacheItem.Nonce != request.Nonce || cacheItem.Status != CacheItemStatus.Popped)
            {
                throw new CommandValidationException(
                          $"No cacheItem with context {request.Context} and nonce {request.Nonce}");
            }

            if (cacheItem.FlowType != FlowType.Fido2Register && cacheItem.FlowType != FlowType.PartialAuthorize)
            {
                throw new CommandValidationException($"Wrong flow type {cacheItem.FlowType}");
            }

            var connectionState = await _accountLinkHandler.GetCurrentUserLinkStateAsync(request.Payload);

            if (connectionState.ConnectedDevicesCount >= _coreConfiguration.MaximumNumberOfConnectedDevices)
            {
                return(new AuthResult(_localizationService.GetLocalizedString("Error_PhoneAlreadyConnected")));
            }

            var connectionExists = !string.IsNullOrEmpty(cacheItem.Fido2CredentialId)
                ? await _userHandlerAdapter.IsFido2UserExistsAsync(cacheItem.Fido2CredentialId)
                : await _userHandlerAdapter.IsUserExistsAsync(cacheItem.PublicKey);

            if (connectionExists)
            {
                return(new AuthResult("Can not add connection. Connection already exists"));
            }

            await _accountLinkHandler.OnLinkAsync(connectionState.DID, new OwnIdConnection
            {
                Fido2CredentialId     = cacheItem.Fido2CredentialId,
                Fido2SignatureCounter = cacheItem.Fido2SignatureCounter.ToString(),
                PublicKey             = cacheItem.PublicKey,
                RecoveryToken         = cacheItem.RecoveryToken,
                RecoveryData          = cacheItem.RecoveryData,
                AuthType = cacheItem.AuthCookieType switch
                {
                    CookieType.Fido2 => ConnectionAuthType.Fido2,
                    CookieType.Passcode => ConnectionAuthType.Passcode,
                    _ => ConnectionAuthType.Basic
                }
            });
Exemplo n.º 2
0
        public IActionResult AddConnection(AddConnectionRequest model)
        {
            var loginId = ((Login)HttpContext.Items["Login"]).LoginId;

            //check if user already has registered this connection

            //get all connections from this user
            var connections = ctx.ConnectionTables.ToList().Where(c => c.LoginId == loginId).ToList();

            if (connections.Exists(c => c.Database == model.Database &&
                                   c.Host == model.Host &&
                                   c.SqlPlatformId == Int32.Parse(model.SqlPlatformId) &&
                                   c.Port == model.Port))
            {
                return(BadRequest(new
                {
                    error = true,
                    message = "Identicall connection already registered."
                }));
            }

            ConnectionTable newConnection = new ConnectionTable
            {
                SqlPlatformId = Int32.Parse(model.SqlPlatformId),
                Database      = model.Database,
                Host          = model.Host,
                LoginId       = loginId,
                Username      = model.Username,
                Port          = model.Port
            };

            ctx.Add(newConnection);
            ctx.SaveChanges();

            return(Ok(new
            {
                error = false,
                message = "Connection successfully created."
            }));
        }