18.1.0 • Published 11 days ago

@whook/authorization v18.1.0

Weekly downloads
20
License
MIT
Repository
github
Last release
11 days ago

@whook/authorization

A wrapper to provide authorization support to a Whook server

GitHub license

This Whook wrapper ties authentication with only two kinds of input:

  • the authorization header which allows several mechanisms to be used including custom ones (this module is using http-auth-utils under the hood). This is the recommended way to authenticate with a server.
  • the access_token defined by the RFC6750 that allows providing the token via query parameters for convenience (mostly for development purpose). Note that you can disable it by setting the DEFAULT_MECHANISM constant to an empty string.

Note that the form-encoded body parameter defined by the bearer authentication RFC is volontarily not supported since nowadays everyone uses JSON and there is no situations where one could not set the token in headers.

To use this wrapper, you'll have to create an authentication service. Here is a simple unique token based implementation (usually in src/services/authentication.ts):

import { autoService } from 'knifecycle';
import { YError } from 'yerror';
import {
  AuthenticationService,
  BaseAuthenticationData,
} from '@whook/authorization';

export type AuthenticationConfig = {
  TOKEN: string;
};

export type BearerPayload = { hash: string };
export type MyAuthenticationService = AuthenticationService<
  BearerPayload,
  BaseAuthenticationData & {
    userId: number;
  }
>;

export default autoService(initAuthentication);

async function initAuthentication({
  TOKEN,
}: AuthenticationConfig): Promise<MyAuthenticationService> {
  const authentication: MyAuthenticationService = {
    // The authentication service must have a check
    // method which take the type of authentication
    // used by the client and its parsed payload
    check: async (type, data) => {
      if (type === 'bearer') {
        if (data.hash === TOKEN) {
          // When successful, the authentication check
          // must return an object with at least a `scopes`
          // property containing an array of the actual
          // scopes the authentication check resolved to
          return {
            applicationId: 'abbacaca-abba-caca-abba-cacaabbacaca',
            userId: 1,
            scope: 'admin',
          };
        }
        throw new YError('E_BAD_BEARER_TOKEN', type, data.hash);
      }
      // Of course, the service must check the auth type
      // and fail if not supported to avoid security issues
      throw new YError('E_UNEXPECTED_AUTH_TYPE', type);
    },
  };

  return authentication;
}

The properties added next to the scopes one will be passed to the wrapped handlers and an authenticated property will also be added in order for handlers to know if the client were authenticated.

Then, simply install this plugin:

npm i @whook/authorization;

Declare this module types in your src/whook.d.ts type definitions:

+ import type { WhookAuthorizationConfig } from '@whook/authorization';

// ...

declare module 'application-services' {

  // ...

  export interface AppConfig
    extends WhookBaseConfigs,
      WhookAuthorizationConfig,
      WhookSwaggerUIConfig,
      WhookCORSConfig,
      APIConfig,
  export interface AppConfig
    extends WhookBaseConfigs,
+    WhookAuthorizationConfig,
    JWTServiceConfig {}

  // ...

}

Then add the config and the errors descriptors or provide your own (usually in src/config/common/config.js):

// ...
import { DEFAULT_ERRORS_DESCRIPTORS } from '@whook/http-router';
+ import {
+   AUTHORIZATION_ERRORS_DESCRIPTORS,
+ } from '@whook/authorization';
import type { AppConfig } from 'application-services';

// ...

const CONFIG: AppConfig = {
  // ...
-   ERRORS_DESCRIPTORS: DEFAULT_ERRORS_DESCRIPTORS,
+   ERRORS_DESCRIPTORS: {
+     ...DEFAULT_ERRORS_DESCRIPTORS,
+     ...AUTHORIZATION_ERRORS_DESCRIPTORS,
+   },
+   DEFAULT_MECHANISM: 'bearer',
  // ...
};

export default CONFIG;

And finally declare this plugin in the src/index.ts file of your project:

  // ...

  $.register(
    constant('HANDLERS_WRAPPERS', [
      'wrapHandlerWithCORS',
+      'wrapHandlerWithAuthorization',
    ]),
  );

  // ...

  $.register(
    constant('WHOOK_PLUGINS', [
      ...WHOOK_DEFAULT_PLUGINS,
      '@whook/cors',
+      '@whook/authorization',
    ]),
  );

  // ...

To see a complete usage of this wrapper, you may have a look at the @whook/example project.

API

initWrapHandlerWithAuthorization(services) ⇒ Promise.<Object>

Wrap an handler to check client's authorizations.

Kind: global function
Returns: Promise.<Object> - A promise of an object containing the reshaped env vars.

ParamTypeDescription
servicesObjectThe services ENV depends on
services.MECHANISMSArrayThe list of supported auth mechanisms
services.DEFAULT_MECHANISMstringThe default authentication mechanism
services.authenticationObjectThe authentication service
services.logObjectA logging service

Authors

License

MIT

18.1.0

11 days ago

18.0.3

17 days ago

18.0.0

17 days ago

17.0.2

3 months ago

17.0.1

5 months ago

17.0.0

5 months ago

16.1.1

7 months ago

16.1.0

9 months ago

16.0.1

10 months ago

16.0.0

10 months ago

15.0.0

10 months ago

13.0.0

1 year ago

13.1.2

1 year ago

13.1.0

1 year ago

13.2.0

1 year ago

14.0.0

1 year ago

13.2.1

1 year ago

12.1.0

2 years ago

10.0.5

2 years ago

10.0.6

2 years ago

10.0.0

3 years ago

10.0.1

3 years ago

10.0.2

3 years ago

10.0.3

2 years ago

10.0.4

2 years ago

12.0.0

2 years ago

12.0.1

2 years ago

12.0.2

2 years ago

11.0.0

2 years ago

11.0.1

2 years ago

9.0.1

3 years ago

9.0.0

3 years ago

8.5.1

3 years ago

8.5.0

3 years ago

8.4.2

3 years ago

8.4.1

3 years ago

8.4.0

3 years ago

8.3.0

3 years ago

8.1.0

4 years ago

8.1.1

4 years ago

8.0.4

4 years ago

8.0.1

4 years ago

8.0.0

4 years ago

8.0.3

4 years ago

8.0.2

4 years ago

7.1.5

4 years ago

7.1.4

4 years ago

7.1.3

4 years ago

7.1.1

4 years ago

7.1.0

4 years ago

7.0.0

4 years ago

6.0.0

4 years ago

5.1.6

4 years ago

5.1.5

4 years ago

5.1.4

4 years ago

5.1.3

4 years ago

5.1.2

4 years ago

5.1.1

4 years ago

5.1.0

4 years ago

5.0.0

4 years ago

4.1.1

4 years ago

4.1.0

4 years ago

4.0.4

5 years ago

4.0.2

5 years ago

4.0.1

5 years ago

4.0.0

5 years ago

4.0.0-alpha.44

5 years ago

4.0.0-alpha.43

5 years ago

4.0.0-alpha.42

5 years ago

4.0.0-alpha.41

5 years ago

4.0.0-alpha.40

5 years ago

4.0.0-alpha.39

5 years ago

4.0.0-alpha.38

5 years ago

4.0.0-alpha.37

5 years ago

4.0.0-alpha.36

5 years ago

4.0.0-alpha.35

5 years ago

4.0.0-alpha.34

5 years ago

4.0.0-alpha.33

5 years ago

4.0.0-alpha.32

5 years ago

4.0.0-alpha.31

5 years ago

4.0.0-alpha.30

5 years ago

4.0.0-alpha.29

5 years ago

4.0.0-alpha.28

5 years ago

4.0.0-alpha.27

5 years ago

4.0.0-alpha.26

5 years ago

4.0.0-alpha.25

5 years ago

4.0.0-alpha.24

5 years ago

4.0.0-alpha.20

5 years ago

4.0.0-alpha.23

5 years ago

4.0.0-alpha.22

5 years ago

4.0.0-alpha.21

5 years ago

4.0.0-alpha.19

5 years ago

4.0.0-alpha.18

5 years ago

4.0.0-alpha.17

5 years ago

4.0.0-alpha.16

5 years ago

4.0.0-alpha.15

5 years ago

4.0.0-alpha.14

5 years ago

4.0.0-alpha.13

5 years ago

4.0.0-alpha.12

5 years ago

4.0.0-alpha.11

5 years ago

4.0.0-alpha.10

6 years ago

4.0.0-alpha.9

6 years ago

4.0.0-alpha.8

6 years ago

4.0.0-alpha.7

6 years ago

4.0.0-alpha.6

6 years ago

4.0.0-alpha.5

6 years ago

4.0.0-alpha.4

6 years ago

4.0.0-alpha.3

6 years ago

4.0.0-alpha.0

6 years ago