domotz-remote-pawn
Version:
Domotz Agent
65 lines (55 loc) • 1.95 kB
JavaScript
/** This file is part of Domotz Agent.
* Copyright (C) 2016 Domotz Ltd
*
* Domotz Agent is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Domotz Agent is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>.
**/
/**
*
* Created by Iacopo Papalini <ipapalini@domotz.com> on 19/06/15.
*/
const urlRegExpWithCredentials = new RegExp('^http(s)?://([^:]+):([^@]+)@(.*?)/([^/]+)$', 'i');
const loopbackRegExp = new RegExp('^http(s)?://127.0.0', 'i');
const PROTOCOL = 1;
const USER = 2;
const PASSWORD = 3;
function parseUrl(url) {
var matches = url.match(urlRegExpWithCredentials);
var s = matches[PROTOCOL] !== undefined ? 's' : '';
return {
user: matches[USER],
password: matches[PASSWORD],
url: 'http' + s + '://' + matches[4] + '/' + matches[5],
};
}
function isLoopbackUrl(url) {
return null !== url.match(loopbackRegExp);
}
function extractLocation(response) {
return response.headers.location;
}
function getUsername(request) {
return request.query.username;
}
function getUserId(request) {
return request.query.user_id;
}
function getPassword(request) {
return request.headers['x-forward-password'];
}
module.exports.parseUrl = parseUrl;
module.exports.isLoopbackUrl = isLoopbackUrl;
module.exports.extractLocation = extractLocation;
module.exports.getUsername = getUsername;
module.exports.getPassword = getPassword;
module.exports.getUserId = getUserId;