Skip to content
This repository has been archived by the owner on Aug 12, 2022. It is now read-only.

Commit

Permalink
chore: simplify session logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Burry committed Jan 24, 2022
1 parent f28602d commit f38ef1d
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 213 deletions.
2 changes: 1 addition & 1 deletion homebridge-ui/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ <h5 class="card-title">Configuration</h5>
homebridge.toast.success('Login successful', 'Success!');
await homebridge.savePluginConfig(pluginConfig);
logoutButton.removeAttribute('disabled');
} else if (response.code === 401) {
} else if (response.code === 401 || response.code === 403) {
homebridge.toast.error(response.message, 'Error!');
} else {
homebridge.toast.error('Unknown response', 'Error!');
Expand Down
40 changes: 13 additions & 27 deletions homebridge-ui/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ class PluginUiServer extends HomebridgePluginUiServer {
constructor() {
super();

this.pluginPath = `${this.homebridgeStoragePath}/smartrent`;
this.sessionPath = `${this.pluginPath}/session.json`;
this.sessionPath = `${this.homebridgeStoragePath}/smartrent/session.json`;

this.onRequest('/session', this.checkSession.bind(this));
this.onRequest('/logout', this.clearSession.bind(this));
Expand Down Expand Up @@ -48,7 +47,6 @@ class PluginUiServer extends HomebridgePluginUiServer {

async login(payload) {
try {
const authClient = new SmartRentAuthClient();
const { email, password, tfaCode } = payload;
if (!email) {
console.error('Email required');
Expand All @@ -58,34 +56,22 @@ class PluginUiServer extends HomebridgePluginUiServer {
console.error('Password required');
return { code: 401, message: 'Password required' };
}
let sessionData = await authClient.getSession({
username: email,
const authClient = new SmartRentAuthClient(this.homebridgeStoragePath);
const accessToken = await authClient.getAccessToken({
email,
password,
tfaCode,
});
if (SmartRentAuthClient.isTfaSession(sessionData)) {
if (!tfaCode) {
console.error('2FA code required');
return { code: 401, message: '2FA code required' };
}
sessionData = await authClient.getTfaSession({
tfa_api_token: sessionData.tfa_api_token,
token: tfaCode,
});
}
if (sessionData && sessionData.accessToken) {
if (!fs.existsSync(this.pluginPath)) {
await fsPromises.mkdir(this.pluginPath);
}
await fsPromises.writeFile(
this.sessionPath,
JSON.stringify(sessionData, null, 2)
);
console.info('Saved session to', this.sessionPath);
if (accessToken) {
return { code: 200 };
}
throw new RequestError('Failed to login to SmartRent', {
message: 'No access token returned',
});
if (authClient.isTfaSession) {
return {
code: 403,
message: tfaCode ? 'Invalid 2FA code' : '2FA code required',
};
}
return { code: 403, message: 'Invalid email or password' };
} catch (error) {
throw new RequestError('Failed to login to SmartRent', {
message: error.message,
Expand Down
Loading

0 comments on commit f38ef1d

Please sign in to comment.