Inversifyjs: Implement HttpContext & AuthProvider in inversify-express-utils

Created on 10 Nov 2017  路  2Comments  路  Source: inversify/InversifyJS

The motivation for this issue was this comment.

I would like to implement the following:

A) Access to current request and response via HttpContext

The idea of HttpContext is based on ASP.NET and is a feature that allows the request and response to objects to be injected.

What should happen then is the following:

  1. An HTTP request hits the server.
  2. A child container is created using this._container.createChild() to prevent rare conditions.
  3. A binding is declared for TYPE.HttpContext and it includes current request and response
interface HttpContext {
    request: Request;
    response: Response; 
}
  1. Routing takes places and container.get is invoked.
  2. The TYPE.HttpContext is resolved usinginRequestScope if the controller has a dependency on it.
  3. Other dependencies of the Controller are resolved.
  4. All the dependencies (including TYPE.HttpContext) are injected into de Controller.
  5. The controller method is invoked

B) Access to current user via HttpContext

This will add an extra bit to the previous workflow. What should happen then is the following:

  1. The developer configures a custom AuthProvider before a request hits the server.
  2. An HTTP request hits the server.
  3. A child container is created using this._container.createChild() to prevent rare conditions.
  4. The current user is resolved using a AuthProvider if it is available (undefined by default).
  5. A binding is declared for TYPE.HttpContext and it includes current request, response, and user
interface HttpContext {
    request: Request;
    response: Response; 
    user: UserIdentity;
}
  1. Routing takes places and container.get is invoked.
  2. The TYPE.HttpContext is resolved usinginRequestScope if the controller has a dependency on it.
  3. Other dependencies of the Controller are resolved.
  4. All the dependencies (including TYPE.HttpContext) are injected into de Controller.
  5. The controller method is invoked

C) Investigate support BaseHttpController to reduce boilerplate

We could also create a base controller instance that gets HttpContext injected by default so users will be able to do:

import * as express from "express";
import { interfaces, controller, httpGet, httpPost, httpDelete, request, queryParam, response, requestParam, BaseHttpController } from "inversify-express-utils";
import { injectable, inject } from "inversify";

@controller("/foo")
@injectable()
export class FooController extends BaseHttpController {

    @inject("FooService") private fooService: FooService;

    @httpGet("/")
    private index(): string {
        if (this.httpContext.user === undefined) {
            throw new Error();
       }
       return this.fooService.get((this.httpContext.user.id);
    }

}

D) Document authorize middleware using HttpContext

We can document examples to implement a middleware that uses the HttpContext to validate if an user has access to certan feature:

@controller(
    "/foo",
     authorize({ feature: FEATURE.SOME_FEATURE_FLAG }),
)
@injectable()
export class FooController extends BaseHttpController {
    // ...
}

Or validate if an user has access to certan role:

@controller(
    "/foo",
     authorize({ role: ROLE.SOME_ROLE }),
)
@injectable()
export class FooController extends BaseHttpController {
    // ...
}

The community will then develop libraries for particular databases etc.

enhancement express-utils

Most helpful comment

All 2 comments

Was this page helpful?
0 / 5 - 0 ratings