@next-nest-auth/nestauth 0.0.5

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.
Files changed (96) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +166 -0
  3. package/dist/global-exception.filter.d.ts +4 -0
  4. package/dist/global-exception.filter.js +30 -0
  5. package/dist/global-exception.filter.js.map +1 -0
  6. package/dist/http-exception.filter.d.ts +4 -0
  7. package/dist/http-exception.filter.js +37 -0
  8. package/dist/http-exception.filter.js.map +1 -0
  9. package/dist/http.exception.filter.d.ts +4 -0
  10. package/dist/http.exception.filter.js +29 -0
  11. package/dist/http.exception.filter.js.map +1 -0
  12. package/dist/index.d.ts +4 -0
  13. package/dist/index.js +21 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/jwt-auth.guard.d.ts +4 -0
  16. package/dist/jwt-auth.guard.js +18 -0
  17. package/dist/jwt-auth.guard.js.map +1 -0
  18. package/dist/jwt.guard.d.ts +4 -0
  19. package/dist/jwt.guard.js +18 -0
  20. package/dist/jwt.guard.js.map +1 -0
  21. package/dist/jwt.strategy.d.ts +12 -0
  22. package/dist/jwt.strategy.js +37 -0
  23. package/dist/jwt.strategy.js.map +1 -0
  24. package/dist/local-auth.guard.d.ts +4 -0
  25. package/dist/local-auth.guard.js +18 -0
  26. package/dist/local-auth.guard.js.map +1 -0
  27. package/dist/local.srategy.d.ts +9 -0
  28. package/dist/local.srategy.js +33 -0
  29. package/dist/local.srategy.js.map +1 -0
  30. package/dist/local.strategy.d.ts +11 -0
  31. package/dist/local.strategy.js +42 -0
  32. package/dist/local.strategy.js.map +1 -0
  33. package/dist/nestauth/nestauth.controller.d.ts +2 -0
  34. package/dist/nestauth/nestauth.controller.js +17 -0
  35. package/dist/nestauth/nestauth.controller.js.map +1 -0
  36. package/dist/nestauth/nestauth.controller.spec.d.ts +1 -0
  37. package/dist/nestauth/nestauth.controller.spec.js +17 -0
  38. package/dist/nestauth/nestauth.controller.spec.js.map +1 -0
  39. package/dist/nestauth-facebook.guard.d.ts +7 -0
  40. package/dist/nestauth-facebook.guard.js +29 -0
  41. package/dist/nestauth-facebook.guard.js.map +1 -0
  42. package/dist/nestauth-facebook.strategy.d.ts +9 -0
  43. package/dist/nestauth-facebook.strategy.js +45 -0
  44. package/dist/nestauth-facebook.strategy.js.map +1 -0
  45. package/dist/nestauth-google.guard.d.ts +7 -0
  46. package/dist/nestauth-google.guard.js +29 -0
  47. package/dist/nestauth-google.guard.js.map +1 -0
  48. package/dist/nestauth-google.strategy.d.ts +9 -0
  49. package/dist/nestauth-google.strategy.js +44 -0
  50. package/dist/nestauth-google.strategy.js.map +1 -0
  51. package/dist/nestauth-jwt.guard.d.ts +6 -0
  52. package/dist/nestauth-jwt.guard.js +32 -0
  53. package/dist/nestauth-jwt.guard.js.map +1 -0
  54. package/dist/nestauth-jwt.strategy.d.ts +18 -0
  55. package/dist/nestauth-jwt.strategy.js +45 -0
  56. package/dist/nestauth-jwt.strategy.js.map +1 -0
  57. package/dist/nestauth-local.guard.d.ts +4 -0
  58. package/dist/nestauth-local.guard.js +18 -0
  59. package/dist/nestauth-local.guard.js.map +1 -0
  60. package/dist/nestauth-local.strategy.d.ts +11 -0
  61. package/dist/nestauth-local.strategy.js +40 -0
  62. package/dist/nestauth-local.strategy.js.map +1 -0
  63. package/dist/nestauth.controller.d.ts +15 -0
  64. package/dist/nestauth.controller.js +119 -0
  65. package/dist/nestauth.controller.js.map +1 -0
  66. package/dist/nestauth.guard.d.ts +4 -0
  67. package/dist/nestauth.guard.js +18 -0
  68. package/dist/nestauth.guard.js.map +1 -0
  69. package/dist/nestauth.interface.d.ts +32 -0
  70. package/dist/nestauth.interface.js +3 -0
  71. package/dist/nestauth.interface.js.map +1 -0
  72. package/dist/nestauth.module.d.ts +5 -0
  73. package/dist/nestauth.module.js +81 -0
  74. package/dist/nestauth.module.js.map +1 -0
  75. package/dist/nestauth.service.d.ts +13 -0
  76. package/dist/nestauth.service.js +71 -0
  77. package/dist/nestauth.service.js.map +1 -0
  78. package/dist/nestauth.strategy.d.ts +11 -0
  79. package/dist/nestauth.strategy.js +38 -0
  80. package/dist/nestauth.strategy.js.map +1 -0
  81. package/dist/tsconfig.tsbuildinfo +1 -0
  82. package/package.json +61 -0
  83. package/src/http-exception.filter.ts +33 -0
  84. package/src/index.ts +4 -0
  85. package/src/nestauth-facebook.guard.ts +12 -0
  86. package/src/nestauth-facebook.strategy.ts +39 -0
  87. package/src/nestauth-google.guard.ts +12 -0
  88. package/src/nestauth-google.strategy.ts +39 -0
  89. package/src/nestauth-jwt.guard.ts +24 -0
  90. package/src/nestauth-jwt.strategy.ts +27 -0
  91. package/src/nestauth-local.guard.ts +5 -0
  92. package/src/nestauth-local.strategy.ts +26 -0
  93. package/src/nestauth.controller.ts +70 -0
  94. package/src/nestauth.interface.ts +37 -0
  95. package/src/nestauth.module.ts +73 -0
  96. package/src/nestauth.service.ts +65 -0
@@ -0,0 +1,24 @@
1
+ import {
2
+ ExecutionContext,
3
+ Injectable,
4
+ UnauthorizedException,
5
+ } from "@nestjs/common";
6
+ import { AuthGuard } from "@nestjs/passport";
7
+ import * as macaddress from "macaddress";
8
+
9
+ @Injectable()
10
+ export class NestAuthJwtGuard extends AuthGuard("jwt") {
11
+ async canActivate(context: ExecutionContext): Promise<boolean> {
12
+ const request = context.switchToHttp().getRequest();
13
+ await super.canActivate(context);
14
+ const user = request.user;
15
+ if (!user) {
16
+ throw new UnauthorizedException("Unauthorized: Invalid token");
17
+ }
18
+ const currentMacId = await macaddress.one();
19
+ if (user.macId !== currentMacId) {
20
+ throw new UnauthorizedException("Unauthorized: Device mismatch");
21
+ }
22
+ return true;
23
+ }
24
+ }
@@ -0,0 +1,27 @@
1
+ import { ExtractJwt, Strategy } from "passport-jwt";
2
+ import { PassportStrategy } from "@nestjs/passport";
3
+ import { Inject, Injectable } from "@nestjs/common";
4
+ import { JwtPayload } from "jsonwebtoken";
5
+
6
+ @Injectable()
7
+ export class NestAuthJwtStrategy extends PassportStrategy(Strategy) {
8
+ constructor(@Inject("JWT_SECRET") jwtSecret: string) {
9
+ super({
10
+ jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
11
+ ignoreExpiration: false,
12
+ secretOrKey: jwtSecret,
13
+ });
14
+ }
15
+
16
+ async validate(payload: JwtPayload) {
17
+ return {
18
+ userId: payload.sub,
19
+ macId: payload.macId,
20
+ email: payload.email,
21
+ role: payload.role,
22
+ name: payload.name,
23
+ username: payload.username,
24
+ pic: payload.pic,
25
+ };
26
+ }
27
+ }
@@ -0,0 +1,5 @@
1
+ import { Injectable } from "@nestjs/common";
2
+ import { AuthGuard } from "@nestjs/passport";
3
+
4
+ @Injectable()
5
+ export class NestAuthLocalGuard extends AuthGuard("nestauth-local") {}
@@ -0,0 +1,26 @@
1
+ import { PassportStrategy } from "@nestjs/passport";
2
+ import { Strategy } from "passport-custom";
3
+ import { Inject, Injectable, UnauthorizedException } from "@nestjs/common";
4
+ import { NestAuthInterface } from "./nestauth.interface";
5
+ import * as macaddress from "macaddress";
6
+
7
+ @Injectable()
8
+ export class NestAuthLocalStrategy extends PassportStrategy(
9
+ Strategy,
10
+ "nestauth-local"
11
+ ) {
12
+ constructor(
13
+ @Inject("UserService") private readonly userService: NestAuthInterface
14
+ ) {
15
+ super();
16
+ }
17
+
18
+ async validate(req: Request): Promise<any> {
19
+ const user = await this.userService.validateUser(req.body);
20
+ if (!user) {
21
+ throw new UnauthorizedException("Invalid credentials");
22
+ }
23
+ user.macId = await macaddress.one();
24
+ return user;
25
+ }
26
+ }
@@ -0,0 +1,70 @@
1
+ import {
2
+ Request,
3
+ Controller,
4
+ Get,
5
+ Post,
6
+ UseGuards,
7
+ Body,
8
+ UnauthorizedException,
9
+ HttpStatus,
10
+ All,
11
+ } from "@nestjs/common";
12
+ import { NestAuthService } from "./nestauth.service";
13
+ import { NestAuthLocalGuard } from "./nestauth-local.guard";
14
+ import { NestAuthGoogleGuard } from "./nestauth-google.guard";
15
+ import { NestAuthFacebookGuard } from "./nestauth-facebook.guard";
16
+
17
+ @Controller("nestauth")
18
+ export class NestAuthController {
19
+ constructor(private readonly nestAuthService: NestAuthService) {}
20
+
21
+ @All()
22
+ async greetings(): Promise<string> {
23
+ return "Welcome to NestAuth. Please check our documentation for more information.";
24
+ }
25
+
26
+ @UseGuards(NestAuthLocalGuard)
27
+ @Post("login")
28
+ async login(@Request() req): Promise<any> {
29
+ return this.nestAuthService.login(req.user);
30
+ }
31
+
32
+ @Get("refresh-token")
33
+ refreshToken(@Body() params: { refresh_token: string }): Promise<any> {
34
+ return this.nestAuthService.refreshToken(params.refresh_token);
35
+ }
36
+
37
+ @UseGuards(NestAuthGoogleGuard)
38
+ @Get("google")
39
+ async googleAuth(@Request() req): Promise<any> {}
40
+
41
+ @Get("google-redirect")
42
+ @UseGuards(NestAuthGoogleGuard)
43
+ googleAuthRedirect(@Request() req) {
44
+ if (!req.user) {
45
+ throw new UnauthorizedException("Unable to login with Google");
46
+ }
47
+ return this.nestAuthService.google(req.user);
48
+ }
49
+
50
+ @Get("/facebook")
51
+ @UseGuards(NestAuthFacebookGuard)
52
+ async facebookLogin(): Promise<any> {
53
+ return HttpStatus.OK;
54
+ }
55
+
56
+ @Get("/facebook-redirect")
57
+ @UseGuards(NestAuthFacebookGuard)
58
+ async facebookLoginRedirect(@Request() req): Promise<any> {
59
+ if (!req.user) {
60
+ throw new UnauthorizedException("Unable to login with Facebook");
61
+ }
62
+ return this.nestAuthService.facebook(req.user);
63
+ }
64
+
65
+ @UseGuards(NestAuthLocalGuard)
66
+ @Get("logout")
67
+ async logout(@Request() req) {
68
+ return req.logout();
69
+ }
70
+ }
@@ -0,0 +1,37 @@
1
+ import { Type } from "@nestjs/common";
2
+
3
+ export interface NestAuthModuleOptions {
4
+ UserService: Type<NestAuthInterface>;
5
+ jwtSecret: string;
6
+ jwtExpiresIn?: string;
7
+ jwtRefreshTokenExpiresIn?: string;
8
+ }
9
+
10
+ export type JwtPayloadType = {
11
+ sub: number;
12
+ name?: string;
13
+ email?: string;
14
+ username?: string;
15
+ role?: string;
16
+ pic?: string;
17
+ macId?: string;
18
+ };
19
+
20
+ export type SocialProfileType = {
21
+ id: string;
22
+ email: string;
23
+ firstName: string;
24
+ lastName: string;
25
+ picture: string;
26
+ accessToken: string;
27
+ refreshToken: string;
28
+ };
29
+
30
+ export type GoogleProfileType = SocialProfileType;
31
+ export type FacebookProfileType = SocialProfileType;
32
+
33
+ export interface NestAuthInterface {
34
+ validateUser(params: any): Promise<JwtPayloadType>;
35
+ google(params: GoogleProfileType): Promise<JwtPayloadType>;
36
+ facebook(params: FacebookProfileType): Promise<JwtPayloadType>;
37
+ }
@@ -0,0 +1,73 @@
1
+ import { Module, DynamicModule, Provider } from "@nestjs/common";
2
+ import { ConfigModule } from "@nestjs/config";
3
+ import { NestAuthService } from "./nestauth.service";
4
+ import { NestAuthController } from "./nestauth.controller";
5
+ import { NestAuthModuleOptions } from "./nestauth.interface";
6
+ import { PassportModule } from "@nestjs/passport";
7
+ import { JwtModule } from "@nestjs/jwt";
8
+ import { NestAuthJwtStrategy } from "./nestauth-jwt.strategy";
9
+ import { NestAuthLocalStrategy } from "./nestauth-local.strategy";
10
+ import { NestAuthGoogleStrategy } from "./nestauth-google.strategy";
11
+ import { NestAuthFacebookStrategy } from "./nestauth-facebook.strategy";
12
+ import { APP_FILTER } from "@nestjs/core";
13
+ import { HttpExceptionFilter } from "./http-exception.filter";
14
+
15
+ @Module({
16
+ imports: [PassportModule, ConfigModule.forRoot({})],
17
+ })
18
+ export class NestAuthModule {
19
+ static register(options: NestAuthModuleOptions): DynamicModule {
20
+ const UserServiceProvider: Provider = {
21
+ provide: "UserService",
22
+ useClass: options.UserService,
23
+ };
24
+
25
+ const JwtSecretProvider: Provider = {
26
+ provide: "JWT_SECRET",
27
+ useValue: options.jwtSecret || "60s",
28
+ };
29
+
30
+ const JwtExpiresInProvider: Provider = {
31
+ provide: "JWT_EXPIRES_IN",
32
+ useValue: options.jwtExpiresIn,
33
+ };
34
+
35
+ const JwtRefreshTokenExpiresInProvider: Provider = {
36
+ provide: "JWT_REFRESH_TOKEN_EXPIRES_IN",
37
+ useValue: options.jwtRefreshTokenExpiresIn,
38
+ };
39
+
40
+ return {
41
+ module: NestAuthModule,
42
+ imports: [
43
+ JwtModule.registerAsync({
44
+ imports: [],
45
+ inject: [],
46
+ useFactory: async () => ({
47
+ secret: options.jwtSecret,
48
+ signOptions: {
49
+ expiresIn: options.jwtExpiresIn,
50
+ },
51
+ }),
52
+ }),
53
+ ],
54
+ providers: [
55
+ NestAuthService,
56
+ UserServiceProvider,
57
+ NestAuthJwtStrategy,
58
+ NestAuthLocalStrategy,
59
+ NestAuthGoogleStrategy,
60
+ NestAuthFacebookStrategy,
61
+ JwtSecretProvider,
62
+ JwtExpiresInProvider,
63
+ JwtRefreshTokenExpiresInProvider,
64
+ {
65
+ provide: APP_FILTER,
66
+ useClass: HttpExceptionFilter,
67
+ },
68
+ ],
69
+ exports: [NestAuthService],
70
+ controllers: [NestAuthController],
71
+ };
72
+ }
73
+ }
@@ -0,0 +1,65 @@
1
+ import { Inject, Injectable, UnauthorizedException } from "@nestjs/common";
2
+ import {
3
+ FacebookProfileType,
4
+ GoogleProfileType,
5
+ JwtPayloadType,
6
+ NestAuthInterface,
7
+ } from "./nestauth.interface";
8
+ import { JwtService } from "@nestjs/jwt";
9
+
10
+ @Injectable()
11
+ export class NestAuthService {
12
+ constructor(
13
+ private jwtService: JwtService,
14
+ @Inject("UserService") private readonly userService: NestAuthInterface,
15
+ @Inject("JWT_EXPIRES_IN") private readonly jwtExpiresIn: string,
16
+ @Inject("JWT_REFRESH_TOKEN_EXPIRES_IN")
17
+ private readonly jwtRefreshTokenExpiresIn: string
18
+ ) {}
19
+
20
+ async login(user: any): Promise<any> {
21
+ return {
22
+ accessToken: this.jwtService.sign(user, {
23
+ expiresIn: this.jwtExpiresIn || "15m",
24
+ }),
25
+ refreshToken: this.jwtService.sign(user, { expiresIn: "7d" }),
26
+ accessTokenExpiresIn: this.jwtExpiresIn || "15m",
27
+ refreshTokenExpiresIn: this.jwtRefreshTokenExpiresIn || "7d",
28
+ };
29
+ }
30
+
31
+ async google(user: GoogleProfileType): Promise<any> {
32
+ const payload: JwtPayloadType = await this.userService.google(user);
33
+
34
+ if (!payload) {
35
+ throw new UnauthorizedException("Invalid credentials");
36
+ }
37
+
38
+ return this.login(payload);
39
+ }
40
+
41
+ async facebook(user: FacebookProfileType): Promise<any> {
42
+ const payload: JwtPayloadType = await this.userService.facebook(user);
43
+
44
+ if (!payload) {
45
+ throw new UnauthorizedException("Invalid credentials");
46
+ }
47
+
48
+ return this.login(payload);
49
+ }
50
+
51
+ async refreshToken(refreshToken: string) {
52
+ try {
53
+ const payload = this.jwtService.verify(refreshToken);
54
+ const user = await this.userService.validateUser(payload.id);
55
+ if (!user) {
56
+ throw new UnauthorizedException(
57
+ "Invalid or expired refresh token"
58
+ );
59
+ }
60
+ return this.login(user);
61
+ } catch (err) {
62
+ throw new UnauthorizedException("Invalid or expired refresh token");
63
+ }
64
+ }
65
+ }