Skip to content

Commit

Permalink
BC-6453 - allow also ONLY jwt in authorization header
Browse files Browse the repository at this point in the history
  • Loading branch information
bergatco committed Jun 11, 2024
1 parent a18eeed commit ee2b0e8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,35 @@ describe(AuthorizationClientAdapter.name, () => {
);
});

it('should forward the JWT token from authorization header even without Bearer token', async () => {
setup();

const request = createMock<Request>({
headers: {
authorization: jwtToken,
},
});

const adapter = new AuthorizationClientAdapter(authorizationApi, request);

const params = {
context: {
action: Action.READ,
requiredPermissions: [],
},
referenceType: AuthorizationBodyParamsReferenceType.COURSES,
referenceId: 'someReferenceId',
};
const expectedOptions = { headers: { authorization: `Bearer ${jwtToken}` } };

await adapter.hasPermissionByReferences(params);

expect(authorizationApi.authorizationReferenceControllerAuthorizeByReference).toHaveBeenCalledWith(
params,
expectedOptions
);
});

it('should throw an UnauthorizedException if no JWT token is found', async () => {
const request = createMock<Request>({
headers: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ export class AuthorizationClientAdapter {

private getJWT(): string {
const getJWT = ExtractJwt.fromExtractors([ExtractJwt.fromAuthHeaderAsBearerToken(), this.fromCookie('jwt')]);
let jwt = getJWT(this.request) || this.request.headers.authorization;
if (jwt?.toLowerCase()?.startsWith('bearer ')) {
[, jwt] = jwt.split(' ');
}
const jwt = getJWT(this.request) || this.request.headers.authorization;

if (!jwt) {
throw new UnauthorizedException('Authentication is required.');
Expand Down

0 comments on commit ee2b0e8

Please sign in to comment.