Add hapi-auth-basic to enable fixing nes properly

This commit is contained in:
Alexander James Phillips
2017-05-08 00:41:29 +01:00
parent 92c0981ea3
commit 97ff039dc5
3 changed files with 83 additions and 0 deletions
@@ -0,0 +1,40 @@
import Bcrypt = require('bcrypt');
import Basic = require('hapi-auth-basic');
import * as Hapi from 'hapi';
const server = new Hapi.Server();
interface User {
username: string;
password: string;
name: string;
id: string;
}
const users: {[index: string]: User} = {
john: {
username: 'john',
password: '$2a$10$iqJSHD.BGr0E2IxQwYgJmeP3NvhPrXAeLSaGCj6IR/XU5QtjVu5Tm', // 'secret'
name: 'John Doe',
id: '2133d32a'
}
};
const validate: Basic.ValidateFunc = function (request, username, password, callback) {
const user = users[username];
if (!user) {
return callback(null, false);
}
Bcrypt.compare(password, user.password, (err, isValid) => {
callback(err, isValid, { id: user.id, name: user.name });
});
};
server.register(Basic, (err) => {
server.auth.strategy('simple', 'basic', { validateFunc: validate });
server.route({ method: 'GET', path: '/', config: { auth: 'simple' } });
});
+21
View File
@@ -0,0 +1,21 @@
// Type definitions for hapi 4.2
// Project: https://github.com/hapijs/hapi-auth-basic
// Definitions by: AJP <https://github.com/AJamesPhillips>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2
import * as Hapi from 'hapi';
declare namespace Basic {
interface ValidateFuncCallback {
(err: Error | null, isValid: boolean, userCredentials?: any): void;
}
interface ValidateFunc {
(request: Hapi.Request, username: string, password: string, callback: ValidateFuncCallback): void;
}
}
declare var Basic: Hapi.PluginFunction<{}>;
export = Basic;
+22
View File
@@ -0,0 +1,22 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"hapi-auth-basic-tests"
]
}