From 5b221ee794c64ee920db8e9b21b5c677faf1f715 Mon Sep 17 00:00:00 2001 From: Simon Schick Date: Sat, 24 Nov 2018 09:30:07 -0800 Subject: [PATCH] feat(hapi): 17.8 --- types/hapi/index.d.ts | 56 +++++++++++++++++--- types/hapi/test/server/server-auth-api.ts | 12 +++-- types/hapi/test/server/server-auth-test.ts | 8 ++- types/hapi/test/server/server-auth-verify.ts | 26 +++++++++ types/hapi/test/server/server-plugins.ts | 17 ++++-- types/hapi/tsconfig.json | 1 + 6 files changed, 104 insertions(+), 16 deletions(-) create mode 100644 types/hapi/test/server/server-auth-verify.ts diff --git a/types/hapi/index.d.ts b/types/hapi/index.d.ts index 644c48c5d1..3e323f8c44 100644 --- a/types/hapi/index.d.ts +++ b/types/hapi/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for hapi 17.6 +// Type definitions for hapi 17.8 // Project: https://github.com/hapijs/hapi // Definitions by: Rafael Souza Fijalkowski // Justin Simms @@ -42,6 +42,18 @@ import { PolicyOptionVariants, EnginePrototypeOrObject, PolicyOptions, EnginePro + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + */ +/** + * one of + * a single plugin name string. + * an array of plugin name strings. + * an object where each key is a plugin name and each matching value is a + * {@link https://www.npmjs.com/package/semver version range string} which must match the registered + * plugin version. + */ +export type Dependencies = string | string[] | { + [key: string]: string; +}; + /** * [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-serverregistrations) */ @@ -126,7 +138,16 @@ export interface PluginBase { multiple?: boolean; /** (optional) a string or an array of strings indicating a plugin dependency. Same as setting dependencies via server.dependency(). */ - dependencies?: string | string[]; + dependencies?: Dependencies; + + /** + * Allows defining semver requirements for node and hapi. + * @default Allows all. + */ + requirements?: { + node?: string; + hapi?: string; + }; /** once - (optional) if true, will only register the plugin once per server. If set, overrides the once option passed to server.register(). Defaults to no override. */ once?: boolean; @@ -2013,6 +2034,15 @@ export interface ServerAuthSchemeObject { */ response?(request: Request, h: ResponseToolkit): Lifecycle.ReturnValue; + /** + * a method used to verify the authentication credentials provided + * are still valid (e.g. not expired or revoked after the initial authentication). + * the method throws an `Error` when the credentials passed are no longer valid (e.g. expired or + * revoked). Note that the method does not have access to the original request, only to the + * credentials and artifacts produced by the `authenticate()` method. + */ + verify?(auth: RequestAuth): Promise; + /** * An object with the following keys: * * payload @@ -2040,7 +2070,7 @@ export interface ServerAuth { * returned from its implementation function. * [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-serverauthapi) */ - api: Util.Dictionary; + api: Util.Dictionary; /** * Contains the default authentication configuration is a default strategy was set via @@ -2100,7 +2130,19 @@ export interface ServerAuth { * include verifying scope, entity, or other route properties. * [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-await-serverauthteststrategy-request) */ - test(strategy: string, request: Request): Promise; + test(strategy: string, request: Request): Promise; + + /** + * Verify a request's authentication credentials against an authentication strategy. + * Returns nothing if verification was successful, otherwise throws an error. + * + * Note that the `verify()` method does not take into account the route authentication configuration + * or any other information from the request other than the `request.auth` object. It also does not + * perform payload authentication. It is limited to verifying that the previously valid credentials + * are still valid (e.g. have not been revoked or expired). It does not include verifying scope, + * entity, or other route properties. + */ + verify(request: Request): Promise; } export type CachePolicyOptions = PolicyOptionVariants & { @@ -3302,7 +3344,7 @@ export class Server { /** * Server Auth: properties and methods */ - auth: ServerAuth; + readonly auth: ServerAuth; /** * Links another server to the initialize/start/stop state of the current server by calling the @@ -3542,7 +3584,7 @@ export class Server { /** * Used within a plugin to declare a required dependency on other plugins where: - * @param dependencies - a single string or an array of plugin name strings which must be registered in order for this plugin to operate. Plugins listed must be registered before the server is + * @param dependencies - plugins which must be registered in order for this plugin to operate. Plugins listed must be registered before the server is * initialized or started. * @param after - (optional) a function that is called after all the specified dependencies have been registered and before the server starts. The function is only called if the server is * initialized or started. The function signature is async function(server) where: server - the server the dependency() method was called on. @@ -3552,7 +3594,7 @@ export class Server { * The method does not provide version dependency which should be implemented using npm peer dependencies. * [See docs](https://github.com/hapijs/hapi/blob/master/API.md#-serverdependencydependencies-after) */ - dependency(dependencies: string | string[], after?: ((server: Server) => Promise)): void; + dependency(dependencies: Dependencies, after?: ((server: Server) => Promise)): void; /** * Registers a custom content encoding compressor to extend the built-in support for 'gzip' and 'deflate' where: diff --git a/types/hapi/test/server/server-auth-api.ts b/types/hapi/test/server/server-auth-api.ts index f1b51bda8c..4508c641c9 100644 --- a/types/hapi/test/server/server-auth-api.ts +++ b/types/hapi/test/server/server-auth-api.ts @@ -1,14 +1,18 @@ // https://github.com/hapijs/hapi/blob/master/API.md#-serverauthapi import { - Request, - ResponseToolkit, Server, ServerAuthScheme, - ServerAuthSchemeObject, - ServerAuthSchemeOptions } from "hapi"; import * as Boom from "boom"; +declare module 'hapi' { + interface ServerAuthSchemeObjectApi { + settings: { + x: number; + }; + } +} + const scheme: ServerAuthScheme = (server, options) => { return { api: { diff --git a/types/hapi/test/server/server-auth-test.ts b/types/hapi/test/server/server-auth-test.ts index 5103d11c7e..4e2a175e1a 100644 --- a/types/hapi/test/server/server-auth-test.ts +++ b/types/hapi/test/server/server-auth-test.ts @@ -3,6 +3,12 @@ import { Request, ResponseToolkit, Server, ServerAuthScheme, ServerAuthSchemeOptions } from "hapi"; import * as Boom from "boom"; +declare module 'hapi' { + interface AuthCredentials { + name?: string; + } +} + const server = new Server({ port: 8000, }); @@ -15,7 +21,7 @@ const scheme: ServerAuthScheme = (server, options) => { if (!authorization) { throw Boom.unauthorized(null, 'Custom'); } - return h.authenticated({ credentials: { user: { a: 1} } }); + return h.authenticated({ credentials: { name: 'john', } }); } }; }; diff --git a/types/hapi/test/server/server-auth-verify.ts b/types/hapi/test/server/server-auth-verify.ts new file mode 100644 index 0000000000..ec788fcb59 --- /dev/null +++ b/types/hapi/test/server/server-auth-verify.ts @@ -0,0 +1,26 @@ +import { Request, Server, RequestAuth } from "hapi"; +import * as Boom from "boom"; + +const server = new Server({ + port: 8000, +}); + +server.auth.scheme('custom', () => ({ + authenticate() { + throw Boom.unauthorized(null, 'hurr'); + }, + async verify(_auth: RequestAuth) { + throw Boom.unauthorized(null, 'durr'); + } +})); +server.auth.strategy('default', 'custom'); + +server.route({ + method: 'GET', + path: '/', + async handler(request: Request) { + request.server.auth.verify(request); + } +}); + +server.start(); diff --git a/types/hapi/test/server/server-plugins.ts b/types/hapi/test/server/server-plugins.ts index 8de3fdc24f..03a3c76907 100644 --- a/types/hapi/test/server/server-plugins.ts +++ b/types/hapi/test/server/server-plugins.ts @@ -33,22 +33,31 @@ const plugin1: Plugin = { server.plugins.example.other = 'other'; console.log(server.plugins.example.key); // 'value' console.log(server.plugins.example.other); // 'other' - } + }, }; const plugin2: Plugin = { name: 'plugin2', - register: async (server: Server, options: Plugin2) => {} + register: async (server: Server, options: Plugin2) => {}, + dependencies: { + plugin1: '*', + }, + requirements: { + node: '>=8', + hapi: '>=1337', + }, }; const plugin3: Plugin = { name: 'plugin3', - register: async (server: Server, options: Plugin3) => {} + register: async (server: Server, options: Plugin3) => {}, + dependencies: ['plugin2'], }; const plugin4: Plugin = { name: 'plugin4', - register: (server: Server, options: Plugin4) => {} + register: (server: Server, options: Plugin4) => {}, + dependencies: 'plugin3', }; const server = new Server({ diff --git a/types/hapi/tsconfig.json b/types/hapi/tsconfig.json index 2804e84871..9df329c575 100644 --- a/types/hapi/tsconfig.json +++ b/types/hapi/tsconfig.json @@ -41,6 +41,7 @@ "test/server/server-auth-api.ts", "test/server/server-auth-default.ts", "test/server/server-auth-test.ts", + "test/server/server-auth-verify.ts", "test/server/server-bind.ts", "test/server/server-cache-provision.ts", "test/server/server-cache.ts",