diff --git a/types/hapi-auth-basic/hapi-auth-basic-tests.ts b/types/hapi-auth-basic/hapi-auth-basic-tests.ts new file mode 100644 index 0000000000..13dd159377 --- /dev/null +++ b/types/hapi-auth-basic/hapi-auth-basic-tests.ts @@ -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' } }); +}); diff --git a/types/hapi-auth-basic/index.d.ts b/types/hapi-auth-basic/index.d.ts new file mode 100644 index 0000000000..23b06de111 --- /dev/null +++ b/types/hapi-auth-basic/index.d.ts @@ -0,0 +1,21 @@ +// Type definitions for hapi 4.2 +// Project: https://github.com/hapijs/hapi-auth-basic +// Definitions by: AJP +// 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; diff --git a/types/hapi-auth-basic/tsconfig.json b/types/hapi-auth-basic/tsconfig.json new file mode 100644 index 0000000000..de57117fd5 --- /dev/null +++ b/types/hapi-auth-basic/tsconfig.json @@ -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" + ] +}