Respect the grant_type option in Postman imports with OAuth2 (#5270)

* fix grant_type casing and add support for PKCE transformation

* update tests

* improve support for more grant-types and properties
This commit is contained in:
James Gatz
2022-10-13 16:14:37 +02:00
committed by GitHub
parent 130796ba9b
commit 23b4ee60a6
5 changed files with 835 additions and 149 deletions

View File

@@ -1,74 +1,526 @@
{
"info": {
"_postman_id": "acf8fcf9-f63b-4bf8-93a9-c95cee40bde4",
"name": "Oauth 2.0 Test",
"description": "A collection for testing",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Added to Request URL",
"request": {
"auth": {
"type": "oauth2",
"oauth2": [
{
"key": "accessToken",
"value": "oauth2.0-access-token-url",
"type": "string"
},
{
"key": "addTokenTo",
"value": "queryParams",
"type": "string"
}
]
},
"method": "GET",
"header": [],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "https://insomnia.rest",
"protocol": "https",
"host": ["insomnia", "rest"]
}
},
"response": []
},
{
"name": "Added to Request Headers",
"request": {
"auth": {
"type": "oauth2",
"oauth2": [
{
"key": "accessToken",
"value": "oauth2.0-adccess-token-headers",
"type": "string"
},
{
"key": "addTokenTo",
"value": "header",
"type": "string"
}
]
},
"method": "GET",
"header": [],
"body": {
"mode": "raw",
"raw": ""
},
"url": {
"raw": "https://insomnia.rest",
"protocol": "https",
"host": ["insomnia", "rest"]
}
},
"response": []
}
]
"info": {
"_postman_id": "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXX",
"name": "oauth2",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "XXXXXXXX"
},
"item": [
{
"name": "auth code",
"request": {
"auth": {
"type": "oauth2",
"oauth2": [
{
"key": "headerPrefix",
"value": "test",
"type": "string"
},
{
"key": "audience",
"value": {
"38333c40-c3c4-4a19-ab07-2cca65eb22b9": "test"
},
"type": "any"
},
{
"key": "resource",
"value": {
"21156c61-4463-4f97-ab80-0cb2adcea953": "test"
},
"type": "any"
},
{
"key": "state",
"value": "test",
"type": "string"
},
{
"key": "scope",
"value": "test",
"type": "string"
},
{
"key": "clientSecret",
"value": "test",
"type": "string"
},
{
"key": "clientId",
"value": "test",
"type": "string"
},
{
"key": "accessTokenUrl",
"value": "test",
"type": "string"
},
{
"key": "authUrl",
"value": "test",
"type": "string"
},
{
"key": "redirect_uri",
"value": "test",
"type": "string"
},
{
"key": "grant_type",
"value": "authorization_code",
"type": "string"
},
{
"key": "tokenName",
"value": "test",
"type": "string"
},
{
"key": "addTokenTo",
"value": "header",
"type": "string"
}
]
},
"method": "GET",
"header": [],
"url": {
"raw": "mockbin.org/request/any",
"host": [
"mockbin",
"org"
],
"path": [
"request",
"any"
],
"query": [
{
"key": "foo",
"value": "bar",
"disabled": true
}
]
}
},
"response": []
},
{
"name": "pkce",
"request": {
"auth": {
"type": "oauth2",
"oauth2": [
{
"key": "headerPrefix",
"value": "test",
"type": "string"
},
{
"key": "audience",
"value": {
"a6b13605-f65a-418c-90da-650fcc7a25a0": "test"
},
"type": "any"
},
{
"key": "resource",
"value": {
"95668487-cf79-411f-b3ac-effaa6f84e98": "test"
},
"type": "any"
},
{
"key": "code_verifier",
"value": "test",
"type": "string"
},
{
"key": "grant_type",
"value": "authorization_code_with_pkce",
"type": "string"
},
{
"key": "state",
"value": "test",
"type": "string"
},
{
"key": "scope",
"value": "test",
"type": "string"
},
{
"key": "clientSecret",
"value": "test",
"type": "string"
},
{
"key": "clientId",
"value": "test",
"type": "string"
},
{
"key": "accessTokenUrl",
"value": "test",
"type": "string"
},
{
"key": "authUrl",
"value": "test",
"type": "string"
},
{
"key": "redirect_uri",
"value": "test",
"type": "string"
},
{
"key": "tokenName",
"value": "test",
"type": "string"
},
{
"key": "addTokenTo",
"value": "header",
"type": "string"
}
]
},
"method": "GET",
"header": [],
"url": {
"raw": "mockbin.org/request/any",
"host": [
"mockbin",
"org"
],
"path": [
"request",
"any"
],
"query": [
{
"key": "foo",
"value": "bar",
"disabled": true
}
]
}
},
"response": []
},
{
"name": "implicit",
"request": {
"auth": {
"type": "oauth2",
"oauth2": [
{
"key": "headerPrefix",
"value": "test",
"type": "string"
},
{
"key": "audience",
"value": {
"9f3101aa-93b4-46c3-8626-d0d9275434d8": "test"
},
"type": "any"
},
{
"key": "grant_type",
"value": "implicit",
"type": "string"
},
{
"key": "code_verifier",
"value": "test",
"type": "string"
},
{
"key": "state",
"value": "test",
"type": "string"
},
{
"key": "scope",
"value": "test",
"type": "string"
},
{
"key": "clientSecret",
"value": "test",
"type": "string"
},
{
"key": "clientId",
"value": "test",
"type": "string"
},
{
"key": "accessTokenUrl",
"value": "test",
"type": "string"
},
{
"key": "authUrl",
"value": "test",
"type": "string"
},
{
"key": "redirect_uri",
"value": "test",
"type": "string"
},
{
"key": "tokenName",
"value": "test",
"type": "string"
},
{
"key": "addTokenTo",
"value": "header",
"type": "string"
}
]
},
"method": "GET",
"header": [],
"url": {
"raw": "mockbin.org/request/any",
"host": [
"mockbin",
"org"
],
"path": [
"request",
"any"
],
"query": [
{
"key": "foo",
"value": "bar",
"disabled": true
}
]
}
},
"response": []
},
{
"name": "password",
"request": {
"auth": {
"type": "oauth2",
"oauth2": [
{
"key": "headerPrefix",
"value": "test",
"type": "string"
},
{
"key": "audience",
"value": {
"dd83a076-85c0-4e53-995c-ac99b738f2d3": "test"
},
"type": "any"
},
{
"key": "password",
"value": "test",
"type": "string"
},
{
"key": "username",
"value": "test",
"type": "string"
},
{
"key": "grant_type",
"value": "password_credentials",
"type": "string"
},
{
"key": "code_verifier",
"value": "test",
"type": "string"
},
{
"key": "state",
"value": "test",
"type": "string"
},
{
"key": "scope",
"value": "test",
"type": "string"
},
{
"key": "clientSecret",
"value": "test",
"type": "string"
},
{
"key": "clientId",
"value": "test",
"type": "string"
},
{
"key": "accessTokenUrl",
"value": "test",
"type": "string"
},
{
"key": "authUrl",
"value": "test",
"type": "string"
},
{
"key": "redirect_uri",
"value": "test",
"type": "string"
},
{
"key": "tokenName",
"value": "test",
"type": "string"
},
{
"key": "addTokenTo",
"value": "header",
"type": "string"
}
]
},
"method": "GET",
"header": [],
"url": {
"raw": "mockbin.org/request/any",
"host": [
"mockbin",
"org"
],
"path": [
"request",
"any"
],
"query": [
{
"key": "foo",
"value": "bar",
"disabled": true
}
]
}
},
"response": []
},
{
"name": "client",
"request": {
"auth": {
"type": "oauth2",
"oauth2": [
{
"key": "headerPrefix",
"value": "test",
"type": "string"
},
{
"key": "audience",
"value": {
"49395f3e-8e67-43b9-9479-1ed5209dbb72": "test"
},
"type": "any"
},
{
"key": "resource",
"value": {
"8da4761b-2871-48e9-8aad-a59df2c1bf9a": "test"
},
"type": "any"
},
{
"key": "grant_type",
"value": "client_credentials",
"type": "string"
},
{
"key": "code_verifier",
"value": "test",
"type": "string"
},
{
"key": "state",
"value": "test",
"type": "string"
},
{
"key": "scope",
"value": "test",
"type": "string"
},
{
"key": "clientSecret",
"value": "test",
"type": "string"
},
{
"key": "clientId",
"value": "test",
"type": "string"
},
{
"key": "accessTokenUrl",
"value": "test",
"type": "string"
},
{
"key": "authUrl",
"value": "test",
"type": "string"
},
{
"key": "redirect_uri",
"value": "test",
"type": "string"
},
{
"key": "tokenName",
"value": "test",
"type": "string"
},
{
"key": "addTokenTo",
"value": "header",
"type": "string"
}
]
},
"method": "GET",
"header": [],
"url": {
"raw": "mockbin.org/request/any",
"host": [
"mockbin",
"org"
],
"path": [
"request",
"any"
],
"query": [
{
"key": "foo",
"value": "bar",
"disabled": true
}
]
}
},
"response": []
}
]
}

View File

@@ -1,64 +1,161 @@
{
"_type": "export",
"__export_format": 4,
"__export_date": "2018-10-27T19:15:26.661Z",
"__export_source": "insomnia.importers:v0.1.0",
"_type": "export",
"resources": [
{
"_id": "__GRP_1__",
"_type": "request_group",
"parentId": "__WORKSPACE_ID__",
"name": "Oauth 2.0 Test",
"description": "A collection for testing",
"environment": {}
"description": "",
"environment": {},
"name": "oauth2",
"parentId": "__WORKSPACE_ID__"
},
{
"_id": "__REQ_1__",
"_type": "request",
"parentId": "__GRP_1__",
"url": "https://insomnia.rest",
"name": "Added to Request URL",
"description": "",
"method": "GET",
"body": {},
"parameters": [],
"headers": [],
"authentication": {
"accessTokenUrl": "",
"authorizationUrl": "",
"clientId": "",
"clientSecret": "",
"accessTokenUrl": "test",
"authorizationUrl": "test",
"clientId": "test",
"clientSecret": "test",
"credentialsInBody": false,
"disabled": false,
"grantType": "",
"redirectUrl": "",
"grantType": "authorization_code",
"password": "",
"pkceMethod": "",
"redirectUrl": "test",
"scope": "test",
"state": "test",
"tokenPrefix": "test",
"type": "oauth2",
"username": ""
}
},
"body": {},
"description": "",
"headers": [],
"method": "GET",
"name": "auth code",
"parameters": [],
"parentId": "__GRP_1__",
"url": "mockbin.org/request/any"
},
{
"_id": "__REQ_2__",
"_type": "request",
"parentId": "__GRP_1__",
"url": "https://insomnia.rest",
"name": "Added to Request Headers",
"description": "",
"method": "GET",
"body": {},
"parameters": [],
"headers": [],
"authentication": {
"accessTokenUrl": "",
"authorizationUrl": "",
"clientId": "",
"clientSecret": "",
"accessTokenUrl": "test",
"authorizationUrl": "test",
"clientId": "test",
"clientSecret": "test",
"credentialsInBody": false,
"disabled": false,
"grantType": "",
"redirectUrl": "",
"grantType": "authorization_code",
"password": "",
"pkceMethod": "",
"redirectUrl": "test",
"scope": "test",
"state": "test",
"tokenPrefix": "test",
"type": "oauth2",
"usePkce": true,
"username": ""
},
"body": {},
"description": "",
"headers": [],
"method": "GET",
"name": "pkce",
"parameters": [],
"parentId": "__GRP_1__",
"url": "mockbin.org/request/any"
},
{
"_id": "__REQ_3__",
"_type": "request",
"authentication": {
"accessTokenUrl": "test",
"authorizationUrl": "test",
"clientId": "test",
"clientSecret": "test",
"credentialsInBody": false,
"disabled": false,
"grantType": "implicit",
"password": "",
"pkceMethod": "",
"redirectUrl": "test",
"scope": "test",
"state": "test",
"tokenPrefix": "test",
"type": "oauth2",
"username": ""
}
},
"body": {},
"description": "",
"headers": [],
"method": "GET",
"name": "implicit",
"parameters": [],
"parentId": "__GRP_1__",
"url": "mockbin.org/request/any"
},
{
"_id": "__REQ_4__",
"_type": "request",
"authentication": {
"accessTokenUrl": "test",
"authorizationUrl": "test",
"clientId": "test",
"clientSecret": "test",
"credentialsInBody": false,
"disabled": false,
"grantType": "password",
"password": "test",
"pkceMethod": "",
"redirectUrl": "test",
"scope": "test",
"state": "test",
"tokenPrefix": "test",
"type": "oauth2",
"username": "test"
},
"body": {},
"description": "",
"headers": [],
"method": "GET",
"name": "password",
"parameters": [],
"parentId": "__GRP_1__",
"url": "mockbin.org/request/any"
},
{
"_id": "__REQ_5__",
"_type": "request",
"authentication": {
"accessTokenUrl": "test",
"authorizationUrl": "test",
"clientId": "test",
"clientSecret": "test",
"credentialsInBody": false,
"disabled": false,
"grantType": "client_credentials",
"password": "",
"pkceMethod": "",
"redirectUrl": "test",
"scope": "test",
"state": "test",
"tokenPrefix": "test",
"type": "oauth2",
"username": ""
},
"body": {},
"description": "",
"headers": [],
"method": "GET",
"name": "client",
"parameters": [],
"parentId": "__GRP_1__",
"url": "mockbin.org/request/any"
}
]
}

View File

@@ -1,40 +1,45 @@
{
"__export_date": "2022-04-04T22:51:36.852Z",
"__export_format": 4,
"__export_source": "insomnia.importers:v0.1.0",
"_type": "export",
"resources": [
{
"_id": "__GRP_1__",
"_type": "request_group",
"description": "",
"environment": {},
"name": "Example collection",
"parentId": "__WORKSPACE_ID__"
},
{
"_id": "__REQ_1__",
"_type": "request",
"authentication": {
"accessTokenUrl": "exampleAccessTokenUrl",
"authorizationUrl": "exampleAuthorizeUrl",
"clientId": "exampleClientId",
"clientSecret": "exampleClientSecret",
"disabled": false,
"grantType": "authorization_code",
"password": "",
"redirectUrl": "exampleCallbackUrl",
"type": "oauth2",
"username": ""
},
"body": {},
"description": "",
"headers": [],
"method": "GET",
"name": "Example request with OAuth2",
"parameters": [],
"parentId": "__GRP_1__",
"url": ""
}
]
"__export_date": "2022-04-04T22:51:36.852Z",
"__export_format": 4,
"__export_source": "insomnia.importers:v0.1.0",
"_type": "export",
"resources": [
{
"_id": "__GRP_1__",
"_type": "request_group",
"description": "",
"environment": {},
"name": "Example collection",
"parentId": "__WORKSPACE_ID__"
},
{
"_id": "__REQ_1__",
"_type": "request",
"authentication": {
"accessTokenUrl": "exampleAccessTokenUrl",
"authorizationUrl": "exampleAuthorizeUrl",
"clientId": "exampleClientId",
"clientSecret": "exampleClientSecret",
"credentialsInBody": false,
"pkceMethod": "S256",
"disabled": false,
"grantType": "authorization_code",
"password": "",
"scope": "",
"state": "",
"tokenPrefix": "",
"redirectUrl": "exampleCallbackUrl",
"type": "oauth2",
"username": ""
},
"body": {},
"description": "",
"headers": [],
"method": "GET",
"name": "Example request with OAuth2",
"parameters": [],
"parentId": "__GRP_1__",
"url": ""
}
]
}

View File

@@ -314,11 +314,122 @@ describe('postman', () => {
authorizationUrl: 'exampleAuthorizeUrl',
clientId: 'exampleClientId',
clientSecret: 'exampleClientSecret',
credentialsInBody: false,
disabled: false,
grantType: 'authorization_code',
password: '',
pkceMethod: 'S256',
redirectUrl: 'exampleCallbackUrl',
scope: '',
state: '',
tokenPrefix: '',
type: 'oauth2',
usePkce: undefined,
username: '',
});
});
it('returns oauth2 for Postman v2.1.0 with PKCE', () => {
const requestWithPCKE: Request1 = {
'auth': {
'type': 'oauth2',
'oauth2': [
{
'key': 'state',
'value': '1234567890',
'type': 'string',
},
{
'key': 'scope',
'value': 'read:org',
'type': 'string',
},
{
'key': 'clientSecret',
'value': '1234567890',
'type': 'string',
},
{
'key': 'clientId',
'value': '1234567890',
'type': 'string',
},
{
'key': 'accessTokenUrl',
'value': 'https://accounts.google.com/o/oauth2/token',
'type': 'string',
},
{
'key': 'authUrl',
'value': 'https://accounts.google.com/o/oauth2/auth',
'type': 'string',
},
{
'key': 'grant_type',
'value': 'authorization_code_with_pkce',
'type': 'string',
},
{
'key': 'tokenName',
'value': 'Test',
'type': 'string',
},
{
'key': 'challengeAlgorithm',
'value': 'S256',
'type': 'string',
},
{
'key': 'addTokenTo',
'value': 'queryParams',
'type': 'string',
},
{
'key': 'client_authentication',
'value': 'header',
'type': 'string',
},
{
key: 'redirect_uri',
value: 'exampleCallbackUrl',
type: 'string',
},
],
},
'method': 'GET',
'header': [],
'url': {
'raw': 'https://mockbin.org/echo',
'protocol': 'https',
'host': [
'mockbin',
'org',
],
'path': [
'echo',
],
},
};
const schema = postmanSchema({ requests: [requestWithPCKE], version: 'v2.1.0' });
const postman = new ImportPostman(schema);
const { authentication } = postman.importRequestItem({ request: requestWithPCKE }, 'n/a');
expect(authentication).toEqual({
accessTokenUrl: 'https://accounts.google.com/o/oauth2/token',
authorizationUrl: 'https://accounts.google.com/o/oauth2/auth',
clientId: '1234567890',
clientSecret: '1234567890',
credentialsInBody: true,
disabled: false,
grantType: 'authorization_code',
password: '',
pkceMethod: 'S256',
redirectUrl: 'exampleCallbackUrl',
scope: 'read:org',
state: '1234567890',
tokenPrefix: '',
type: 'oauth2',
usePkce: true,
username: '',
});
});

View File

@@ -56,6 +56,18 @@ const POSTMAN_SCHEMA_V2_0 =
const POSTMAN_SCHEMA_V2_1 =
'https://schema.getpostman.com/json/collection/v2.1.0/collection.json';
const mapGrantTypeToInsomniaGrantType = (grantType: string) => {
if (grantType === 'authorization_code_with_pkce') {
return 'authorization_code';
}
if (grantType === 'password_credentials') {
return 'password';
}
return grantType;
};
export class ImportPostman {
collection;
@@ -658,14 +670,23 @@ export class ImportPostman {
// Note: We only support importing OAuth2 configuration from Postman v2.1
if (schema === POSTMAN_SCHEMA_V2_1) {
const oauth2 = auth.oauth2 as V210Auth['oauth2'];
const grantTypeField = this.findValueByKey(oauth2, 'grant_type');
const grantType = mapGrantTypeToInsomniaGrantType(grantTypeField);
return {
type: 'oauth2',
disabled: false,
pkceMethod: this.findValueByKey(oauth2, 'challengeAlgorithm'),
state: this.findValueByKey(oauth2, 'state'),
scope: this.findValueByKey(oauth2, 'scope'),
tokenPrefix: this.findValueByKey(oauth2, 'headerPrefix'),
credentialsInBody: this.findValueByKey(oauth2, 'addTokenTo') !== 'header',
accessTokenUrl: this.findValueByKey(oauth2, 'accessTokenUrl'),
authorizationUrl: this.findValueByKey(oauth2, 'authUrl'),
grantType: this.findValueByKey(oauth2, 'grant_type'),
password: '',
username: '',
grantType,
password: this.findValueByKey(oauth2, 'password'),
username: this.findValueByKey(oauth2, 'username'),
usePkce: grantTypeField === 'authorization_code_with_pkce' ? true : undefined,
clientId: this.findValueByKey(oauth2, 'clientId'),
clientSecret: this.findValueByKey(oauth2, 'clientSecret'),
redirectUrl: this.findValueByKey(oauth2, 'redirect_uri'),