solid-auth-tls
Advanced tools
Comparing version
151
lib/auth.js
@@ -0,14 +1,22 @@ | ||
'use strict'; | ||
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }(); | ||
/* global XMLHttpRequest */ | ||
var defaultConfig = require('./config-default') | ||
var defaultConfig = require('./config-default'); | ||
/** | ||
* Returns whether or not the current JS engine is in a browser environment. The | ||
* environment is assumed to be Node.js if this returns false. Heuristically | ||
* just looks for 'window' and 'XMLHttpRequest'. | ||
* @returns {Boolean} true if the environment is a browser, false otherwise | ||
* Note: | ||
* | ||
* global.IS_BROWSER is a global made available by a webpack plugin. | ||
* If you're using this library in the browser you must include the folowing | ||
* configuration in your webpack config (or equivalent): | ||
* module.exports = { | ||
* ..., | ||
* plugins: [ | ||
* new webpack.DefinePlugin({ global.IS_BROWSER: true }), | ||
* ] | ||
* } | ||
*/ | ||
function isBrowser () { | ||
return typeof window !== 'undefined' && typeof XMLHttpRequest !== 'undefined' | ||
} | ||
@@ -20,6 +28,6 @@ /** | ||
*/ | ||
const AUTH_ENDPOINTS = { | ||
var AUTH_ENDPOINTS = { | ||
PRIMARY: 0, | ||
SECONDARY: 1 | ||
} | ||
}; | ||
@@ -39,6 +47,7 @@ /** | ||
*/ | ||
function login (config) { | ||
config = Object.assign({}, defaultConfig, config) | ||
return loginTo(AUTH_ENDPOINTS.PRIMARY, config) | ||
.then(webId => webId || loginTo(AUTH_ENDPOINTS.SECONDARY, config)) | ||
function login(config) { | ||
config = Object.assign({}, defaultConfig, config); | ||
return loginTo(AUTH_ENDPOINTS.PRIMARY, config).then(function (webId) { | ||
return webId || loginTo(AUTH_ENDPOINTS.SECONDARY, config); | ||
}); | ||
} | ||
@@ -49,3 +58,3 @@ | ||
*/ | ||
const currentUser = login | ||
var currentUser = login; | ||
@@ -59,6 +68,4 @@ /** | ||
*/ | ||
function loginTo (endpoint, config) { | ||
return isBrowser() | ||
? loginFromBrowser(endpoint, config) | ||
: loginFromNode(endpoint, config) | ||
function loginTo(endpoint, config) { | ||
return global.IS_BROWSER ? loginFromBrowser(endpoint, config) : loginFromNode(endpoint, config); | ||
} | ||
@@ -73,26 +80,26 @@ | ||
*/ | ||
function loginFromBrowser (endpoint, config) { | ||
let uri | ||
function loginFromBrowser(endpoint, config) { | ||
var uri = void 0; | ||
switch (endpoint) { | ||
case AUTH_ENDPOINTS.PRIMARY: | ||
uri = config.authEndpoint || window.location.origin + window.location.pathname | ||
break | ||
uri = config.authEndpoint || window.location.origin + window.location.pathname; | ||
break; | ||
case AUTH_ENDPOINTS.SECONDARY: | ||
uri = config.fallbackAuthEndpoint | ||
break | ||
uri = config.fallbackAuthEndpoint; | ||
break; | ||
} | ||
return new Promise((resolve, reject) => { | ||
const xhr = new XMLHttpRequest() | ||
xhr.withCredentials = true | ||
xhr.addEventListener('load', () => { | ||
resolve(xhr.getResponseHeader('user') || null) | ||
}) | ||
xhr.addEventListener('error', err => { | ||
reject(err) | ||
}) | ||
xhr.open('HEAD', uri) | ||
xhr.send() | ||
}) | ||
return new Promise(function (resolve, reject) { | ||
var xhr = new XMLHttpRequest(); | ||
xhr.withCredentials = true; | ||
xhr.addEventListener('load', function () { | ||
resolve(xhr.getResponseHeader('user') || null); | ||
}); | ||
xhr.addEventListener('error', function (err) { | ||
reject(err); | ||
}); | ||
xhr.open('HEAD', uri); | ||
xhr.send(); | ||
}); | ||
} | ||
@@ -107,29 +114,41 @@ | ||
*/ | ||
function loginFromNode (endpoint, config) { | ||
function loginFromNode(endpoint, config) { | ||
if (!(config.key && config.cert)) { | ||
throw new Error('Must provide TLS key and cert when running in node') | ||
throw new Error('Must provide TLS key and cert when running in node'); | ||
} | ||
let uri | ||
var uri = void 0; | ||
switch (endpoint) { | ||
case AUTH_ENDPOINTS.PRIMARY: | ||
uri = config.authEndpoint | ||
break | ||
uri = config.authEndpoint; | ||
break; | ||
case AUTH_ENDPOINTS.SECONDARY: | ||
uri = config.fallbackAuthEndpoint | ||
break | ||
uri = config.fallbackAuthEndpoint; | ||
break; | ||
} | ||
const fs = require('fs') | ||
const https = require('https') | ||
const url = require('url') | ||
var fs = void 0, | ||
https = void 0, | ||
url = void 0; | ||
return Promise.all([config.key, config.cert].map(filename => | ||
new Promise((resolve, reject) => { | ||
fs.readFile(filename, (err, data) => err ? reject(err) : resolve(data)) | ||
}) | ||
)).then(([keyBuf, certBuf]) => { | ||
const parsedUrl = url.parse(uri) | ||
const options = { | ||
if (!global.IS_BROWSER) { | ||
fs = require('fs'); | ||
https = require('https'); | ||
url = require('url'); | ||
} | ||
return Promise.all([config.key, config.cert].map(function (filename) { | ||
return new Promise(function (resolve, reject) { | ||
fs.readFile(filename, function (err, data) { | ||
return err ? reject(err) : resolve(data); | ||
}); | ||
}); | ||
})).then(function (_ref) { | ||
var _ref2 = _slicedToArray(_ref, 2), | ||
keyBuf = _ref2[0], | ||
certBuf = _ref2[1]; | ||
var parsedUrl = url.parse(uri); | ||
var options = { | ||
method: 'HEAD', | ||
@@ -142,16 +161,16 @@ hostname: parsedUrl.hostname, | ||
cert: certBuf | ||
} | ||
return new Promise((resolve, reject) => { | ||
const req = https.request(options, res => { | ||
resolve(res.headers['user'] || null) | ||
}) | ||
req.on('error', reject) | ||
req.end() | ||
}) | ||
}) | ||
}; | ||
return new Promise(function (resolve, reject) { | ||
var req = https.request(options, function (res) { | ||
resolve(res.headers['user'] || null); | ||
}); | ||
req.on('error', reject); | ||
req.end(); | ||
}); | ||
}); | ||
} | ||
module.exports = { | ||
currentUser, | ||
login | ||
} | ||
currentUser: currentUser, | ||
login: login | ||
}; |
@@ -1,2 +0,2 @@ | ||
'use strict' | ||
'use strict'; | ||
/** | ||
@@ -7,2 +7,3 @@ * Provides a simple configuration object for Solid web client and other | ||
*/ | ||
module.exports = { | ||
@@ -43,2 +44,2 @@ /** | ||
cert: '' | ||
} | ||
}; |
@@ -31,3 +31,3 @@ /* | ||
*/ | ||
'use strict' | ||
'use strict'; | ||
@@ -40,3 +40,3 @@ /** | ||
const auth = require('./auth') | ||
var auth = require('./auth'); | ||
@@ -47,2 +47,2 @@ module.exports = { | ||
currentUser: auth.currentUser | ||
} | ||
}; |
@@ -1,3 +0,5 @@ | ||
var defaultConfig = require('./config-default') | ||
'use strict'; | ||
var defaultConfig = require('./config-default'); | ||
/** | ||
@@ -12,4 +14,4 @@ * Provides functionality for signing up with a Solid provider | ||
*/ | ||
function Signup (config) { | ||
this.config = config || defaultConfig | ||
function Signup(config) { | ||
this.config = config || defaultConfig; | ||
} | ||
@@ -22,25 +24,21 @@ | ||
*/ | ||
Signup.prototype.listen = function listen () { | ||
Signup.prototype.listen = function listen() { | ||
var promise = new Promise(function (resolve, reject) { | ||
var eventMethod = window.addEventListener | ||
? 'addEventListener' | ||
: 'attachEvent' | ||
var eventListener = window[eventMethod] | ||
var messageEvent = eventMethod === 'attachEvent' | ||
? 'onmessage' | ||
: 'message' | ||
var eventMethod = window.addEventListener ? 'addEventListener' : 'attachEvent'; | ||
var eventListener = window[eventMethod]; | ||
var messageEvent = eventMethod === 'attachEvent' ? 'onmessage' : 'message'; | ||
eventListener(messageEvent, function (e) { | ||
var u = e.data | ||
var u = e.data; | ||
if (u.slice(0, 5) === 'User:') { | ||
var user = u.slice(5, u.length) | ||
var user = u.slice(5, u.length); | ||
if (user && user.length > 0 && user.slice(0, 4) === 'http') { | ||
return resolve(user) | ||
return resolve(user); | ||
} else { | ||
return reject(user) | ||
return reject(user); | ||
} | ||
} | ||
}, true) | ||
}) | ||
return promise | ||
} | ||
}, true); | ||
}); | ||
return promise; | ||
}; | ||
@@ -55,25 +53,22 @@ /** | ||
*/ | ||
Signup.prototype.signup = function signup (signupUrl) { | ||
signupUrl = signupUrl || this.config.signupEndpoint | ||
var width = this.config.signupWindowWidth | ||
var height = this.config.signupWindowHeight | ||
Signup.prototype.signup = function signup(signupUrl) { | ||
signupUrl = signupUrl || this.config.signupEndpoint; | ||
var width = this.config.signupWindowWidth; | ||
var height = this.config.signupWindowHeight; | ||
// set borders | ||
var leftPosition = (window.screen.width / 2) - ((width / 2) + 10) | ||
var leftPosition = window.screen.width / 2 - (width / 2 + 10); | ||
// set title and status bars | ||
var topPosition = (window.screen.height / 2) - ((height / 2) + 50) | ||
var windowTitle = 'Solid signup' | ||
var windowUrl = signupUrl + '?origin=' + | ||
encodeURIComponent(window.location.origin) | ||
var windowSpecs = 'resizable,scrollbars,status,width=' + width + ',height=' + | ||
height + ',left=' + leftPosition + ',top=' + topPosition | ||
window.open(windowUrl, windowTitle, windowSpecs) | ||
var self = this | ||
var topPosition = window.screen.height / 2 - (height / 2 + 50); | ||
var windowTitle = 'Solid signup'; | ||
var windowUrl = signupUrl + '?origin=' + encodeURIComponent(window.location.origin); | ||
var windowSpecs = 'resizable,scrollbars,status,width=' + width + ',height=' + height + ',left=' + leftPosition + ',top=' + topPosition; | ||
window.open(windowUrl, windowTitle, windowSpecs); | ||
var self = this; | ||
return new Promise(function (resolve) { | ||
self.listen() | ||
.then(function (webid) { | ||
return resolve(webid) | ||
}) | ||
}) | ||
} | ||
self.listen().then(function (webid) { | ||
return resolve(webid); | ||
}); | ||
}); | ||
}; | ||
module.exports = Signup | ||
module.exports = Signup; |
{ | ||
"name": "solid-auth-tls", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"engines": { | ||
@@ -10,8 +10,11 @@ "node": "^6.0" | ||
"files": [ | ||
"lib" | ||
"lib", | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "npm run test", | ||
"prepublish": "npm run test", | ||
"standard": "standard lib/*.js test/*.js", | ||
"build:lib": "babel src -d lib", | ||
"build:umd": "webpack", | ||
"build": "npm run build:lib && npm run build:umd", | ||
"prepublish": "npm run test && npm run build", | ||
"standard": "standard src/*.js test/*.js", | ||
"pretest": "npm run standard", | ||
@@ -51,2 +54,6 @@ "test": "nyc --reporter=html mocha", | ||
"devDependencies": { | ||
"babel-cli": "^6.24.0", | ||
"babel-core": "^6.24.0", | ||
"babel-loader": "^6.4.1", | ||
"babel-preset-env": "^1.3.2", | ||
"chai": "^3.5.0", | ||
@@ -59,5 +66,5 @@ "coveralls": "^2.12.0", | ||
"sinon-chai": "^2.9.0", | ||
"solid-web-client": "0.0.6", | ||
"standard": "^5.4.1" | ||
"standard": "^5.4.1", | ||
"webpack": "^2.3.3" | ||
} | ||
} |
@@ -10,1 +10,46 @@ # solid-auth-tls | ||
[`solid-client`](https://github.com/solid/solid-client). | ||
## Usage | ||
The `solid-auth-tls` client can be used from Node as well as browser processes. | ||
### Within Node | ||
```js | ||
const auth = require('solid-auth-tls') | ||
auth.login({ key: 'path/to/tls-key.pem', cert: 'path/to/tls-cert.pem' }) | ||
.then(webId => /* ... */) | ||
.catch(err => /* ... */) | ||
``` | ||
### Within the Browser | ||
A UMD bundle is provided so that you can do the following (after including the | ||
bundle via an HTML `<script>` tag): | ||
```js | ||
SolidAuthTLS.login() // the browser automatically provides the client key/cert for you | ||
.then(webId => /* ... */) | ||
.catch(err => /* ... */) | ||
``` | ||
You can also use a module bundler such as webpack and require the module like in | ||
the node example. When using webpack you need to include the following | ||
plugin(s) in order to keep webpack from trying to resolve node modules such as | ||
`fs` and `https`. Add this to your `webpack.config.js`: | ||
```js | ||
const webpack = require('webpack') | ||
module.exports = { | ||
// ... | ||
plugins: [ | ||
new webpack.DefinePlugin({ 'global.IS_BROWSER': true }) | ||
new webpack.optimize.UglifyJsPlugin(/* ... */) | ||
] | ||
} | ||
``` | ||
This makes sure that code paths that require node modules (for use within node) | ||
become dead code and get removed by the `UglifyJsPlugin`'s dead code eliminator. |
17518
42.59%8
14.29%285
2.52%55
450%13
44.44%