chkr1011 / Wirehome.Core

Wirehome.Core is a home automation system written in C# targeting .NET Core. It runs on Linux, Windows and macOS.
Apache License 2.0
254 stars 63 forks source link

user name and password with wirehome cloud #15

Open masterchen opened 5 years ago

masterchen commented 5 years ago

after launching the web ,how to log in

chkr1011 commented 5 years ago

If you deployed the cloud service you should login with "https://XYZ.azurewebsites.net/Cloud/Account/Login". After login you can also open links to the App etc.

Please make sure you have the proper files in place with user accounts.

masterchen commented 5 years ago

Thanks ,i just launch the wirehome cloud in vs 2017 ,i can see the login page but doesnt have acount and password

chkr1011 commented 5 years ago

You need to create the accounts in the file system. Please have a look at class RepositoryService. There you find the root path and the object IdentityConfiguration which is deserialized from a JSON file. Those files contain the user and password hashes.

masterchen commented 5 years ago

i just launch the Wirehome.Cloud in vs 2017 , Sorry , there is no identtites under the folowing path: C:\Users\dell\AppData\Roaming\Wirehome so i tried to creat it.: C:\Users\dell\AppData\Roaming\Wirehome.Cloud\Identities\ xxxx@gmail.com\Configuration.js

but i don't know what content should be added to it. and the hashes should not easy to do by hand , can you give a sampe

And is it possible to provide way i can register user acount and password ? Thanks.

chkr1011 commented 5 years ago

There are no files (accounts) by default. You need to create initial users via creating the proper files. I will add an API for this soon. But at the moment this is the only way. Here is an example of such a file:

{
    "PasswordHash": "AQAAAAEAACcQAAAAEHA8zFk0OdqwNjSZqovgGGgBZjEWv+wW44ZOm0gvVApxkeGLmGKRe2NabDcw138TrA==",
    "IsLocked": false,
    "Channels": {
        "default": {}
    }
}

The password is hashed. The hash above is for the password "123456" (without the "). You can login using that default password and change it via the API (please check Swagger UI for the cloud API).

masterchen commented 5 years ago

Thank you for your quickly reply, i can login now by the default password. after login , there are three links , but all of them redirect to the page "Your device is not connected. Please try again later." while on clicked , i read the wiki again, still can't find the answer, i was wondering where i did wrong or should i creat a dummy device ? if so how to .
Sorry for this unwised questions

chkr1011 commented 5 years ago

The problem is that I don't have much time left to update/extend the wiki. So you questions are all fine and reasonable.

After setting up the cloud account you need to update the cloud settings at the Wirehome.Core in the file CloudServiceConfiguration.json. The password there is not encrypted and plain text. The IdentityUid is the e-mail address you used. And you need to set the proper host according to your hosting strategy. After restarting Wirehome.Core you should see some messages in the log indicating whether the connection is up and running or failed.

masterchen commented 5 years ago

@chkr1011 Long time passed, i'm back again. now i can login, and there is log info in the terminal shows login OK but device not connected.

what i need to do next ? if i need creat a mqtt client to connect to this server ?

Or could you please give a short guide i can follow to do , thank

i'm run Wirehome.Cloud in windows 10 with vs2017.

--------------------Config ------------------------------- CloudServiceConfiguration.json contents are : { "isEnabled": true, "host": "127.0.0.1:54128", "identityUid": chenfeng10000@126.com, "password": 123456, "channelUid": "default", "reconnectDelay": "00:00:10" }

------------------------LOG ----------------------------------------------

Wirehome.Cloud> info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[1] Wirehome.Cloud> Authorization was successful. Wirehome.Cloud> info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1] Wirehome.Cloud> Executing action method Wirehome.Cloud.Controllers.ChannelController.DeviceNotConnected (Wirehome.Cloud) - Validation state: Valid ...... Wirehome.Cloud> Executing ViewResult, running view DeviceNotConnected. ... Wirehome.Cloud> Executed action Wirehome.Cloud.Controllers.ChannelController.DeviceNotConnected (Wirehome.Cloud) in 39.0311ms Wirehome.Cloud> info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2] Wirehome.Cloud> Request finished in 46.5352ms 200 text/html; charset=utf-8

chkr1011 commented 5 years ago

Please check the device status using the cloud API (.../cloud/api/index.html). There you have the API " ​/api​/v1​/cloud​/statistics" which shows you if and which device is connected. Please let me know if your connection is listed there.

No you don't need a MQTT client. If the connection is established and your credentials are correct the cloud acts as a proxy for EVERY URL and forwards that you your device. So http://127.0.0.1:54128/app/ will be forwarded to your Pi, then invoke /app/ and returns the result. You can access ANY other web app like /api/, /configurator/ etc.

But first please let me know if your connection is shown in the statistics API.

masterchen commented 5 years ago

I'd like to decsribe what i have done so far:

1、i commit out history service in Startup file of WireHome.Core //serviceProvider.GetRequiredService().Start(); because ,i haven't install MariDB and mysql, current i installed postgres and mssql

2、runing the irehome.Core.Hosts.Console, 3、Launching Wirehome.Cloud, i can login to the http://localhost:54128/cloud/account, on the page i can see my account info and i can see three links: app, configure, api. but with link i clicked , page route to http://localhost:54128/Cloud/Channel/DeviceNotConnected.

4、nivagate to http://localhost:54128/api/v1/cloud/statistics。 i can see : {"connectedDevicesCount":0,"connectedDevices":[]} nivagate to http://localhost:54128/cloud/api/index.html, i can see the swagger. and in swagger i try ping api, i got 200 response, and say: Your device is not connected. Please try again later.

Then , while the console startup ,i see these error , i also change the CloudServiceConfig.json to : "host": "localhost:54128", the error still has " Error while connecting with Wirehome.Cloud service at host 'localhost:54128'." ------------------------------ERROR LOG ------------------------------------ nfo: Wirehome.Core.Python.PythonEngineService[0] Starting Python engine... fail: Wirehome.Core.Cloud.CloudService[0] Error while connecting with Wirehome.Cloud service at host '127.0.0.1:54128'. System.Net.WebSockets.WebSocketException (0x80004005): Unable to connect to the remote server ---> System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception. ---> System.IO.IOException: The handshake failed due to an unexpected packet format. at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest) at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)

masterchen commented 4 years ago

Hi Christian, in vs online debug Mode , runing Wirehome.Coud, navigate to : https://localhost:5001/api/v1/cloud/statistics, the result is :

{"connectedDevicesCount":0,"connectedDevices":[]}

and The cloudservcieconfiguration,json is:

{ "isEnabled": true, "host": "localhost:54128", "identityUid":"masterchen@gmail.com", "password": "123456", "channelUid": "default", "reconnectDelay": "00:00:10" } can you give further info what i need to check ? Thanks

chkr1011 commented 4 years ago

Hi, am currently rewriting the code of the cloud so that no longer the plain text password is required to login etc. So we have to start from scratch 😄

But I also added some more debug information so that we can find the issue easily. So please use the latest master branch for testing.

But I already saw that you use port 5001 at the cloud service but configured "localhost:54128" in Wirehome.Core. This cannot work at all because ports must be the same.

Let me know as soon as you have the latest master branch running. I will also add some sample configuration files to the repo.

masterchen commented 4 years ago

Hi Christian, Thank you very much for your reply. The Master has same problem.

By the way, because 80 port was occupied,so i changed the port from 80 to 8088 in the following files: CloudServicies.cs _httpClient.BaseAddress = new Uri("http://127.0.0.1:8088");
WirehomeCoreHost.cs .UseKestrel(o => o.ListenAnyIP(8088))

Now, i modified host address to localhost:5001 in the CloudServiceConfiguration.json file, the problem still there : {"connectedDevicesCount":0,"connectedDevices":[]}

I did a further debug , i "Found" if Wirehome.Cloud wants to has connected channel , there should have a websokcet client connection from wirehome.core.Hosts.console via "https://localhost:5001/Connector", and then Wirehome.Cloud call "deviceConnectorService.RunAsync", then assgin channel to " _openChannels[channelIdentifier] = openChannel;", is this right ?

And i also found there is no such call from wirehome.core.Hosts.console , when the CloudService Started , because the "_options.ChannelAccessToken" is NULL , so it jump to next cycle runing in ConnectAsync task. Please check:

Task.Run(() => ConnectAsync(cancellationTokenSource.Token),cancellationTokenSource.Token).Forget(_logger); /// if (string.IsNullOrEmpty(_options.ChannelAccessToken)) { continue; }

                using (var webSocketClient = new ClientWebSocket())
                {
                    using (var timeout = new CancellationTokenSource(_options.ReconnectDelay))
                    {
                       var url = $"wss://{_options.Host}/Connector";
                       .....
                    }

} //// I'm waiting for your help !

masterchen commented 4 years ago

Hi Christian, I add channelAccessToken in configuration.json file, Because i don't know it's value ,i set the key simillar as PasswordHash in Configuration.json file for test, like this: { "isEnabled": true, "host": "localhost:5001", "identityUid":"mastchen@gmail.com", "password": "123456", "channelUid": "default", "reconnectDelay": "00:00:10", "channelAccessToken": "AQAAAAEAACcQAAAAEHA8zFk0OdqwNjSZqovgGGgBZjEWv+wW44ZOm0gvVApxkeGLmGKRe2NabDcw138TrA==" }

Then, in VS2019, i launch the Cloud project with selction is IIS Express( if selection is Wirehome.Cloud itself, it can't get websocket call from websocket client ), then Wirehome.Cloud can get call , but there is an exception: //// Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request starting HTTP/1.1 GET http://localhost:5001/Connector
Exception:“System.UnauthorizedAccessException”(located Wirehome.Cloud.dll ) Exception:“System.UnauthorizedAccessException”(located System.Private.CoreLib.dll ) Microsoft.AspNetCore.Server.Kestrel: Information: Connection id "0HLSHSD6F0OS3", Request id "0HLSHSD6F0OS3:0000000D": the application aborted the connection. Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request finished in 62.9109ms 0 ///

Is it caused by wrong token ? Thanks. @chkr1011

chkr1011 commented 4 years ago

OK, let me summarize some things.

  1. Please use exactly the config from the repo for now. Then I can see if they are correct at all. (But change the host address for sure 😄 )
  2. Please start the cloud project WITHOUT the IIS. It must also work with that strategy.
  3. The Connector only supports HTTPS for security reasons. So please make sure that HTTPS is also available from the Wirehome.Cloud service. Visual Studio usually asks if a self signed certificate should be created and trusted. If you skipped this (by clicking "no") you may have to reset that and let VS setup everything properly.
  4. In the new Core version is a system status variable containing the Cloud connect exception. Please post the value in case that it still not works.
  5. Please make sure that you use the HTTPS and not HTTP port in the Core configuration.
masterchen commented 4 years ago

Hi Christian,

Thanks for your patient guidance, now Wirehome.core can properly connect to Cloud BUT when i click App and Confugurator link, both are show blank screen with 404 info in ternimal(see below info), but APIdefinition link works fine, i can see swagger pages,and there are many apis,such as mqtt .... And i can see device is connected info via statistics API:

{"connectedDevicesCount":1,"connectedDevices":[{"identityUid":"user@wirehome.com","channelUid":"user@wirehome.com"}]}

////// Request starting HTTP/2 GET https://localhost:5001/app/ info: Microsoft.AspNetCore.Hosting.Diagnostics[2] Request finished in 387.0195ms 404 info: Microsoft.AspNetCore.Hosting.Diagnostics[1] Request starting HTTP/2 GET https://localhost:5001/configurator/ info: Microsoft.AspNetCore.Hosting.Diagnostics[2] Request finished in 18.2087ms 404 /////

Currently i use config same as repo except two changes: 1、 Add "IdentityUid":"user@wirehome.com" // without this ,there will be an exception "ChannelUid": "user@wirehome.com",

2、 changing Host "Host": "localhost:5001",

Now the two configure content like this: Configuration.json: { "PasswordHash":"AQAAAAEAACcQAAAAENwtffADuTsyx+2jbPM68vx2063D+zDA+WkMdM1XAdQZW39z76YDQ/ssBQVFpXh8Bw==", "IsLocked":false, "IsAdmin":false, "Channels": { "user@wirehome.com": { "IsDefault":false, "AccessToken": { "Value":"vRjriJ8pVhLLgbWcYWvN2jjTSWszZXBspjp1EpAobAdgjRzVh7BD6nSt3HVEgqYBYHPfZmup7PtCNoKA619WsQ==", "ValidUntil":"2020-01-29T13:49:54.5060802Z" }, "AllowedIdentities":{} } } } CloudServiceConfiguration.json: { "IsEnabled": true, "Host": "localhost:5001", "IdentityUid":"user@wirehome.com", "Password": "starter123456!", "ChannelUid": "user@wirehome.com", "Logger":"info", "ChannelAccessToken": "vRjriJ8pVhLLgbWcYWvN2jjTSWszZXBspjp1EpAobAdgjRzVh7BD6nSt3HVEgqYBYHPfZmup7PtCNoKA619WsQ==", "ReconnectDelay": "00:00:10", "UseCompression": true }

So , now the problem changes form"Device not connect " to "blank screen", @chkr1011 ☹

In Swagger, App module has follow apis. ​/api​/v1​/app​/package_uid ​/api​/v1​/app​/access_type ​/api​/v1​/app​/panels ​/api​/v1​/app​/status

In Cloud , i can see AppController only support router: api/v1/app/access_type

Dose it matter ? Thanks

chkr1011 commented 4 years ago

That is a step forward 😄 For me it works perfectly.

Why do you need the use identity etc? These are no longer used in code so they can be removed from the service configuration (not entity).

Where is ""Logger":"info"" coming from? Did you add something on your own? Please make sure that you are using the latest master branch.

The app service and controller is OK so far. Only one API is "overridden". But let me explain this after the other stuff works 😄

Do you have the app package installed? I mean can you open the app (.../app/) when not using the cloud connection?

masterchen commented 4 years ago

OK, i use the same config as repo, it works fine except "blank page" problem.

I can't open the app with or without cloud connection. I havne't installed app yet 😟

I read the questions https://github.com/chkr1011/Wirehome.Core/issues/2, but swagger page doesn't have "Repository" area.

Also, wiki said i need put Wirehome.App to the directory WebApp in the bin directory, i can't find which patch WebApp locates , I use visual studio debug mode under window10 system.

Hope for more information 😊

chkr1011 commented 4 years ago

OK that is perfect. So it seems to work now. When you can see the swagger definition it works perfectly. The cloud is just an addon. So we still have to setup the "big" things like components and component groups.

You can prepare the app etc. by executing the setup API from the system API group (directly from swagger). It will automatically download the latest APP version from GitHub to your installation. The Wiki is a little bit outdated. So please try to ignore this for now 😄

After this is done you can try to open http://whatever/app/ using the address from your pi or localhost. I should open the app. After that you can also try to open the app by using the cloud service. The uri remains the same.

masterchen commented 4 years ago

@chkr1011 👍👍👍 Opened App link and configure link , bbth are like the picture below. i already download Apps : wirehome.app wirehome.configurator

image

chkr1011 commented 4 years ago

Ah that is perfect. Now you have to add components and component groups etc. Do you have some hardware? Then I can support you in creating these files.

masterchen commented 4 years ago

Yes , I have a Pi 4B here, and Led 、Buzzer module and a modbus module can connect to it

chkr1011 commented 4 years ago

Ah OK. You can try to prepare everything then I can tell you how to configure it in wirehome. Basically you need to create a new component which uses the logical lamp package and the gpio adapter. With both packages you can integrate the LED. But you have to download the packages first using the package manager API.