Ejemplo n.º 1
0
        /// <summary>
        /// Authenticates this CoreRemoting client instance with the specified credentials.
        /// </summary>
        /// <exception cref="SecurityException">Thrown, if authentication failed or timed out</exception>
        private void Authenticate()
        {
            if (_config.Credentials == null || (_config.Credentials != null && _config.Credentials.Length == 0))
            {
                return;
            }

            if (_authenticationCompletedWaitHandle.IsSet)
            {
                return;
            }

            byte[] sharedSecret =
                MessageEncryption
                    ? _sessionId.ToByteArray()
                    : null;

            var authRequestMessage =
                new AuthenticationRequestMessage()
            {
                Credentials = _config.Credentials
            };

            var wireMessage =
                MessageEncryptionManager.CreateWireMessage(
                    messageType: "auth",
                    serializer: Serializer,
                    serializedMessage: Serializer.Serialize(authRequestMessage),
                    keyPair: _keyPair,
                    sharedSecret: sharedSecret);

            byte[] rawData = Serializer.Serialize(wireMessage);

            _rawMessageTransport.LastException = null;

            _rawMessageTransport.SendMessage(rawData);

            if (_rawMessageTransport.LastException != null)
            {
                throw _rawMessageTransport.LastException;
            }

            _authenticationCompletedWaitHandle.Wait(_config.AuthenticationTimeout * 1000);

            if (!_authenticationCompletedWaitHandle.IsSet)
            {
                throw new SecurityException("Authentication timeout.");
            }

            if (!_isAuthenticated)
            {
                throw new SecurityException("Authentication failed. Please check credentials.");
            }
        }
Ejemplo n.º 2
0
        private async Task <IEnumerable <T> > ReceiveAsync <T>()
        {
            ResponseStatus status;
            var            result = new List <T>();

            do
            {
                var received = await _webSocketConnection.ReceiveMessageAsync().ConfigureAwait(false);

                var receivedMessage = _messageSerializer.DeserializeMessage <ResponseMessage <T> >(received);

                status = receivedMessage.Status;
                status.ThrowIfStatusIndicatesError();

                if (receivedMessage.Result.Data != null)
                {
                    result.AddRange(receivedMessage.Result.Data);
                }

                if (status.Code == ResponseStatusCode.Authenticate)
                {
                    if (string.IsNullOrEmpty(_username) || string.IsNullOrEmpty(_password))
                    {
                        throw new InvalidOperationException(
                                  $"The Gremlin Server requires authentication, but no credentials are specified - username: {_username}, password: {_password}.");
                    }

                    var message = new AuthenticationRequestMessage
                    {
                        Arguments = new AuthenticationRequestArguments(_username, _password)
                    };

                    await SendAsync(message).ConfigureAwait(false);
                }
            } while (status.Code == ResponseStatusCode.PartialContent || status.Code == ResponseStatusCode.Authenticate);

            return(result);
        }
        /// Verify the token in the message and return some data about it
        private async Task <ResultMessage> VerifyToken(AuthenticationRequestMessage message, MessageContext context)
        {
            using (var scope = _scopeFactory.CreateScope())
            {
                var accountService      = scope.ServiceProvider.GetRequiredService <AccountService>();
                var tokenValidationArgs = await accountService.ValidateAuthToken(message.AuthToken);

                if (!tokenValidationArgs.IsValid)
                {
                    throw new Exception("Token is invalid");
                }

                return(new ResultMessage {
                    status = ResultMessage.Status.Ok,
                    data = new AuthenticationData {
                        UserId = tokenValidationArgs.Token.UserId,
                        Username = "",
                        TokenId = tokenValidationArgs.Token.Id,
                        Token = tokenValidationArgs.Token.Token
                    },
                });
            }
        }