Skip to content

Commit

Permalink
Merge pull request #6 from BrewInteractive/feature/create-GetAuthoriz…
Browse files Browse the repository at this point in the history
…ation-decorator

Feature/create get authorization decorator
  • Loading branch information
mfozmen authored May 28, 2024
2 parents c54ba6d + c27f608 commit 618750a
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 2 deletions.
76 changes: 76 additions & 0 deletions src/decorators/authorization-token.decorator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { Faker } from 'mockingbird';
import { AuthorizationToken } from './authorization-token.decorator';
import { ROUTE_ARGS_METADATA } from '@nestjs/common/constants';

function getParamDecoratorFactory() {
class TestDecorator {
public getDecoratorValue(@AuthorizationToken() value) {
console.log(value);
}
}

const args = Reflect.getMetadata(
ROUTE_ARGS_METADATA,
TestDecorator,
'getDecoratorValue',
);
return args[Object.keys(args)[0]].factory;
}

describe('AuthorizationToken Decorator', () => {
it('should return the authorization header', () => {
const token = Faker.datatype.string();
const mockRequest = {
headers: {
authorization: `Bearer ${token}`,
},
};

const mockContext = {
switchToHttp: jest.fn().mockReturnValue({
getRequest: jest.fn().mockReturnValue(mockRequest),
}),
};

const factory = getParamDecoratorFactory();
const authorization = factory('authorization', mockContext);

expect(authorization).toBe(token);
});

it('should return undefined if the token type is not bearer ', () => {
const mockRequest = {
headers: {
authorization: 'test mockToken',
},
};

const mockContext = {
switchToHttp: jest.fn().mockReturnValue({
getRequest: jest.fn().mockReturnValue(mockRequest),
}),
};

const factory = getParamDecoratorFactory();
const authorization = factory('authorization', mockContext);

expect(authorization).toBeUndefined();
});

it('should return undefined if no authorization header is present', () => {
const mockRequest = {
headers: {},
};

const mockContext = {
switchToHttp: jest.fn().mockReturnValue({
getRequest: jest.fn().mockReturnValue(mockRequest),
}),
};

const factory = getParamDecoratorFactory();
const authorization = factory('authorization', mockContext);

expect(authorization).toBeUndefined();
});
});
9 changes: 9 additions & 0 deletions src/decorators/authorization-token.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ExecutionContext, createParamDecorator } from '@nestjs/common';

export const AuthorizationToken = createParamDecorator(
(_: unknown, context: ExecutionContext): string => {
const request = context.switchToHttp().getRequest();
const [type, token] = request.headers['authorization']?.split(' ') ?? [];
return type === 'Bearer' ? token : undefined;
},
);
1 change: 1 addition & 0 deletions src/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './authorization-token.decorator';
2 changes: 1 addition & 1 deletion src/hasura.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ describe('HasuraService', () => {
expect(actualResult).toBe(expectedResult);
expect(graphqlClientSpy).toHaveBeenCalledWith(query, undefined, {
'x-hasura-role': authorizationOptions.role,
authorization: authorizationOptions.authorizationToken,
authorization: `Bearer ${authorizationOptions.authorizationToken}`,
});
});

Expand Down
3 changes: 2 additions & 1 deletion src/hasura.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export class HasuraService {
private createHeadersByAuthorizationOptions(options: AuthorizationOptions) {
return Object.entries(options).reduce((acc, [key, value]) => {
const headerKey = HasuraHeaders[key];
if (headerKey) acc[headerKey] = value;
if (headerKey === 'authorization') acc[headerKey] = `Bearer ${value}`;
else if (headerKey) acc[headerKey] = value;
return acc;
}, {});
}
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './hasura.module';
export * from './hasura.service';
export * from './models';
export * from './error';
export * from './decorators';

0 comments on commit 618750a

Please sign in to comment.