-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This release adds support for the new Hilo authentication flow. You will need to reconfigure your plugin in Homebridge UI to retrieve your refresh token for authentication with Hilo
- Loading branch information
1 parent
dcbb0fa
commit 616ac78
Showing
11 changed files
with
1,115 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<div class="card"> | ||
<ol> | ||
<li>You will need to know the IP address or the domain name you use to connect to homebridge. Usually it will be the address in your browser</li> | ||
<li>Click on the button below to start the authentication process</li> | ||
<li>Login to your hilo account</li> | ||
<li>Enter the ip address or domain name you took not at step 1 and add the port 8880 at the end. Examples: http://192.168.0.10:8880 http://homebridge.local:8880 | ||
<ul> | ||
<li>Note: Depending on how you have configured your homebridge server, you will need to ensure that port 8880 is routed to your homebridge server. For example for a docker configuration you will need to add `-p 8880:8880` to your docker command. If you are using a reverse proxy, you will need to add port 8880 to your configuration</li> | ||
</ul> | ||
</li> | ||
<li>Click on the "Save" button, then click on the "Link account" button</li> | ||
<li>You can save your config and restart your homebridge server</li> | ||
</ol> | ||
<button type="button" class="btn btn-primary" onclick="initiateAuth()"> | ||
Login with Hilo | ||
</button> | ||
</div> | ||
<script> | ||
homebridge.showSchemaForm(); | ||
async function initiateAuth() { | ||
const { authorizationURL } = await homebridge.request('/autorizationUrl'); | ||
const callback = homebridge.request('/callback'); | ||
|
||
window.open(authorizationURL, "_blank"); | ||
|
||
const { accessToken, refreshToken, expiresIn } = await callback; | ||
let pluginConfigBlocks = await homebridge.getPluginConfig(); | ||
pluginConfigBlocks = pluginConfigBlocks.map((block) => { | ||
block.refreshToken = refreshToken; | ||
return block; | ||
}); | ||
await homebridge.updatePluginConfig(pluginConfigBlocks); | ||
await homebridge.savePluginConfig(); | ||
homebridge.toast.success("Successfully authenticated with Hilo"); | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
const { HomebridgePluginUiServer } = require("@homebridge/plugin-ui-utils"); | ||
const express = require("express"); | ||
const crypto = require("crypto"); | ||
|
||
const port = 8880; | ||
|
||
class UiServer extends HomebridgePluginUiServer { | ||
constructor() { | ||
super(); | ||
this.verifier = this.base64URLEncode(crypto.randomBytes(32)); | ||
this.app = express(); | ||
this.app.listen(port, () => { | ||
console.log(`Auth server listening on port ${port}`); | ||
}); | ||
this.onRequest("/callback", this.handleCallback.bind(this)); | ||
this.onRequest("/autorizationUrl", this.handleAuthorizationUrl.bind(this)); | ||
this.ready(); | ||
} | ||
|
||
async handleAuthorizationUrl() { | ||
const challenge = this.base64URLEncode(this.sha256(this.verifier)); | ||
const clientId = "1ca9f585-4a55-4085-8e30-9746a65fa561"; | ||
const redirectUri = "https://my.home-assistant.io/redirect/oauth"; | ||
const scope = [ | ||
"openid", | ||
"https://HiloDirectoryB2C.onmicrosoft.com/hiloapis/user_impersonation", | ||
"offline_access", | ||
].join(" "); | ||
const state = Math.random().toString(36).substring(7); | ||
const authorizationURL = `https://connexion.hiloenergie.com/HiloDirectoryB2C.onmicrosoft.com/B2C_1A_SIGN_IN/oauth2/v2.0/authorize?response_type=code&code_challenge=${challenge}&code_challenge_method=S256&client_id=${clientId}&redirect_uri=${redirectUri}&scope=${scope}&state=${state}`; | ||
return { | ||
authorizationURL, | ||
}; | ||
} | ||
|
||
base64URLEncode(str) { | ||
return str | ||
.toString("base64") | ||
.replace(/\+/g, "-") | ||
.replace(/\//g, "_") | ||
.replace(/=/g, ""); | ||
} | ||
|
||
sha256(buffer) { | ||
return crypto.createHash("sha256").update(buffer).digest(); | ||
} | ||
|
||
async handleCallback() { | ||
return new Promise((resolve) => { | ||
this.app.get("/auth/external/callback", async (req, res) => { | ||
const code = req.query.code; | ||
const tokenResponse = await fetch( | ||
"https://connexion.hiloenergie.com/hilodirectoryb2c.onmicrosoft.com/oauth2/v2.0/token?p=b2c_1a_sign_in", | ||
{ | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
}, | ||
body: new URLSearchParams({ | ||
client_id: "1ca9f585-4a55-4085-8e30-9746a65fa561", | ||
code, | ||
code_verifier: this.verifier, | ||
grant_type: "authorization_code", | ||
redirect_uri: "https://my.home-assistant.io/redirect/oauth", | ||
}), | ||
} | ||
); | ||
const body = await tokenResponse.json(); | ||
const refreshToken = body.refresh_token; | ||
res.send("Success - you can now close this window"); | ||
resolve({ | ||
refreshToken, | ||
}); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
(() => new UiServer())(); |
Oops, something went wrong.