/// <summary>
        /// Establish an authenticated connection to the miniserver. Fires the OnAuthenticated event when successfull.
        /// After the event fired, the connection to the miniserver can be used.
        /// </summary>
        /// <param name="handler">The tokenhandler that should be used</param>
        public async override Task Authenticate(TokenHandler handler)
        {
            if (await MiniserverReachable())
            {
                WebSocket = new ClientWebSocket();
                await WebSocket.ConnectAsync(new Uri($"ws://{IP}:{Port}/ws/rfc6455"), CancellationToken.None);

                BeginListening();
                string key = await Session.GetSessionKey();

                string keyExchangeResponse = (await SendWebservice(new WebserviceRequest <string>($"jdev/sys/keyexchange/{key}", EncryptionType.None))).Value;
                TokenHandler = handler;
                if (TokenHandler?.Token != null)
                {
                    string hash = await TokenHandler?.GetTokenHash();

                    string       response     = (await SendWebservice(new WebserviceRequest <string> ($"authwithtoken/{hash}/{TokenHandler.Username}", EncryptionType.RequestAndResponse))).Value;
                    AuthResponse authResponse = JsonConvert.DeserializeObject <AuthResponse>(response);
                    if (authResponse.ValidUntil != default && authResponse.TokenRights != default)
                    {
                        OnAuthenticated.BeginInvoke(this, new ConnectionAuthenticatedEventArgs(TokenHandler), null, null);
                        return;
                    }
                }
                if (await TokenHandler.RequestNewToken())
                {
                    OnAuthenticated.BeginInvoke(this, new ConnectionAuthenticatedEventArgs(TokenHandler), null, null);
                    return;
                }
                await HttpClient?.Authenticate(new TokenHandler(HttpClient, handler.Username, handler.Token, false));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a clone of a request and encrypts it
        /// </summary>
        /// <param name="request">The request that should be encrypted</param>
        /// <returns>the encrypted clone of the given request</returns>
        private async Task <WebserviceRequest> GetEncryptedRequest(WebserviceRequest request)
        {
            if (request == null)
            {
                return(null);
            }
            WebserviceRequest encRequest = (WebserviceRequest)request.Clone();

            if (request.NeedAuthentication && TokenHandler != null)              //add authentication if needed
            {
                if (TokenHandler.Token == null)
                {
                    await TokenHandler.RequestNewToken();
                }
                encRequest.Queries.Add("autht", await TokenHandler.GetTokenHash());
                encRequest.Queries.Add("user", TokenHandler.Username);
                encRequest.NeedAuthentication = false;
                if (encRequest.Encryption == EncryptionType.None)
                {
                    encRequest.Encryption = EncryptionType.Request;
                }
            }
            switch (encRequest.Encryption)
            {
            case EncryptionType.Request:
                encRequest.Command = "jdev/sys/enc/";
                break;

            case EncryptionType.RequestAndResponse:
                encRequest.Command = "jdev/sys/fenc/";
                break;

            case EncryptionType.None:
            default:
                return(encRequest);
            }
            string query = encRequest.Queries.HasKeys() ? $"?{encRequest.Queries.ToString()}" : "";

            encRequest.Command += Uri.EscapeDataString(Cryptography.AesEncrypt($"salt/{Session.Salt}/{request.Command}{query}", Session));
            encRequest.Queries.Clear();
            encRequest.Queries.Add("sk", await Session.GetSessionKey());
            encRequest.Encryption = EncryptionType.None;
            return(encRequest);
        }
Esempio n. 3
0
 /// <summary>
 /// Disposes the WebserviceClient
 /// </summary>
 public virtual void Dispose()
 {
     TokenHandler?.Dispose();
 }
Esempio n. 4
0
 /// <summary>
 /// Establish an authenticated connection to the miniserver
 /// </summary>
 /// <param name="handler">The tokenhandler that should be used</param>
 public abstract Task Authenticate(TokenHandler handler);
Esempio n. 5
0
 /// <summary>
 /// Provides info required for the authentication on the miniserver
 /// </summary>
 /// <param name="handler">The tokenHandler that should be used</param>
 public override Task Authenticate(TokenHandler handler)
 {
     TokenHandler = handler;
     return(Task.CompletedTask);
 }
 /// <summary>
 /// Initialises the eventArgs
 /// </summary>
 /// <param name="handler">The tokenHandler used for authentication</param>
 public ConnectionAuthenticatedEventArgs(TokenHandler handler)
 {
     TokenHandler = handler;
 }