Skip to content
This repository has been archived by the owner on Jul 3, 2021. It is now read-only.

Commit

Permalink
Guest mode
Browse files Browse the repository at this point in the history
can login with new PlugAPI({ guest: true }) to login as a guest
  • Loading branch information
Dazzuh committed Dec 1, 2016
1 parent 8a36269 commit e00c8fa
Showing 1 changed file with 97 additions and 62 deletions.
159 changes: 97 additions & 62 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,12 @@ function getRoomState(callback) {
*/
function PlugAPI(authenticationData, callback) {

if (typeof authenticationData !== 'object') {
handleErrors.error('You must pass the authentication data into the PlugAPI object to connect correctly', callback);
if (typeof authenticationData !== 'object' || authenticationData === null) {
this.guest = true;

// handleErrors.error('You must pass the authentication data into the PlugAPI object to connect correctly', callback);
} else if (!authenticationData.hasOwnProperty('email') && !authenticationData.hasOwnProperty('email') && authenticationData.hasOwnProperty('guest')) {
this.guest = true;
} else {
if (typeof authenticationData.email !== 'string') {
handleErrors.error('Missing Login Email', callback);
Expand All @@ -520,29 +524,31 @@ function PlugAPI(authenticationData, callback) {

var cookieHash = '';

(function(crypto) {
var hashPriority = ['md5', 'sha1', 'sha128', 'sha256', 'sha512'];
var foundHash = '';
if (!this.guest) {
(function(crypto) {
var hashPriority = ['md5', 'sha1', 'sha128', 'sha256', 'sha512'];
var foundHash = '';

(function(hashes) {
for (var i in hashes) {
if (!hashes.hasOwnProperty(i)) continue;
var hash = hashes[i];
(function(hashes) {
for (var i in hashes) {
if (!hashes.hasOwnProperty(i)) continue;
var hash = hashes[i];

if (hashPriority.indexOf(hash) > -1) {
if (hashPriority.indexOf(hash) > hashPriority.indexOf(foundHash))
foundHash = hash;
if (hashPriority.indexOf(hash) > -1) {
if (hashPriority.indexOf(hash) > hashPriority.indexOf(foundHash))
foundHash = hash;
}
}
}
})(crypto.getHashes());
if (foundHash !== '') {
var hash = crypto.createHash(foundHash);
})(crypto.getHashes());
if (foundHash !== '') {
var hash = crypto.createHash(foundHash);

hash.update(authenticationData.email);
hash.update(authenticationData.password);
cookieHash = hash.digest('hex');
}
})(require('crypto'));
hash.update(authenticationData.email);
hash.update(authenticationData.password);
cookieHash = hash.digest('hex');
}
})(require('crypto'));
}
_cookies = new CookieHandler(cookieHash);
authenticationInfo = authenticationData;
performLoginCredentials(callback);
Expand Down Expand Up @@ -748,11 +754,14 @@ function queueConnectSocket(roomSlug) {

/**
* Get the auth code for the user
* @param {string} roomSlug room for guests to get auth code from
* @param {Function} callback Callback function
* @private
*/
function getAuthCode(callback) {
request('/', {
function getAuthCode(roomSlug, callback) {
var url = roomSlug !== null ? roomSlug : '/';

request(url, {
headers: {
'User-Agent': 'plugAPI_' + PlugAPIInfo.version,
Cookie: _cookies.toString()
Expand Down Expand Up @@ -783,9 +792,15 @@ function getAuthCode(callback) {
*/
function connectSocket(roomSlug) {
if (_authCode === null || _authCode === '') {
getAuthCode(function() {
connectSocket(roomSlug);
});
if (that.guest) {
getAuthCode(roomSlug, function() {
connectSocket(roomSlug);
});
} else {
getAuthCode(null, function() {
connectSocket(roomSlug);
});
}
return;
}

Expand Down Expand Up @@ -1111,52 +1126,72 @@ function performLoginCredentials(callback) {
*/
var loggingIn = true;

request.get({
uri: '/',
headers: {
'User-Agent': 'plugAPI_' + PlugAPIInfo.version,
Cookie: _cookies.toString()
}
}, function(err, res, body) {
var csrfToken;

if (res && res.statusCode !== 200 || err) {
handleErrors.error('Login Error: ' + (res && res.statusCode !== 200 && ' HTTP Status: ' + res.statusCode + ' - ' + http.STATUS_CODES[res.statusCode] || '') + ' ' + (err && err || ''), callback);
}
try {
_cookies.fromHeaders(res.headers);
csrfToken = body.split('_csrf')[1].split('"')[1];
} catch (e) {
handleErrors.error("Login Error: Can't Obtain CSRF Token. " + e.message, callback);
}
request({
method: 'POST',
uri: '/_/auth/login',
if (that.guest) {
request.get({
uri: '/plugcubed',
headers: {
'User-Agent': 'plugAPI_' + PlugAPIInfo.version,
Cookie: _cookies.toString()
},
json: {
csrf: csrfToken,
email: authenticationInfo.email,
password: authenticationInfo.password
}
}, function(error, response, data) {
if (!error) {
if (data && data.status !== 'ok' || response && response.statusCode !== 200 || error) {
handleErrors.error('Login Error: ' + (data && data.status !== 'ok' && 'API Status: ' + data.status || '') + ' ' + (response && response.statusCode !== 200 && 'HTTP Status: ' + response.statusCode + ' - ' + http.STATUS_CODES[response.statusCode] || '') + ' ' + (error && error || ''), callback);
} else {
_cookies.fromHeaders(response.headers);
loggingIn = false;
if (typeof callback === 'function') {
return callback(err, that);
}
_cookies.fromHeaders(response.headers);
loggingIn = false;
if (typeof callback === 'function') {
return callback(error, that);
}
} else if (typeof callback === 'function') {
return callback(err, null);
return callback(error, null);
}
});
});
} else {
request.get({
uri: '/',
headers: {
'User-Agent': 'plugAPI_' + PlugAPIInfo.version,
Cookie: _cookies.toString()
}
}, function(err, res, body) {
var csrfToken;

if (res && res.statusCode !== 200 || err) {
handleErrors.error('Login Error: ' + (res && res.statusCode !== 200 && ' HTTP Status: ' + res.statusCode + ' - ' + http.STATUS_CODES[res.statusCode] || '') + ' ' + (err && err || ''), callback);
}
try {
_cookies.fromHeaders(res.headers);
csrfToken = body.split('_csrf')[1].split('"')[1];
} catch (e) {
handleErrors.error("Login Error: Can't Obtain CSRF Token. " + e.message, callback);
}
request({
method: 'POST',
uri: '/_/auth/login',
headers: {
'User-Agent': 'plugAPI_' + PlugAPIInfo.version,
Cookie: _cookies.toString()
},
json: {
csrf: csrfToken,
email: authenticationInfo.email,
password: authenticationInfo.password
}
}, function(error, response, data) {
if (!error) {
if (data && data.status !== 'ok' || response && response.statusCode !== 200 || error) {
handleErrors.error('Login Error: ' + (data && data.status !== 'ok' && 'API Status: ' + data.status || '') + ' ' + (response && response.statusCode !== 200 && 'HTTP Status: ' + response.statusCode + ' - ' + http.STATUS_CODES[response.statusCode] || '') + ' ' + (error && error || ''), callback);
} else {
_cookies.fromHeaders(response.headers);
loggingIn = false;
if (typeof callback === 'function') {
return callback(err, that);
}
}
} else if (typeof callback === 'function') {
return callback(err, null);
}
});
});
}

if (typeof callback !== 'function') {
var deasync = require('deasync');
Expand Down

0 comments on commit e00c8fa

Please sign in to comment.