Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement HttpContext & AuthProvider in inversify-express-utils #672

Closed
remojansen opened this issue Nov 10, 2017 · 2 comments
Closed

Implement HttpContext & AuthProvider in inversify-express-utils #672

remojansen opened this issue Nov 10, 2017 · 2 comments

Comments

@remojansen
Copy link
Member

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.

@remojansen
Copy link
Member Author

I have implemented A, B and C inversify/inversify-express-utils#72 and I have moved D to a new issue #673

@remojansen
Copy link
Member Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant