-
-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Update authentication documentation
- Loading branch information
Showing
8 changed files
with
123 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--- | ||
meta: | ||
- name: description | ||
content: Authentication configuration | ||
- name: keywords | ||
content: ts.ed express typescript auth node.js javascript decorators | ||
--- | ||
# Authentication | ||
## Usage | ||
|
||
Ts.ED use middleware to protect your route with your own strategy. To handle correctly a request and protect your endpoints, | ||
we have to use the @@UseAuth@@ decorator. | ||
|
||
<<< @/docs/tutorials/snippets/authentication/auth-example.ts | ||
|
||
::: tip | ||
If you planed to use `Passport.js`, it's recommended to follow the [Passport.js guide here](/tutorials/passport.md). | ||
::: | ||
|
||
Any middleware can be used as a authentication strategy. Just keep in mind, to works properly, the middleware must use @@EndpointInfo@@ | ||
decorator to retrieve the endpoint context execution. | ||
|
||
Here an example of the a CustomAuth middleware with use the Passport.js method to check authentication: | ||
|
||
<<< @/docs/tutorials/snippets/authentication/auth-middleware.ts | ||
|
||
## Create your Auth decorator | ||
|
||
It could be practical to create you own Authentication decorator to reduce the amount of code. | ||
For example, if we use swagger, we have to configure some extra **security** and **responses** information and it can quickly become heavy. | ||
|
||
Example: | ||
<<< @/docs/tutorials/snippets/authentication/auth-swagger-example.ts | ||
|
||
To avoid that, we can create a decorator which apply all of theses instructions automatically, like this: | ||
|
||
<<< @/docs/tutorials/snippets/authentication/auth-decorator-example.ts | ||
|
||
And use it on our controller and endpoints: | ||
|
||
<<< @/docs/tutorials/snippets/authentication/auth-custom-auth-example.ts |
11 changes: 11 additions & 0 deletions
11
docs/docs/snippets/authentication/auth-custom-auth-example.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {Controller, Get} from "@tsed/common"; | ||
import {CustomAuth} from "../decorators/CustomAuth"; | ||
|
||
@Controller("/dashboard") | ||
@CustomAuth({role: "admin", scopes: ["email", "firstname"]}) | ||
class DashboardCtrl { | ||
@Get("/") | ||
@CustomAuth({role: "admin", scopes: ["email", "firstname"]}) | ||
public getResource() { | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
docs/docs/snippets/authentication/auth-decorator-example.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import {IUseAuthOptions, UseAuth} from "@tsed/common"; | ||
import {applyDecorators} from "@tsed/core"; | ||
import {Responses, Security} from "@tsed/swagger"; | ||
import {CustomAuthMiddleware} from "../guards/CustomAuthMiddleware"; | ||
|
||
export interface ICustomAuthOptions extends IUseAuthOptions { | ||
role?: string; | ||
scopes?: string[]; | ||
} | ||
|
||
export function CustomAuth(options: ICustomAuthOptions = {scopes: []}): Function { | ||
return applyDecorators( | ||
UseAuth(CustomAuthMiddleware, options), | ||
Security("oauth", ...options.scopes!), | ||
Responses(401, {description: "Unauthorized"}), | ||
Responses(403, {description: "Forbidden"}) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {Controller, Get, UseAuth} from "@tsed/common"; | ||
import {CustomAuthMiddleware} from "../guards/CustomAuthMiddleware"; | ||
|
||
@Controller("/dashboard") | ||
@UseAuth(CustomAuthMiddleware, {role: "admin"}) // on class level for all endpoints | ||
class DashboardCtrl { | ||
@Get("/") | ||
@UseAuth(CustomAuthMiddleware, {role: "admin"}) // or for specific endpoints | ||
public getResource() { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import {EndpointInfo, IMiddleware, Middleware, Req} from "@tsed/common"; | ||
import {Forbidden, Unauthorized} from "ts-httpexceptions"; | ||
|
||
@Middleware() | ||
export class CustomAuthMiddleware implements IMiddleware { | ||
public use(@Req() request: Express.Request, @EndpointInfo() endpoint: EndpointInfo) { | ||
// retrieve options given to the @UseAuth decorator | ||
const options = endpoint.get(CustomAuthMiddleware) || {}; | ||
|
||
if (!request.isAuthenticated()) { // passport.js method to check auth | ||
throw new Forbidden("Forbidden"); | ||
} | ||
|
||
if (request.user.role !== options.role) { | ||
throw new Unauthorized("Unauthorized"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import {Controller, Get, UseAuth} from "@tsed/common"; | ||
import {Responses, Security} from "@tsed/swagger"; | ||
import {CustomAuthMiddleware} from "../guards/CustomAuthMiddleware"; | ||
|
||
@Controller("/dashboard") | ||
@UseAuth(CustomAuthMiddleware, {role: "admin"}) // on class level for all endpoints | ||
@Security("oauth2", "email", "firstname") | ||
@Responses(401, {description: "Unauthorized"}) | ||
@Responses(403, {description: "Forbidden"}) | ||
class DashboardCtrl { | ||
@Get("/") | ||
@UseAuth(CustomAuthMiddleware, {role: "admin"}) // or for specific endpoints | ||
@Security("oauth2", "email", "firstname") | ||
@Responses(401, {description: "Unauthorized"}) | ||
@Responses(403, {description: "Forbidden"}) | ||
public getResource() { | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.