Skip to content

Commit

Permalink
Added unit tests and JSDoc comments for public properties
Browse files Browse the repository at this point in the history
Added example in README file
  • Loading branch information
JonWallsten committed Oct 4, 2024
1 parent 1613fe0 commit 6b75152
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 2 deletions.
44 changes: 44 additions & 0 deletions packages/aws-cdk-lib/aws-apigatewayv2-authorizers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,47 @@ import software.amazon.awscdk.aws_apigatewayv2_authorizers.*;
// If you want to import a specific construct
import software.amazon.awscdk.aws_apigatewayv2_authorizers.WebSocketIamAuthorizer;
```

## Import an existing HTTP Authorizer
If you want to import av existing HTTP Authorizer you can retrieve the authorizer id once it has been bound to a route, store it where you prefer, and then pass the values to

`HttpAuthorizer.fromHttpAuthorizerAttributes`

```ts
import { HttpLambdaAuthorizer, HttpLambdaResponseType } from 'aws-cdk-lib/aws-apigatewayv2-authorizers';
import { HttpUrlIntegration } from 'aws-cdk-lib/aws-apigatewayv2-integrations';
import { CfnOutput } from 'aws-cdk-lib';

// This function handles your auth logic
declare const authHandler: lambda.Function;

const authorizer = new HttpLambdaAuthorizer('BooksAuthorizer', authHandler, {
responseTypes: [HttpLambdaResponseType.SIMPLE], // Define if returns simple and/or iam response
});

const api = new apigwv2.HttpApi(this, 'HttpApi');

api.addRoutes({
integration: new HttpUrlIntegration('BooksIntegration', 'https://get-books-proxy.example.com'),
path: '/books',
authorizer,
});

// You can only access authorizerId after it's been bound to a route
// In this example we expect use CfnOutput
new CfnOutput(stack, 'authorizerId', authorizer.authorizerId);
new CfnOutput(stack, 'authorizerType', authorizer.authorizerType);
```

```ts
import { HttpAuthorizer } from 'aws-cdk-lib/aws-apigatewayv2';
import { Fn } from 'aws-cdk-lib'

const authorizerId = Fn.importValue('authorizerId');
const authorizerType = Fn.importValue('authorizerType');

const authorizer = HttpAuthorizer.fromHttpAuthorizerAttributes(stack, '', {
authorizerId,
authorizerType
});
```
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import {
* Authorize HTTP API Routes with IAM
*/
export class HttpIamAuthorizer implements IHttpRouteAuthorizer {
/**
* The authorizationType used for IAM Authorizer
*/
public readonly authorizationType = HttpAuthorizerType.IAM;
public bind(_options: HttpRouteAuthorizerBindOptions): HttpRouteAuthorizerConfig {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export interface HttpJwtAuthorizerProps {
*/
export class HttpJwtAuthorizer implements IHttpRouteAuthorizer {
private authorizer?: HttpAuthorizer;
/**
* The authorizationType used for JWT Authorizer
*/
public readonly authorizationType = 'JWT';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ export interface HttpLambdaAuthorizerProps {
export class HttpLambdaAuthorizer implements IHttpRouteAuthorizer {
private authorizer?: HttpAuthorizer;
private httpApi?: IHttpApi;

/**
* The authorizationType used for Lambda Authorizer
*/
public readonly authorizationType = 'CUSTOM';

/**
* Initialize a lambda authorizer to be bound with HTTP route.
* @param id The id of the underlying construct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ export interface HttpUserPoolAuthorizerProps {
*/
export class HttpUserPoolAuthorizer implements IHttpRouteAuthorizer {
private authorizer?: HttpAuthorizer;
/**
* The authorizationType used for UserPool Authorizer
*/
public readonly authorizationType = 'JWT';
/**
* Initialize a Cognito user pool authorizer to be bound with HTTP route.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,37 @@ describe('HttpJwtAuthorizer', () => {
// THEN
Template.fromStack(stack).resourceCountIs('AWS::ApiGatewayV2::Authorizer', 1);
});

test('should expose authorizer id after authorizer has been bound to route', () => {
// GIVEN
const stack = new Stack();
const api = new HttpApi(stack, 'HttpApi');
const authorizer = new HttpJwtAuthorizer('BooksAuthorizer', 'https://test.us.auth0.com', {
jwtAudience: ['3131231'],
});

// WHEN
api.addRoutes({
integration: new DummyRouteIntegration(),
path: '/books',
authorizer,
});

// THEN
expect(authorizer.authorizerId).toBeDefined();
});

test('should throw error when acessing authorizer before it been bound to route', () => {
// GIVEN
const stack = new Stack();
const t = () => {
const authorizer = new HttpJwtAuthorizer('BooksAuthorizer', 'https://test.us.auth0.com', {
jwtAudience: ['3131231'],
});
const authorizerId = authorizer.authorizerId;
};

// THEN
expect(t).toThrow(Error);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,11 @@ describe('HttpLambdaAuthorizer', () => {
});

const t = () => {
new HttpLambdaAuthorizer('BooksAuthorizer', handler);
const authorizer = new HttpLambdaAuthorizer('BooksAuthorizer', handler);
const authorizerId = authorizer.authorizerId;
};

// THEN
expect(t).toThrow(TypeError);
expect(t).toThrow(Error);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,44 @@ describe('HttpUserPoolAuthorizer', () => {
},
});
});

test('should expose authorizer id after authorizer has been bound to route', () => {
// GIVEN
const stack = new Stack();
const api = new HttpApi(stack, 'HttpApi');
const userPool = new UserPool(stack, 'UserPool');
const userPoolClient1 = userPool.addClient('UserPoolClient1');
const userPoolClient2 = userPool.addClient('UserPoolClient2');
const authorizer = new HttpUserPoolAuthorizer('BooksAuthorizer', userPool, {
userPoolClients: [userPoolClient1, userPoolClient2],
});

// WHEN
api.addRoutes({
integration: new DummyRouteIntegration(),
path: '/books',
authorizer,
});

// THEN
expect(authorizer.authorizerId).toBeDefined();
});

test('should throw error when acessing authorizer before it been bound to route', () => {
// GIVEN
const stack = new Stack();
const userPool = new UserPool(stack, 'UserPool');
const userPoolClient1 = userPool.addClient('UserPoolClient1');
const userPoolClient2 = userPool.addClient('UserPoolClient2');

const t = () => {
const authorizer = new HttpUserPoolAuthorizer('BooksAuthorizer', userPool, {
userPoolClients: [userPoolClient1, userPoolClient2],
});
const authorizerId = authorizer.authorizerId;
};

// THEN
expect(t).toThrow(Error);
});
});

0 comments on commit 6b75152

Please sign in to comment.