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

Fix/error handling #55

Merged
merged 2 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion api/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,27 @@ export class FsaeApiApplication extends BootMixin(

// Set up the custom sequence
this.sequence(MySequence);

// Add the security scheme to the OpenAPI specification
this.api({
openapi: '3.0.0',
info: {title: 'MyApp', version: '1.0.0'},
paths: {},
components: {
securitySchemes: {
// Define the security scheme (e.g., JWT bearer token)
bearerAuth: {
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
},
},
},
security: [
{
bearerAuth: [],
},
],
});
// Set up default home page
this.static('/', path.join(__dirname, '../public'));

Expand Down
8 changes: 4 additions & 4 deletions api/src/auth/auth-strategies/jwt-strategy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {AuthenticationStrategy} from '@loopback/authentication';
import {UserProfile} from '@loopback/security';
import {RedirectRoute, Request} from '@loopback/rest';
import {HttpErrors, RedirectRoute, Request} from '@loopback/rest';
import {inject} from '@loopback/core';
import {JwtService} from '../../services';

Expand All @@ -19,18 +19,18 @@ export class FSAEJwtStrategy implements AuthenticationStrategy {

private extractTokenFromRequest(request: Request): string {
if (!request.headers.authorization) {
throw new Error('Authorization header not found');
throw new HttpErrors.Unauthorized("Authorization header not found")
}

const authHeaderValue = request.headers.authorization;

if(!authHeaderValue.startsWith('Bearer')) {
throw new Error('Authorization header is not of type Bearer. "Bearer <<Token>>"');
throw new HttpErrors.Unauthorized('Authorization header is not of type Bearer. "Bearer <<Token>>"');
}

const parts = authHeaderValue.split(' '); // Splits 'Bearer <Token>'
if (parts.length !== 2) {
throw new Error('Authorization header value has too many parts. It must follow the pattern: \'Bearer xx.yy.zz\' where xx.yy.zz is a valid JWT token.');
throw new HttpErrors.BadRequest('Authorization header value has too many parts. It must follow the pattern: \'Bearer xx.yy.zz\' where xx.yy.zz is a valid JWT token.');
}
const token = parts[1];
return token;
Expand Down
2 changes: 1 addition & 1 deletion api/src/services/jwt.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class JwtService implements TokenService{

verifyToken(token: string): Promise<any> {
if (!token) {
throw new Error('Error verifying Token. Token cannot be null');
throw new HttpErrors.Unauthorized('Error verifying Token. Token cannot be null');
}

let securityUserProfile: UserProfile;
Expand Down
5 changes: 0 additions & 5 deletions api/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2252,11 +2252,6 @@ fs.realpath@^1.0.0:
resolved "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz"
integrity sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==

fsevents@~2.3.2:
version "2.3.3"
resolved "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz"
integrity sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==

function-bind@^1.1.2:
version "1.1.2"
resolved "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz"
Expand Down
Loading