@ghentcdh/authentication-api 0.0.0 → 0.0.2-10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,7 +1,86 @@
1
- # authentication-api
1
+ GhentCdh Keycloak libraries for authentication
2
2
 
3
- This library was generated with [Nx](https://nx.dev).
4
3
 
5
- ## Building
4
+ # Backend nestjs
6
5
 
7
- Run `nx build authentication-api` to build the library.
6
+ ## Install the libraries
7
+
8
+ ```ssh
9
+ pnpm add @ghentcdh/auth/vue @ghentcdh/auth/backend
10
+ ```
11
+
12
+ Add following environment variables to the `.env` file.
13
+
14
+ ```
15
+ - KEYCLOAK_HOST=$KEYCLOAK_HOST
16
+ - KEYCLOAK_REALM=$KEYCLOAK_REALM
17
+ ```
18
+
19
+ ### Secure requests
20
+
21
+ Add the module to your module.ts imports
22
+
23
+ ```typescript
24
+
25
+ import {AuthenticationApiModule} from "@ghentcdh/authentication-api";
26
+
27
+ @Module({
28
+ imports: [AuthenticationApiModule],
29
+ })
30
+ export class MyModule {
31
+ }
32
+
33
+ ```
34
+
35
+ Extend the `GhentCdhGuard` decorator to secure your routes
36
+
37
+
38
+
39
+ ```typescript
40
+ import { Injectable } from '@nestjs/common';
41
+ import { GhentCdhGuard } from '@ghentcdh/authentication-api';
42
+ import { HttpService } from '@nestjs/axios';
43
+ import { ConfigService } from '@nestjs/config';
44
+
45
+ @Injectable()
46
+ export class MyGuard extends GhentCdhGuard {
47
+ constructor(_httpService: HttpService, _configService: ConfigService) {
48
+ super(_httpService, _configService);
49
+ }
50
+ }
51
+
52
+ ```
53
+
54
+ Use the guard in your controller
55
+
56
+ ```typescript
57
+ import {MyGuard} from "./my.guard";
58
+
59
+ @Controller()
60
+ export class MyController {
61
+ @UseGuards(MyGuard)
62
+ @Post('/secure')
63
+ async securePath(@User() user: any, @Request() req: any) {
64
+ return user;
65
+ }
66
+ }
67
+ ```
68
+
69
+ The `@User()` decorator will give you the user object from the keycloak token.
70
+
71
+
72
+ > TODO list
73
+ > - [ ] Add roles decorator and implement logic `@GhentCdhRoles(['admin'])`
74
+
75
+ ## Development environment
76
+
77
+ Make sure the following is added to your hosts file, if you are running a local keycloak instance. Internally the docker
78
+ containers cannot connect to the localhost:8080 port, so a hook is needed in terms of host rewrite.
79
+
80
+ ```sh
81
+ echo "127.0.0.1 authentication\n" | sudo tee -a /etc/hosts
82
+ ```
83
+
84
+ ## Setup docker
85
+
86
+ > TODO describe me
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@ghentcdh/authentication-api",
3
- "version": "0.0.0",
3
+ "license": "MIT",
4
+ "version": "0.0.2-10",
4
5
  "type": "commonjs",
5
6
  "main": "./src/index.js",
6
7
  "types": "./src/index.d.ts",
@@ -14,7 +15,6 @@
14
15
  },
15
16
  "repository": {
16
17
  "type": "git",
17
- "url": "https://github.com/GhentCDH/ghentcdh-monorepo.git",
18
- "directory": "libs/authentication/api"
18
+ "url": "https://github.com/GhentCDH/ghentcdh-monorepo.git"
19
19
  }
20
20
  }
@@ -2,7 +2,6 @@ import { HttpService } from '@nestjs/axios';
2
2
  import type { CanActivate, ExecutionContext } from '@nestjs/common';
3
3
  import { ConfigService } from '@nestjs/config';
4
4
  import type { Observable } from 'rxjs';
5
- export declare const AUTH_NAME = "keycloack";
6
5
  export declare class GhentCdhGuard implements CanActivate {
7
6
  private readonly httpService;
8
7
  private readonly configService;
@@ -1,14 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GhentCdhGuard = exports.AUTH_NAME = void 0;
4
- const tslib_1 = require("tslib");
5
- const axios_1 = require("@nestjs/axios");
3
+ exports.GhentCdhGuard = void 0;
6
4
  const common_1 = require("@nestjs/common");
7
- const config_1 = require("@nestjs/config");
8
5
  const rxjs_1 = require("rxjs");
9
6
  const auth_const_1 = require("./auth.const");
10
- exports.AUTH_NAME = 'keycloack';
11
- let GhentCdhGuard = class GhentCdhGuard {
7
+ class GhentCdhGuard {
12
8
  constructor(httpService, configService) {
13
9
  this.httpService = httpService;
14
10
  this.configService = configService;
@@ -42,11 +38,6 @@ let GhentCdhGuard = class GhentCdhGuard {
42
38
  }
43
39
  throw new common_1.UnauthorizedException();
44
40
  }
45
- };
41
+ }
46
42
  exports.GhentCdhGuard = GhentCdhGuard;
47
- exports.GhentCdhGuard = GhentCdhGuard = tslib_1.__decorate([
48
- (0, common_1.Injectable)(),
49
- tslib_1.__metadata("design:paramtypes", [axios_1.HttpService,
50
- config_1.ConfigService])
51
- ], GhentCdhGuard);
52
43
  //# sourceMappingURL=auth.guard.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"auth.guard.js","sourceRoot":"","sources":["../../../../../../libs/authentication/api/src/lib/auth.guard.ts"],"names":[],"mappings":";;;;AACA,yCAA6C;AAE7C,2CAAmE;AACnE,2CAAgD;AAEhD,+BAAsC;AAGtC,6CAAyC;AAE5B,QAAA,SAAS,GAAG,WAAW,CAAC;AAG9B,IAAM,aAAa,GAAnB,MAAM,aAAa;IACxB,YACmB,WAAwB,EACxB,aAA4B;QAD5B,gBAAW,GAAX,WAAW,CAAa;QACxB,kBAAa,GAAb,aAAa,CAAe;IAC5C,CAAC;IAEJ,WAAW,CACT,OAAyB;QAEzB,MAAM,OAAO,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC,UAAU,EAAE,CAAC;QAEpD,MAAM,aAAa,GAAG,OAAO,CAAC,OAAO,CAAC,aAAa,CAAC;QAEpD,IAAI,aAAa,EAAE,CAAC;YAClB,MAAM,YAAY,GAAG,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,sBAAS,CAAC,IAAI,CAAC,CAAC;YAC5D,MAAM,SAAS,GAAG,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,sBAAS,CAAC,KAAK,CAAC,CAAC;YAG1D,MAAM,GAAG,GAAG,GAAG,YAAY,UAAU,SAAS,mCAAmC,CAAC;YAElF,OAAO,IAAA,qBAAc,EACnB,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,GAAG,EAAE;gBACxB,OAAO,EAAE;oBACP,uEAAuE;oBACvE,aAAa,EAAE,aAAa;oBAC5B,mBAAmB,EAAE,gBAAgB;iBACtC;aACF,CAAC,CACH;iBACE,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE;gBACjB,yDAAyD;gBACzD,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG,EAAE,CAAC;oBAC5B,MAAM,IAAI,8BAAqB,EAAE,CAAC;gBACpC,CAAC;gBAED,0DAA0D;gBAC1D,OAAO,CAAC,IAAI,GAAG,QAAQ,CAAC,IAAI,CAAC;gBAE7B,OAAO,IAAI,CAAC;YACd,CAAC,CAAC;iBACD,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;gBACf,MAAM,IAAI,8BAAqB,EAAE,CAAC;YACpC,CAAC,CAAC,CAAC;QACP,CAAC;QAED,MAAM,IAAI,8BAAqB,EAAE,CAAC;IACpC,CAAC;CACF,CAAA;AA/CY,sCAAa;wBAAb,aAAa;IADzB,IAAA,mBAAU,GAAE;6CAGqB,mBAAW;QACT,sBAAa;GAHpC,aAAa,CA+CzB"}
1
+ {"version":3,"file":"auth.guard.js","sourceRoot":"","sources":["../../../../../../libs/authentication/api/src/lib/auth.guard.ts"],"names":[],"mappings":";;;AAGA,2CAAmE;AAGnE,+BAAsC;AAGtC,6CAAyC;AAEzC,MAAa,aAAa;IACxB,YACmB,WAAwB,EACxB,aAA4B;QAD5B,gBAAW,GAAX,WAAW,CAAa;QACxB,kBAAa,GAAb,aAAa,CAAe;IAC5C,CAAC;IAEJ,WAAW,CACT,OAAyB;QAEzB,MAAM,OAAO,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC,UAAU,EAAE,CAAC;QAEpD,MAAM,aAAa,GAAG,OAAO,CAAC,OAAO,CAAC,aAAa,CAAC;QAEpD,IAAI,aAAa,EAAE,CAAC;YAClB,MAAM,YAAY,GAAG,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,sBAAS,CAAC,IAAI,CAAC,CAAC;YAC5D,MAAM,SAAS,GAAG,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,sBAAS,CAAC,KAAK,CAAC,CAAC;YAG1D,MAAM,GAAG,GAAG,GAAG,YAAY,UAAU,SAAS,mCAAmC,CAAC;YAElF,OAAO,IAAA,qBAAc,EACnB,IAAI,CAAC,WAAW,CAAC,GAAG,CAAC,GAAG,EAAE;gBACxB,OAAO,EAAE;oBACP,uEAAuE;oBACvE,aAAa,EAAE,aAAa;oBAC5B,mBAAmB,EAAE,gBAAgB;iBACtC;aACF,CAAC,CACH;iBACE,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE;gBACjB,yDAAyD;gBACzD,IAAI,QAAQ,CAAC,MAAM,KAAK,GAAG,EAAE,CAAC;oBAC5B,MAAM,IAAI,8BAAqB,EAAE,CAAC;gBACpC,CAAC;gBAED,0DAA0D;gBAC1D,OAAO,CAAC,IAAI,GAAG,QAAQ,CAAC,IAAI,CAAC;gBAE7B,OAAO,IAAI,CAAC;YACd,CAAC,CAAC;iBACD,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;gBACf,MAAM,IAAI,8BAAqB,EAAE,CAAC;YACpC,CAAC,CAAC,CAAC;QACP,CAAC;QAED,MAAM,IAAI,8BAAqB,EAAE,CAAC;IACpC,CAAC;CACF;AA/CD,sCA+CC"}
@@ -1 +1 @@
1
- {"version":3,"file":"authentication-api.module.js","sourceRoot":"","sources":["../../../../../../libs/authentication/api/src/lib/authentication-api.module.ts"],"names":[],"mappings":";;;;AAAA,yCAA2C;AAC3C,2CAAwC;AACxC,2CAA8C;AAE9C,yDAAqD;AAQ9C,IAAM,uBAAuB,GAA7B,MAAM,uBAAuB;CAAG,CAAA;AAA1B,0DAAuB;kCAAvB,uBAAuB;IANnC,IAAA,eAAM,EAAC;QACN,OAAO,EAAE,CAAC,kBAAU,EAAE,qBAAY,CAAC;QACnC,WAAW,EAAE,CAAC,kCAAe,CAAC;QAC9B,SAAS,EAAE,EAAE;QACb,OAAO,EAAE,EAAE;KACZ,CAAC;GACW,uBAAuB,CAAG"}
1
+ {"version":3,"file":"authentication-api.module.js","sourceRoot":"","sources":["../../../../../../libs/authentication/api/src/lib/authentication-api.module.ts"],"names":[],"mappings":";;;;AAAA,yCAA2C;AAC3C,2CAAwC;AACxC,2CAA8C;AAG9C,yDAAqD;AAQ9C,IAAM,uBAAuB,GAA7B,MAAM,uBAAuB;CAAG,CAAA;AAA1B,0DAAuB;kCAAvB,uBAAuB;IANnC,IAAA,eAAM,EAAC;QACN,OAAO,EAAE,CAAC,kBAAU,EAAE,qBAAY,CAAC;QACnC,WAAW,EAAE,CAAC,kCAAe,CAAC;QAC9B,SAAS,EAAE,EAAE;QACb,OAAO,EAAE,EAAE;KACZ,CAAC;GACW,uBAAuB,CAAG"}