Skip to content

Commit 621588e

Browse files
committed
Add sample demonstrating passed withAwsSignature variable works
1 parent 09d18ce commit 621588e

File tree

31 files changed

+1785
-2
lines changed

31 files changed

+1785
-2
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
generatorName: typescript-axios
2+
outputDir: samples/client/petstore/typescript-axios/builds/with-aws-iam
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/typescript-axios/with-aws-iam.yaml
4+
templateDir: modules/openapi-generator/src/main/resources/typescript-axios
5+
additionalProperties:
6+
npmVersion: 1.0.0
7+
npmName: '@openapitools/typescript-axios-with-aws-iam'
8+
npmRepository: https://skimdb.npmjs.com/registry
9+
supportsES6: true
10+
withNodeImports: true
11+
withSeparateModelsAndApi: true
12+
apiPackage: client
13+
modelPackage: models
14+
enumPropertyNaming: UPPERCASE

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptAxiosClientCodegen.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ private boolean isAwsV4SecurityScheme(String schemeName, SecurityScheme scheme,
161161
// Pattern 1: Check for AWS-specific extension
162162
if (scheme.getExtensions() != null) {
163163
Object authType = scheme.getExtensions().get("x-amazon-apigateway-authtype");
164-
if ("awsSigv4".equals(authType) || "aws_iam".equals(authType)) {
164+
if ("awsSigv4".equals(authType)) {
165165
return true;
166166
}
167167
}

modules/openapi-generator/src/main/resources/typescript-axios/package.mustache

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
"prepare": "npm run build"
2727
},
2828
"dependencies": {
29-
"axios": "{{axiosVersion}}"
29+
"axios": "{{axiosVersion}}"{{#withAWSV4Signature}},
30+
"aws4": "^1.11.0",
31+
"@aws-sdk/credential-provider-node": "^3.400.0"{{/withAWSV4Signature}}
3032
},
3133
"devDependencies": {
3234
"@types/node": "12.11.5 - 12.20.42",
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
openapi: 3.0.1
2+
info:
3+
title: OpenAPI Petstore with AWS IAM
4+
version: 1.0.0
5+
description: Test API for AWS IAM authentication detection
6+
servers:
7+
- url: https://abc123.execute-api.us-east-1.amazonaws.com/prod
8+
paths:
9+
/pet:
10+
get:
11+
summary: Find pet by ID
12+
operationId: getPetById
13+
security:
14+
- aws_iam: []
15+
responses:
16+
'200':
17+
description: successful operation
18+
content:
19+
application/json:
20+
schema:
21+
$ref: '#/components/schemas/Pet'
22+
'404':
23+
description: Pet not found
24+
post:
25+
summary: Create a new pet
26+
operationId: createPet
27+
security:
28+
- aws_iam: []
29+
requestBody:
30+
required: true
31+
content:
32+
application/json:
33+
schema:
34+
$ref: '#/components/schemas/Pet'
35+
responses:
36+
'201':
37+
description: Pet created
38+
content:
39+
application/json:
40+
schema:
41+
$ref: '#/components/schemas/Pet'
42+
'400':
43+
description: Invalid input
44+
/store/inventory:
45+
get:
46+
summary: Returns pet inventories by status
47+
operationId: getInventory
48+
security:
49+
- sigv4_auth: []
50+
responses:
51+
'200':
52+
description: successful operation
53+
content:
54+
application/json:
55+
schema:
56+
type: object
57+
additionalProperties:
58+
type: integer
59+
format: int32
60+
components:
61+
securitySchemes:
62+
aws_iam:
63+
type: apiKey
64+
name: Authorization
65+
in: header
66+
x-amazon-apigateway-authtype: awsSigv4
67+
sigv4_auth:
68+
type: apiKey
69+
name: Authorization
70+
in: header
71+
schemas:
72+
Pet:
73+
type: object
74+
required:
75+
- name
76+
- photoUrls
77+
properties:
78+
id:
79+
type: integer
80+
format: int64
81+
name:
82+
type: string
83+
example: doggie
84+
photoUrls:
85+
type: array
86+
items:
87+
type: string
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
## @openapitools/typescript-axios-with-aws-iam@1.0.0
2+
3+
This generator creates TypeScript/JavaScript client that utilizes [axios](https://github.com/axios/axios). The generated Node module can be used in the following environments:
4+
5+
Environment
6+
* Node.js
7+
* Webpack
8+
* Browserify
9+
10+
Language level
11+
* ES5 - you must have a Promises/A+ library installed
12+
* ES6
13+
14+
Module system
15+
* CommonJS
16+
* ES6 module system
17+
18+
It can be used in both TypeScript and JavaScript. In TypeScript, the definition will be automatically resolved via `package.json`. ([Reference](https://www.typescriptlang.org/docs/handbook/declaration-files/consumption.html))
19+
20+
### Building
21+
22+
To build and compile the typescript sources to javascript use:
23+
```
24+
npm install
25+
npm run build
26+
```
27+
28+
### Publishing
29+
30+
First build the package then run `npm publish`
31+
32+
### Consuming
33+
34+
navigate to the folder of your consuming project and run one of the following commands.
35+
36+
_published:_
37+
38+
```
39+
npm install @openapitools/typescript-axios-with-aws-iam@1.0.0 --save
40+
```
41+
42+
_unPublished (not recommended):_
43+
44+
```
45+
npm install PATH_TO_GENERATED_PACKAGE --save
46+
```
47+
48+
### Documentation for API Endpoints
49+
50+
All URIs are relative to *https://abc123.execute-api.us-east-1.amazonaws.com/prod*
51+
52+
Class | Method | HTTP request | Description
53+
------------ | ------------- | ------------- | -------------
54+
*DefaultApi* | [**createPet**](docs/DefaultApi.md#createpet) | **POST** /pet | Create a new pet
55+
*DefaultApi* | [**getInventory**](docs/DefaultApi.md#getinventory) | **GET** /store/inventory | Returns pet inventories by status
56+
*DefaultApi* | [**getPetById**](docs/DefaultApi.md#getpetbyid) | **GET** /pet | Find pet by ID
57+
58+
59+
### Documentation For Models
60+
61+
- [Pet](docs/Pet.md)
62+
63+
64+
<a id="documentation-for-authorization"></a>
65+
## Documentation For Authorization
66+
67+
68+
Authentication schemes defined for the API:
69+
<a id="aws_iam"></a>
70+
### aws_iam
71+
72+
- **Type**: API key
73+
- **API key parameter name**: Authorization
74+
- **Location**: HTTP header
75+
76+
<a id="sigv4_auth"></a>
77+
### sigv4_auth
78+
79+
- **Type**: API key
80+
- **API key parameter name**: Authorization
81+
- **Location**: HTTP header
82+
83+
84+
<a id="aws-v4-signature"></a>
85+
### AWS V4 Signature
86+
87+
This client supports AWS Signature Version 4 for authenticating requests to AWS services.
88+
89+
#### Configuration
90+
91+
```typescript
92+
import { Configuration } from '@openapitools/typescript-axios-with-aws-iam';
93+
94+
const configuration = new Configuration({
95+
awsv4: {
96+
credentials: {
97+
accessKeyId: 'your-access-key-id',
98+
secretAccessKey: 'your-secret-access-key',
99+
sessionToken: 'your-session-token' // Optional, for temporary credentials
100+
},
101+
options: {
102+
region: 'us-east-1', // AWS region
103+
service: 'execute-api' // AWS service name, typically 'execute-api' for API Gateway
104+
}
105+
}
106+
});
107+
```
108+
109+
#### Environment Variables
110+
111+
You can also use environment variables for AWS credentials:
112+
113+
```bash
114+
export AWS_ACCESS_KEY_ID=your-access-key-id
115+
export AWS_SECRET_ACCESS_KEY=your-secret-access-key
116+
export AWS_SESSION_TOKEN=your-session-token # Optional
117+
```
118+
119+
When environment variables are set, they will be used as fallbacks if not provided in the configuration.
120+
121+
#### Usage Example
122+
123+
```typescript
124+
import { DefaultApi, Configuration } from '@openapitools/typescript-axios-with-aws-iam';
125+
126+
const configuration = new Configuration({
127+
basePath: 'https://your-api-gateway-id.execute-api.us-east-1.amazonaws.com/stage',
128+
awsv4: {
129+
credentials: {
130+
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
131+
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
132+
sessionToken: process.env.AWS_SESSION_TOKEN
133+
},
134+
options: {
135+
region: 'us-east-1',
136+
service: 'execute-api'
137+
}
138+
}
139+
});
140+
141+
const apiInstance = new DefaultApi(configuration);
142+
143+
// All requests will now be signed with AWS V4 signature
144+
apiInstance.someMethod().then((response) => {
145+
console.log(response.data);
146+
}).catch((error) => {
147+
console.error(error);
148+
});
149+
```
150+
151+
#### IAM Permissions
152+
153+
Ensure your AWS credentials have the necessary IAM permissions to access the API endpoints. For API Gateway, this typically includes:
154+
155+
```json
156+
{
157+
"Version": "2012-10-17",
158+
"Statement": [
159+
{
160+
"Effect": "Allow",
161+
"Action": [
162+
"execute-api:Invoke"
163+
],
164+
"Resource": "arn:aws:execute-api:region:account-id:api-id/*"
165+
}
166+
]
167+
}
168+
```
169+

0 commit comments

Comments
 (0)