Skip to content

Commit 32c582d

Browse files
Merge branch 'stage'
2 parents c43e461 + ee442fe commit 32c582d

File tree

372 files changed

+15616
-16560
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

372 files changed

+15616
-16560
lines changed

.jest/setEnvVars.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
process.env.KAFKAJS_LOG_LEVEL='ERROR'
1+
process.env.KAFKAJS_LOG_LEVEL = "ERROR";

.prettierrc.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

.prettierrc.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"trailingComma": "es5",
3+
"tabWidth": 2,
4+
"printWidth": 120,
5+
"semi": true,
6+
"singleQuote": false,
7+
"useTabs": false,
8+
"bracketSpacing": true,
9+
"arrowParens": "avoid",
10+
"endOfLine": "auto"
11+
}

package-lock.json

Lines changed: 540 additions & 412 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/auth/api-key.strategy.ts

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,47 +10,36 @@ import { ApiKeyHeader, ApiKeyStrategyName, HeaderApiVerifiedCallback } from "./c
1010
const passReqToCallback = false;
1111

1212
@Injectable()
13-
export class ApiKeyStrategy extends PassportStrategy(
14-
HeaderAPIKeyStrategy,
15-
ApiKeyStrategyName
16-
) {
17-
constructor(
18-
private authService: AuthService,
19-
private permissionService: PermissionService
20-
) {
21-
super(
22-
{
23-
header: ApiKeyHeader,
24-
prefix: "",
25-
},
26-
passReqToCallback
27-
);
28-
}
13+
export class ApiKeyStrategy extends PassportStrategy(HeaderAPIKeyStrategy, ApiKeyStrategyName) {
14+
constructor(private authService: AuthService, private permissionService: PermissionService) {
15+
super(
16+
{
17+
header: ApiKeyHeader,
18+
prefix: "",
19+
},
20+
passReqToCallback
21+
);
22+
}
2923

30-
async validate(
31-
apiKey: string,
32-
_done: HeaderApiVerifiedCallback
33-
): Promise<AuthenticatedUser> {
34-
const apiKeyDb = await this.authService.validateApiKey(apiKey);
35-
if (!apiKeyDb) {
36-
throw new UnauthorizedException(ErrorCodes.ApiKeyAuthFailed);
37-
}
24+
async validate(apiKey: string, _done: HeaderApiVerifiedCallback): Promise<AuthenticatedUser> {
25+
const apiKeyDb = await this.authService.validateApiKey(apiKey);
26+
if (!apiKeyDb) {
27+
throw new UnauthorizedException(ErrorCodes.ApiKeyAuthFailed);
28+
}
3829

39-
// Get the permissions and the UserID from the API Key instead of the user
40-
const permissions = await this.permissionService.findPermissionGroupedByLevelForApiKey(
41-
apiKeyDb.id
42-
);
30+
// Get the permissions and the UserID from the API Key instead of the user
31+
const permissions = await this.permissionService.findPermissionGroupedByLevelForApiKey(apiKeyDb.id);
4332

44-
// const permissions = dbApiKey.permissions as Permission[];
45-
const userId = apiKeyDb.systemUser.id;
33+
// const permissions = dbApiKey.permissions as Permission[];
34+
const userId = apiKeyDb.systemUser.id;
4635

47-
// Set the permissions and the userId on the returned user
48-
const user: AuthenticatedUser = {
49-
userId,
50-
username: apiKeyDb.systemUser.name,
51-
permissions,
52-
};
36+
// Set the permissions and the userId on the returned user
37+
const user: AuthenticatedUser = {
38+
userId,
39+
username: apiKeyDb.systemUser.name,
40+
permissions,
41+
};
5342

54-
return user;
55-
}
43+
return user;
44+
}
5645
}

src/auth/constants.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { AuthenticatedUser } from "@dto/internal/authenticated-user";
22

33
export type HeaderApiVerifiedCallback = (
4-
err: Error | null,
5-
user?: AuthenticatedUser,
6-
info?: Record<string, unknown>
4+
err: Error | null,
5+
user?: AuthenticatedUser,
6+
info?: Record<string, unknown>
77
) => void;
88

99
export const ApiKeyStrategyName = "api-key";

src/auth/custom-exception-filter.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ import { RedirectingException } from "./redirecting-exception";
44

55
@Catch(HttpException)
66
export class CustomExceptionFilter implements ExceptionFilter {
7-
catch(exception: HttpException, host: ArgumentsHost): void {
8-
const ctx = host.switchToHttp();
9-
const response = ctx.getResponse<Response>();
10-
const request = ctx.getRequest<Request>();
11-
const status = exception.getStatus();
7+
catch(exception: HttpException, host: ArgumentsHost): void {
8+
const ctx = host.switchToHttp();
9+
const response = ctx.getResponse<Response>();
10+
const request = ctx.getRequest<Request>();
11+
const status = exception.getStatus();
1212

13-
if (status == 302) {
14-
const asRedirectingException = exception as RedirectingException;
15-
return response.redirect(asRedirectingException.url);
16-
}
17-
18-
response.status(status).json({
19-
statusCode: status,
20-
timestamp: new Date().toISOString(),
21-
path: request.url,
22-
});
13+
if (status == 302) {
14+
const asRedirectingException = exception as RedirectingException;
15+
return response.redirect(asRedirectingException.url);
2316
}
17+
18+
response.status(status).json({
19+
statusCode: status,
20+
timestamp: new Date().toISOString(),
21+
path: request.url,
22+
});
23+
}
2424
}

src/auth/handle-redirect-url-parameter.middleware.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ import { Request, Response } from "express";
33

44
@Injectable()
55
export class HandleRedirectUrlParameterMiddleware implements NestMiddleware {
6-
private readonly logger = new Logger(HandleRedirectUrlParameterMiddleware.name);
6+
private readonly logger = new Logger(HandleRedirectUrlParameterMiddleware.name);
77

8-
// eslint-disable-next-line @typescript-eslint/ban-types
9-
use(req: Request, res: Response, next: Function): void {
10-
const redirectParam = req.query["redirect"];
11-
if (redirectParam) {
12-
this.logger.debug(`Has 'redirect' param: ${redirectParam}`);
13-
res.cookie("redirect", redirectParam, {
14-
expires: new Date(Date.now() + 900000),
15-
});
16-
}
17-
18-
next();
8+
// eslint-disable-next-line @typescript-eslint/ban-types
9+
use(req: Request, res: Response, next: Function): void {
10+
const redirectParam = req.query["redirect"];
11+
if (redirectParam) {
12+
this.logger.debug(`Has 'redirect' param: ${redirectParam}`);
13+
res.cookie("redirect", redirectParam, {
14+
expires: new Date(Date.now() + 900000),
15+
});
1916
}
17+
18+
next();
19+
}
2020
}

src/auth/jwt.strategy.ts

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,51 +10,46 @@ import { JwtStrategyName } from "./constants";
1010

1111
@Injectable()
1212
export class JwtStrategy extends PassportStrategy(Strategy, JwtStrategyName) {
13-
constructor(
14-
private permissionService: PermissionService,
15-
private userService: UserService,
16-
private configService: ConfigService
17-
) {
18-
super({
19-
// Configure the strategy to look for the JWT token in the Authorization header
20-
jwtFromRequest: ExtractJwt.fromExtractors([
21-
ExtractJwt.fromAuthHeaderAsBearerToken(),
22-
ExtractJwt.fromUrlQueryParameter("secret_token"),
23-
]),
24-
ignoreExpiration: false,
25-
secretOrKey: configService.get<string>("jwt.secret"),
26-
});
27-
}
28-
private readonly logger = new Logger(JwtStrategy.name);
29-
30-
private readonly NAME_ID_FORMAT =
31-
"urn:oasis:names:tc:SAML:1.1:nameid-format:X509SubjectName";
13+
constructor(
14+
private permissionService: PermissionService,
15+
private userService: UserService,
16+
private configService: ConfigService
17+
) {
18+
super({
19+
// Configure the strategy to look for the JWT token in the Authorization header
20+
jwtFromRequest: ExtractJwt.fromExtractors([
21+
ExtractJwt.fromAuthHeaderAsBearerToken(),
22+
ExtractJwt.fromUrlQueryParameter("secret_token"),
23+
]),
24+
ignoreExpiration: false,
25+
secretOrKey: configService.get<string>("jwt.secret"),
26+
});
27+
}
28+
private readonly logger = new Logger(JwtStrategy.name);
3229

33-
async validate(payload: JwtPayloadDto): Promise<AuthenticatedUser> {
34-
// Does the user still exist?
35-
const exists = await this.userService.findOne(payload.sub);
36-
if (!exists) {
37-
this.logger.warn(
38-
`Authorization for user with id: ${payload.sub} failed, since they no longer exists`
39-
);
40-
throw new UnauthorizedException();
41-
}
30+
private readonly NAME_ID_FORMAT = "urn:oasis:names:tc:SAML:1.1:nameid-format:X509SubjectName";
4231

43-
const result: AuthenticatedUser = {
44-
userId: payload.sub,
45-
username: payload.username,
46-
};
32+
async validate(payload: JwtPayloadDto): Promise<AuthenticatedUser> {
33+
// Does the user still exist?
34+
const exists = await this.userService.findOne(payload.sub);
35+
if (!exists) {
36+
this.logger.warn(`Authorization for user with id: ${payload.sub} failed, since they no longer exists`);
37+
throw new UnauthorizedException();
38+
}
4739

48-
if (exists.nameId) {
49-
// Add SAML stuff
50-
result.nameID = exists.nameId;
51-
result.nameIDFormat = this.NAME_ID_FORMAT;
52-
}
53-
// This data is already validated
54-
result.permissions = await this.permissionService.findPermissionGroupedByLevelForUser(
55-
payload.sub
56-
);
40+
const result: AuthenticatedUser = {
41+
userId: payload.sub,
42+
username: payload.username,
43+
};
5744

58-
return result;
45+
if (exists.nameId) {
46+
// Add SAML stuff
47+
result.nameID = exists.nameId;
48+
result.nameIDFormat = this.NAME_ID_FORMAT;
5949
}
50+
// This data is already validated
51+
result.permissions = await this.permissionService.findPermissionGroupedByLevelForUser(payload.sub);
52+
53+
return result;
54+
}
6055
}

src/auth/kombit-auth.guard.ts

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,32 @@
11
import { ErrorCodes } from "@enum/error-codes.enum";
2-
import {
3-
ExecutionContext,
4-
Injectable,
5-
Logger,
6-
UnauthorizedException,
7-
} from "@nestjs/common";
2+
import { ExecutionContext, Injectable, Logger, UnauthorizedException } from "@nestjs/common";
83
import { AuthGuard } from "@nestjs/passport";
94
import { Request as expressRequest, Response } from "express";
105
import { RedirectingException } from "./redirecting-exception";
116

127
@Injectable()
138
export class KombitAuthGuard extends AuthGuard("kombit") {
14-
constructor() {
15-
super();
16-
}
9+
constructor() {
10+
super();
11+
}
1712

18-
private readonly logger = new Logger(KombitAuthGuard.name);
13+
private readonly logger = new Logger(KombitAuthGuard.name);
1914

20-
handleRequest(
21-
err: any,
22-
user: any,
23-
info: any,
24-
context: ExecutionContext,
25-
status: any
26-
) {
27-
if (err || !user) {
28-
const req: expressRequest = context.switchToHttp().getRequest();
29-
const res: Response = context.switchToHttp().getResponse();
30-
const redirectTarget = req.cookies["redirect"];
31-
this.logger.error(`Login with KOMBIT failed, got error: ${err}`, err);
15+
handleRequest(err: any, user: any, info: any, context: ExecutionContext, status: any) {
16+
if (err || !user) {
17+
const req: expressRequest = context.switchToHttp().getRequest();
18+
const res: Response = context.switchToHttp().getResponse();
19+
const redirectTarget = req.cookies["redirect"];
20+
this.logger.error(`Login with KOMBIT failed, got error: ${err}`, err);
3221

33-
if (redirectTarget) {
34-
const redirectError =
35-
err?.message == ErrorCodes.UserInactive
36-
? ErrorCodes.UserInactive
37-
: ErrorCodes.KOMBITLoginFailed;
38-
throw new RedirectingException(
39-
`${redirectTarget}?error=${redirectError}`
40-
);
41-
} else {
42-
throw new UnauthorizedException(ErrorCodes.MissingRole);
43-
}
44-
}
45-
return user;
22+
if (redirectTarget) {
23+
const redirectError =
24+
err?.message == ErrorCodes.UserInactive ? ErrorCodes.UserInactive : ErrorCodes.KOMBITLoginFailed;
25+
throw new RedirectingException(`${redirectTarget}?error=${redirectError}`);
26+
} else {
27+
throw new UnauthorizedException(ErrorCodes.MissingRole);
28+
}
4629
}
30+
return user;
31+
}
4732
}

0 commit comments

Comments
 (0)