Skip to content

Commit

Permalink
reolink: fallback if token exchange fails
Browse files Browse the repository at this point in the history
  • Loading branch information
koush committed Jul 6, 2024
1 parent b7bf995 commit f5c324b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 34 deletions.
4 changes: 2 additions & 2 deletions plugins/reolink/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/reolink/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@scrypted/reolink",
"version": "0.0.81",
"version": "0.0.82",
"description": "Reolink Plugin for Scrypted",
"author": "Scrypted",
"license": "Apache",
Expand Down
75 changes: 44 additions & 31 deletions plugins/reolink/src/probe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,38 +60,51 @@ export async function getLoginParameters(host: string, username: string, passwor
catch (e) {
}

const url = new URL(`http://${host}/api.cgi`);
const params = url.searchParams;
params.set('cmd', 'Login');

const response = await httpFetch({
url,
method: 'POST',
responseType: 'json',
rejectUnauthorized: false,
body: [
{
cmd: 'Login',
action: 0,
param: {
User: {
userName: username,
password: password
try {
const url = new URL(`http://${host}/api.cgi`);
const params = url.searchParams;
params.set('cmd', 'Login');

const response = await httpFetch({
url,
method: 'POST',
responseType: 'json',
rejectUnauthorized: false,
body: [
{
cmd: 'Login',
action: 0,
param: {
User: {
userName: username,
password: password
}
}
}
},
],
});

const token = response.body?.[0]?.value?.Token?.name || response.body?.value?.Token?.name;
if (!token)
throw new Error('unable to login');
const { body } = response;
const leaseTimeSeconds: number = body?.[0]?.value?.Token.leaseTime || body?.value?.Token.leaseTime;
return {
parameters: {
token,
},
],
});

const token = response.body?.[0]?.value?.Token?.name || response.body?.value?.Token?.name;
if (!token)
throw new Error('unable to login');
const { body } = response;
const leaseTimeSeconds: number = body?.[0]?.value?.Token.leaseTime || body?.value?.Token.leaseTime;
return {
parameters: {
token,
},
leaseTimeSeconds,
leaseTimeSeconds,
}
}
catch (e) {
// if the token exchange fails, fall back to basic auth
// TODO: maybe detect error type?
return {
parameters: {
user: username,
password,
},
leaseTimeSeconds: 60,
}
}
}

0 comments on commit f5c324b

Please sign in to comment.