Make the OpenAPI 3.0 support HTTP Bearer scheme (#1953)

This commit is contained in:
YMHuang 2020-03-12 06:41:18 +08:00 committed by GitHub
parent 34c5a79338
commit 327eb5b692
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 201 additions and 127 deletions

View File

@ -10,6 +10,7 @@ Insomnia v3 format.
- cURL
- HTTP Archive Format 1.2 (HAR)
- Swagger 2.0
- OpenAPI 3.0
## Installation
@ -20,7 +21,7 @@ npm install -g insomnia-importers
```
For programmatic usage, install in project
```bash
npm install --save insomnia-importers
```
@ -34,10 +35,10 @@ insomnia-import /path/to/har-export.json > insomnia-export.json
## Programmatic Usage
```javascript
const importers = require('insomnia-importers')
const importers = require('insomnia-importers');
// Convert a Curl command
const output = importers.convert('curl -X POST https://insomnia.rest --data "Cool!"')
const output = importers.convert('curl -X POST https://insomnia.rest --data "Cool!"');
// Pretty print the result
console.log(JSON.stringify(output.data, null, 2));

View File

@ -10,6 +10,9 @@ components:
Basic:
type: http
scheme: basic
Bearer:
type: http
scheme: bearer
Key-Header:
type: apiKey
name: x-api_key
@ -31,6 +34,13 @@ paths:
responses:
'200':
description: OK
/bearer:
get:
security:
- Bearer: []
responses:
'200':
description: OK
/key/header:
get:
security:

View File

@ -1,141 +1,158 @@
{
"__export_date": "2019-10-12T17:07:14.568Z",
"__export_format": 4,
"__export_source": "insomnia.importers:v0.1.0",
"_type": "export",
"__export_format": 4,
"__export_date": "2020-02-19T03:47:33.193Z",
"__export_source": "insomnia.importers:v0.1.0",
"resources": [
{
"_id": "__WORKSPACE_ID__",
"_type": "workspace",
"description": "",
"_id": "__WORKSPACE_ID__",
"parentId": null,
"name": "Endpoint Security 1.2",
"parentId": null
"description": ""
},
{
"_id": "__BASE_ENVIRONMENT_ID__",
"_type": "environment",
"parentId": "__WORKSPACE_ID__",
"name": "Base environment",
"data": {
"base_url": "{{ scheme }}://{{ host }}{{ base_path }}"
},
"name": "Base environment",
"parentId": "__WORKSPACE_ID__"
"_type": "environment",
"_id": "__BASE_ENVIRONMENT_ID__"
},
{
"_id": "env___BASE_ENVIRONMENT_ID___sub",
"_type": "environment",
"parentId": "__BASE_ENVIRONMENT_ID__",
"name": "OpenAPI env",
"data": {
"apiKey": "apiKey",
"scheme": "https",
"base_path": "/v1",
"host": "api.server.test",
"httpPassword": "password",
"apiKey": "apiKey",
"httpUsername": "username",
"scheme": "https"
"httpPassword": "password",
"bearerToken": "bearerToken"
},
"name": "OpenAPI env",
"parentId": "__BASE_ENVIRONMENT_ID__"
"_type": "environment",
"_id": "env___BASE_ENVIRONMENT_ID___sub"
},
{
"_id": "req___WORKSPACE_ID__4a563129",
"_type": "request",
"authentication": {
"password": "{{ httpPassword }}",
"type": "basic",
"username": "{{ httpUsername }}"
},
"body": {},
"headers": [],
"method": "GET",
"name": "/basic",
"parameters": [],
"parentId": "__WORKSPACE_ID__",
"url": "{{ base_url }}/basic"
"name": "/basic",
"url": "{{ base_url }}/basic",
"body": {},
"method": "GET",
"parameters": [],
"headers": [],
"authentication": {
"type": "basic",
"username": "{{ httpUsername }}",
"password": "{{ httpPassword }}"
},
"_type": "request",
"_id": "req___WORKSPACE_ID__4a563129"
},
{
"_id": "req___WORKSPACE_ID__48bba8a5",
"_type": "request",
"authentication": {},
"parentId": "__WORKSPACE_ID__",
"name": "/bearer",
"url": "{{ base_url }}/bearer",
"body": {},
"method": "GET",
"parameters": [],
"headers": [],
"authentication": {
"type": "bearer",
"token": "{{bearerToken}}",
"prefix": ""
},
"_type": "request",
"_id": "req___WORKSPACE_ID__6ecf1fc2"
},
{
"parentId": "__WORKSPACE_ID__",
"name": "/key/header",
"url": "{{ base_url }}/key/header",
"body": {},
"method": "GET",
"parameters": [],
"headers": [
{
"disabled": false,
"name": "x-api_key",
"disabled": false,
"value": "{{ apiKey }}"
}
],
"method": "GET",
"name": "/key/header",
"parameters": [],
"parentId": "__WORKSPACE_ID__",
"url": "{{ base_url }}/key/header"
"authentication": {},
"_type": "request",
"_id": "req___WORKSPACE_ID__48bba8a5"
},
{
"_id": "req___WORKSPACE_ID__2ea006cf",
"_type": "request",
"authentication": {},
"parentId": "__WORKSPACE_ID__",
"name": "/key/cookie",
"url": "{{ base_url }}/key/cookie",
"body": {},
"method": "GET",
"parameters": [],
"headers": [
{
"disabled": false,
"name": "Cookie",
"disabled": false,
"value": "CookieName={{ apiKey }}"
}
],
"method": "GET",
"name": "/key/cookie",
"parameters": [],
"parentId": "__WORKSPACE_ID__",
"url": "{{ base_url }}/key/cookie"
"authentication": {},
"_type": "request",
"_id": "req___WORKSPACE_ID__2ea006cf"
},
{
"_id": "req___WORKSPACE_ID__0a8d5285",
"_type": "request",
"authentication": {},
"body": {},
"headers": [],
"method": "GET",
"parentId": "__WORKSPACE_ID__",
"name": "/key/query",
"url": "{{ base_url }}/key/query",
"body": {},
"method": "GET",
"parameters": [
{
"disabled": false,
"name": "key",
"disabled": false,
"value": "{{ apiKey }}"
}
],
"parentId": "__WORKSPACE_ID__",
"url": "{{ base_url }}/key/query"
"headers": [],
"authentication": {},
"_type": "request",
"_id": "req___WORKSPACE_ID__0a8d5285"
},
{
"_id": "req___WORKSPACE_ID__e285189c",
"_type": "request",
"authentication": {
"password": "{{ httpPassword }}",
"type": "basic",
"username": "{{ httpUsername }}"
},
"parentId": "__WORKSPACE_ID__",
"name": "/all",
"url": "{{ base_url }}/all",
"body": {},
"method": "GET",
"parameters": [
{
"name": "key",
"disabled": false,
"value": "{{ apiKey }}"
}
],
"headers": [
{
"disabled": false,
"name": "x-api_key",
"disabled": false,
"value": "{{ apiKey }}"
},
{
"disabled": false,
"name": "Cookie",
"disabled": false,
"value": "CookieName={{ apiKey }}"
}
],
"method": "GET",
"name": "/all",
"parameters": [
{
"disabled": false,
"name": "key",
"value": "{{ apiKey }}"
}
],
"parentId": "__WORKSPACE_ID__",
"url": "{{ base_url }}/all"
"authentication": {
"type": "basic",
"username": "{{ httpUsername }}",
"password": "{{ httpPassword }}"
},
"_type": "request",
"_id": "req___WORKSPACE_ID__e285189c"
}
]
}

View File

@ -10,6 +10,9 @@ components:
Basic:
type: http
scheme: basic
Bearer:
type: http
scheme: bearer
Key-Header:
type: apiKey
name: x-api_key

View File

@ -1,78 +1,79 @@
{
"__export_date": "2019-10-12T17:23:25.171Z",
"__export_format": 4,
"__export_source": "insomnia.importers:v0.1.0",
"_type": "export",
"__export_format": 4,
"__export_date": "2020-02-19T03:46:03.524Z",
"__export_source": "insomnia.importers:v0.1.0",
"resources": [
{
"_id": "__WORKSPACE_ID__",
"_type": "workspace",
"description": "",
"_id": "__WORKSPACE_ID__",
"parentId": null,
"name": "Global Security 1.2",
"parentId": null
"description": ""
},
{
"_id": "__BASE_ENVIRONMENT_ID__",
"_type": "environment",
"parentId": "__WORKSPACE_ID__",
"name": "Base environment",
"data": {
"base_url": "{{ scheme }}://{{ host }}{{ base_path }}"
},
"name": "Base environment",
"parentId": "__WORKSPACE_ID__"
"_type": "environment",
"_id": "__BASE_ENVIRONMENT_ID__"
},
{
"_id": "env___BASE_ENVIRONMENT_ID___sub",
"_type": "environment",
"parentId": "__BASE_ENVIRONMENT_ID__",
"name": "OpenAPI env",
"data": {
"apiKey": "apiKey",
"scheme": "https",
"base_path": "/v1",
"host": "api.server.test",
"httpPassword": "password",
"apiKey": "apiKey",
"httpUsername": "username",
"scheme": "https"
"httpPassword": "password",
"bearerToken": "bearerToken"
},
"name": "OpenAPI env",
"parentId": "__BASE_ENVIRONMENT_ID__"
"_type": "environment",
"_id": "env___BASE_ENVIRONMENT_ID___sub"
},
{
"_id": "req___WORKSPACE_ID__21946b60",
"_type": "request",
"authentication": {
"password": "{{ httpPassword }}",
"type": "basic",
"username": "{{ httpUsername }}"
},
"parentId": "__WORKSPACE_ID__",
"name": "/global",
"url": "{{ base_url }}/global",
"body": {},
"method": "GET",
"parameters": [],
"headers": [
{
"disabled": false,
"name": "x-api_key",
"disabled": false,
"value": "{{ apiKey }}"
}
],
"method": "GET",
"name": "/global",
"parameters": [],
"parentId": "__WORKSPACE_ID__",
"url": "{{ base_url }}/global"
"authentication": {
"type": "basic",
"username": "{{ httpUsername }}",
"password": "{{ httpPassword }}"
},
"_type": "request",
"_id": "req___WORKSPACE_ID__21946b60"
},
{
"_id": "req___WORKSPACE_ID__b410454b",
"_type": "request",
"authentication": {},
"body": {},
"headers": [],
"method": "GET",
"parentId": "__WORKSPACE_ID__",
"name": "/override",
"url": "{{ base_url }}/override",
"body": {},
"method": "GET",
"parameters": [
{
"disabled": false,
"name": "apiKeyHere",
"disabled": false,
"value": "{{ apiKey }}"
}
],
"parentId": "__WORKSPACE_ID__",
"url": "{{ base_url }}/override"
"headers": [],
"authentication": {},
"_type": "request",
"_id": "req___WORKSPACE_ID__b410454b"
}
]
}

View File

@ -17,7 +17,12 @@ const SECURITY_TYPE = {
OAUTH: 'oauth2',
OPEN_ID: 'openIdConnect',
};
const HTTP_AUTH_SCHEME = {
BASIC: 'basic',
BEARER: 'bearer',
};
const SUPPORTED_SECURITY_TYPES = [SECURITY_TYPE.HTTP, SECURITY_TYPE.API_KEY];
const SUPPORTED_HTTP_AUTH_SCHEMES = [HTTP_AUTH_SCHEME.BASIC, HTTP_AUTH_SCHEME.BEARER];
let requestCounts = {};
@ -262,7 +267,7 @@ function prepareHeaders(endpointSchema) {
*
* @param {Object} security - OpenAPI 3 security rules
* @param {Object} securitySchemes - OpenAPI 3 security schemes
* @returns {Object} headers or basic http authentication details
* @returns {Object} headers or basic|bearer http authentication details
*/
function parseSecurity(security, securitySchemes) {
if (!security || !securitySchemes) {
@ -308,11 +313,12 @@ function parseSecurity(security, securitySchemes) {
apiKeyHeaders.push(apiKeyCookieHeader);
}
const httpAuth = supportedSchemes.find(
scheme => scheme.type === SECURITY_TYPE.HTTP && scheme.scheme === 'basic',
)
? { type: 'basic', username: '{{ httpUsername }}', password: '{{ httpPassword }}' }
: {};
const httpAuthScheme = supportedSchemes.find(
scheme =>
scheme.type === SECURITY_TYPE.HTTP && SUPPORTED_HTTP_AUTH_SCHEMES.includes(scheme.scheme),
);
const httpAuth = httpAuthScheme ? parseHttpAuth(httpAuthScheme.scheme) : {};
return {
authentication: httpAuth,
@ -337,17 +343,26 @@ function getSecurityEnvVariables(securitySchemes) {
const hasApiKeyScheme = securitySchemesArray.some(
scheme => scheme.type === SECURITY_TYPE.API_KEY,
);
const hasHttpScheme = securitySchemesArray.some(scheme => scheme.type === SECURITY_TYPE.HTTP);
const hasHttpBasicScheme = securitySchemesArray.some(
scheme => scheme.type === SECURITY_TYPE.HTTP && scheme.scheme === 'basic',
);
const hasHttpBearerScheme = securitySchemesArray.some(
scheme => scheme.type === SECURITY_TYPE.HTTP && scheme.scheme === 'bearer',
);
if (hasApiKeyScheme) {
variables.apiKey = 'apiKey';
}
if (hasHttpScheme) {
if (hasHttpBasicScheme) {
variables.httpUsername = 'username';
variables.httpPassword = 'password';
}
if (hasHttpBearerScheme) {
variables.bearerToken = 'bearerToken';
}
return variables;
}
@ -510,3 +525,30 @@ function generateUniqueRequestId(endpointSchema) {
return `req_${WORKSPACE_ID}${hash}${requestCounts[hash] || ''}`;
}
function parseHttpAuth(scheme) {
switch (scheme) {
case HTTP_AUTH_SCHEME.BASIC:
return importBasicAuthentication();
case HTTP_AUTH_SCHEME.BEARER:
return importBearerAuthentication();
default:
return {};
}
}
function importBearerAuthentication() {
return {
type: 'bearer',
token: '{{bearerToken}}',
prefix: '',
};
}
function importBasicAuthentication() {
return {
type: 'basic',
username: '{{ httpUsername }}',
password: '{{ httpPassword }}',
};
}