Skip to content

Commit

Permalink
Merge pull request #1 from matthewglover/clean-up
Browse files Browse the repository at this point in the history
Clean up
  • Loading branch information
matthewglover committed Oct 3, 2016
2 parents 8829650 + 9e15732 commit d15a53c
Show file tree
Hide file tree
Showing 13 changed files with 138 additions and 46 deletions.
10 changes: 10 additions & 0 deletions integration/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "airbnb",
"parser": "babel-eslint",
"env": {
"node": true
},
"rules": {
"import/no-extraneous-dependencies": ["error", {"devDependencies": true}]
}
}
24 changes: 24 additions & 0 deletions integration/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { createServer, setConnection, registerPlugins, addRoutes, startServer } =
require('@matthewglover/hapi-wrapper');
const oauthPlugin = require('./oauth_plugin');
const jwtPlugin = require('./jwt_plugin');

const protectedRoute = {
method: 'GET',
path: '/protected',
config: { auth: 'jwt' },
handler: (req, reply) => reply('boom'),
};

const server =
createServer()
.then(setConnection({ port: 3000 }))
.then(registerPlugins([oauthPlugin, jwtPlugin]))
.then(addRoutes([protectedRoute]))
.then(startServer)
/* eslint-disable no-console */
.then(s => console.log(`Server running at: ${s.info.uri}`))
.catch(err => console.error(err));
/* eslint-enable */

module.exports = server;
21 changes: 21 additions & 0 deletions integration/jwt_plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require('env2')('./config.env');
const hapiJwt = require('../');

/* eslint-disable max-len */
const options = {
strategyName: 'jwt', // Name of strategy (defaults to jwt)

createTokenPath: '/create-token', // Path for token creation
prepareTokenData: req => req.query, // Function to prepare token payload data

issueTokenPath: '/issue-token.html', // Path which will issue token (as /issue-token.html?jwt=[token])

verifyTokenPath: '/verify-token', // Path which will verify token (as /verify-token?jwt=[token])

jwtOptions: { algorithm: 'HS256' }, // jwt creation options (as per jsonwebtoken.sign)
jwtVerificationOptions: { algorithm: 'HS256' }, // jwt verification options (as per jsonwebtoken.verify)
jwtSecret: process.env.JWT_SECRET, // secret for creating token
};
/* eslint-enable max-len */

module.exports = { register: hapiJwt, options };
21 changes: 21 additions & 0 deletions integration/oauth_plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require('env2')('./config.env');
const hapiOauth = require('@matthewglover/hapi-oauth');

const options = {
configs: [
{
provider: 'facebook',
loginPath: '/fb-login',
authPath: '/fb-auth',
redirectPath: '/create-token',
baseUrl: process.env.BASE_URL,
clientId: process.env.FB_CLIENT_ID,
clientSecret: process.env.FB_CLIENT_SECRET,
options: {
scope: 'user_likes',
},
},
],
};

module.exports = { register: hapiOauth, options };
13 changes: 13 additions & 0 deletions integration/public/issue-token.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>

<script type="text/javascript">

</script>
</body>
</html>
2 changes: 1 addition & 1 deletion lib/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const jwtScheme = require('./jwt_scheme');
const register = (server, opts, next) => {
server.route([createTokenRoute(opts), verifyTokenRoute(opts)]);
server.auth.scheme('jwtScheme', jwtScheme);
server.auth.strategy(opts.strategyName || 'jwt', 'jwtScheme');
server.auth.strategy(opts.strategyName || 'jwt', 'jwtScheme', opts);
next();
};

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"ava": "^0.16.0",
"babel-eslint": "^6.1.2",
"coveralls": "^2.11.12",
"env2": "^2.1.1",
"eslint": "^3.4.0",
"eslint-config-airbnb": "^11.0.0",
"eslint-plugin-import": "^1.14.0",
Expand Down
46 changes: 46 additions & 0 deletions public/issue-token.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<a id="verifyTokenLink" href="#">verify token</a><br><br>
<a id="accessProtectedLinkFail" href="#">access protected without jwt</a><br><br>
<a id="accessProtectedLinkSuccess" href="#">access protected with jwt</a>

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script type="text/javascript">
const params =
window.location.search
.match(/[^?&]+/g)
.map(s => s.split('='))
.reduce((acc, [k, v]) => Object.assign(acc, { [k]: v }), {});

document.getElementById('verifyTokenLink').addEventListener('click', () => {
axios
.get(`/verify-token?jwt=${params.jwt}`)
.then(({ data }) => data)
.then(console.log.bind(console));
});

document.getElementById('accessProtectedLinkFail').addEventListener('click', () => {
axios
.get(`/protected`)
.then(({ data }) => data)
.then(data => console.log('succes:', data))
.catch(err => console.log('error:', err));
});

document.getElementById('accessProtectedLinkSuccess').addEventListener('click', () => {
axios
.get(`/protected`, { headers: { Authorization: `Bearer ${params.jwt}` } })
.then(({ data }) => data)
.then(data => console.log('succes:', data))
.catch(err => console.log('error:', err));
});


</script>
</body>
</html>
4 changes: 1 addition & 3 deletions test/lib/jwt_scheme/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ const failingCredentialsOptions =
testOptions,
{ validateCredentials: () => Promise.reject(new Error('Invalid credentials')) });

const testCredentials = {
name: 'matt',
};
const testCredentials = { name: 'matt' };

test('jwtScheme allows access if valid token provided', async t => {
const validToken =
Expand Down
12 changes: 0 additions & 12 deletions test/util/then.test.js

This file was deleted.

16 changes: 0 additions & 16 deletions test/util/then_catch.test.js

This file was deleted.

7 changes: 0 additions & 7 deletions util/then.js

This file was deleted.

7 changes: 0 additions & 7 deletions util/then_catch.js

This file was deleted.

0 comments on commit d15a53c

Please sign in to comment.