Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow csrf to be updated on cookie refresh and add 401 error handling #148

Merged
merged 1 commit into from
Feb 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions alexa-remote.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ class AlexaRemote extends EventEmitter {

if (!this.cookie || typeof this.cookie !== 'string') return;
let ar = this.cookie.match(/csrf=([^;]+)/);
if (!ar || ar.length < 2) ar = this.cookie.match(/csrf=([^;]+)/);
if (!this.csrf && ar && ar.length >= 2) {
if (ar && ar.length >= 2) {
this.csrf = ar[1];
}
if (!this.csrf) {
Expand Down Expand Up @@ -757,6 +756,11 @@ class AlexaRemote extends EventEmitter {
httpsGetCall(path, callback, flags = {}) {

const handleResponse = (err, res, body) => {
if (!err && typeof res.statusCode === 'number' && res.statusCode == 401) {
this._options.logger && this._options.logger('Alexa-Remote: Response: 401 Unauthorized');
return callback(new Error('401 Unauthorized'), null);
}

if (err || !body) { // Method 'DELETE' may return HTTP STATUS 200 without body
this._options.logger && this._options.logger('Alexa-Remote: Response: No body');
return typeof res.statusCode === 'number' && res.statusCode >= 200 && res.statusCode < 300 ? callback(null, {'success': true}) : callback(new Error('no body'), null);
Expand Down