@advanced-rest-client/api-console-extension
Advanced tools
Comparing version 0.1.2 to 0.1.3
{ | ||
"manifest_version": 3, | ||
"name": "API console extension", | ||
"version": "0.1.0", | ||
"version": "0.1.3", | ||
"description": "API Console extension to proxy HTTP requests to the documented API.", | ||
@@ -6,0 +6,0 @@ "author": "Pawel Psztyc <pawel.psztyc@mulesoft.com>", |
{ | ||
"name": "@advanced-rest-client/api-console-extension", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "API Console extension to proxy HTTP requests to the documented API.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -237,2 +237,3 @@ /** @typedef {import('./types').IApiConsoleHttpRequest} IApiConsoleHttpRequest */ | ||
code: "no_response", | ||
error: true, | ||
}, | ||
@@ -245,8 +246,5 @@ }) | ||
if (typedError.error) { | ||
const message = | ||
typeof typedError.message === "string" | ||
? typedError.message | ||
: "No response has been recorded."; | ||
const code = | ||
typeof typedError.code === "string" ? typedError.code : "unknown_error"; | ||
const message = typeof typedError.message === "string" ? typedError.message : "No response has been recorded."; | ||
const code = typeof typedError.code === "string" ? typedError.code : "unknown_error"; | ||
const state = typeof typedError.state === "string" ? typedError.state : undefined; | ||
this.eventTarget.dispatchEvent( | ||
@@ -259,2 +257,4 @@ new CustomEvent("oauth2-error", { | ||
code, | ||
state, | ||
error: true, | ||
}, | ||
@@ -266,21 +266,10 @@ }) | ||
const typedToken = /** @type ITokenInfo */ (data); | ||
const state = | ||
typeof typedToken.state === "string" ? typedToken.state : undefined; | ||
const accessToken = | ||
typedToken.accessToken && typeof typedToken.accessToken === "string" | ||
? typedToken.accessToken | ||
: undefined; | ||
const tokenType = | ||
typedToken.tokenType && typeof typedToken.tokenType === "string" | ||
? typedToken.tokenType | ||
: undefined; | ||
const expiresIn = | ||
typedToken.expiresIn && | ||
(typeof typedToken.expiresIn === "number" || | ||
typeof typedToken.expiresIn === "string") | ||
? Number(typedToken.expiresIn) | ||
: undefined; | ||
const scope = Array.isArray(typedToken.scope) | ||
? typedToken.scope | ||
: undefined; | ||
const state = typeof typedToken.state === "string" ? typedToken.state : undefined; | ||
const accessToken = typedToken.accessToken && typeof typedToken.accessToken === "string" ? typedToken.accessToken : undefined; | ||
const refreshToken = typedToken.refreshToken && typeof typedToken.refreshToken === "string" ? typedToken.refreshToken : undefined; | ||
const tokenType = typedToken.tokenType && typeof typedToken.tokenType === "string" ? typedToken.tokenType : undefined; | ||
const expiresIn = typedToken.expiresIn && (typeof typedToken.expiresIn === "number" || typeof typedToken.expiresIn === "string") ? Number(typedToken.expiresIn): undefined; | ||
const expiresAt = typedToken.expiresAt && (typeof typedToken.expiresAt === "number" || typeof typedToken.expiresAt === "string") ? Number(typedToken.expiresAt): undefined; | ||
const expiresAssumed = typeof typedToken.expiresAssumed === "boolean" ? typedToken.expiresAssumed : undefined; | ||
const scope = Array.isArray(typedToken.scope) ? typedToken.scope : undefined; | ||
this.eventTarget.dispatchEvent( | ||
@@ -296,2 +285,5 @@ new CustomEvent("oauth2-token-response", { | ||
scope, | ||
refreshToken, | ||
expiresAt, | ||
expiresAssumed, | ||
}, | ||
@@ -298,0 +290,0 @@ }) |
@@ -253,2 +253,3 @@ import { AuthorizationError, CodeError } from './AuthorizationError.js'; | ||
}; | ||
this._addTabHandlers(); | ||
} catch (e) { | ||
@@ -347,3 +348,3 @@ throw new AuthorizationError( | ||
try { | ||
const raw = this._authDataFromUrl(url); | ||
raw = this._authDataFromUrl(url); | ||
if (!raw) { | ||
@@ -442,2 +443,3 @@ throw new Error(''); | ||
tokenInfo = await this.exchangeCode(code); | ||
tokenInfo.state = state; | ||
} catch (e) { | ||
@@ -444,0 +446,0 @@ this._handleTokenCodeError(/** @type Error */(e)); |
@@ -62,3 +62,3 @@ /** @typedef {import('../types').IOAuth2Authorization} IOAuth2Authorization */ | ||
const array = new Uint32Array(28); | ||
window.crypto.getRandomValues(array); | ||
globalThis.crypto.getRandomValues(array); | ||
return Array.from(array, (dec) => `0${dec.toString(16)}`.substr(-2)).join(""); | ||
@@ -97,3 +97,3 @@ } | ||
const data = encoder.encode(value); | ||
return window.crypto.subtle.digest("SHA-256", data); | ||
return globalThis.crypto.subtle.digest("SHA-256", data); | ||
} | ||
@@ -131,5 +131,5 @@ | ||
let array = new Uint8Array(size); | ||
window.crypto.getRandomValues(array); | ||
globalThis.crypto.getRandomValues(array); | ||
array = array.map(x => validChars.charCodeAt(x % validChars.length)); | ||
return String.fromCharCode.apply(null, array); | ||
} |
@@ -54,4 +54,5 @@ import { HttpProxy } from './proxy/HttpProxy.js'; | ||
'message': e.message || 'The request is invalid.', | ||
'code': 'invalid_request', | ||
'error': true | ||
'code': e.code || 'invalid_request', | ||
'error': true, | ||
'state': e.state, | ||
}); | ||
@@ -58,0 +59,0 @@ } |
@@ -111,2 +111,3 @@ interface IProxyMessage { | ||
message: string; | ||
state?: string; | ||
} | ||
@@ -113,0 +114,0 @@ |
@@ -152,120 +152,1 @@ import { test as base, chromium, expect } from '@playwright/test'; | ||
}); | ||
// const assert = require('chai').assert; | ||
// const ApiConsoleOauthProxy = require('../js/background.js').ApiConsoleOauthProxy; | ||
// describe('api-console-extension', function() { | ||
// describe('OAuth2 proxy', function() { | ||
// describe('popup-url', function() { | ||
// var proxy; | ||
// var settings; | ||
// var popupUrl; | ||
// before(function() { | ||
// settings = { | ||
// authorizationUrl: 'https://authorizationUrl.com', | ||
// clientId: 'test-123 test', | ||
// redirectUrl: 'https://redirectUrl.com', | ||
// scopes: ['scope-1', 'scope-2'] | ||
// }; | ||
// proxy = new ApiConsoleOauthProxy(settings); | ||
// popupUrl = proxy._constructPopupUrl('token'); | ||
// }); | ||
// // beforeEach(function() { | ||
// // proxy = new ApiConsoleOauthProxy(settings); | ||
// // }); | ||
// function getParam(name) { | ||
// var _url = popupUrl.substr(popupUrl.indexOf('?') + 1); | ||
// var parts = _url.split('&'); | ||
// for (var i = 0, len = parts.length; i < len; i++) { | ||
// let params = parts[i].split('='); | ||
// if (params[0] === name) { | ||
// return params[1]; | ||
// } | ||
// } | ||
// } | ||
// it('Constructs OAuth URL', function() { | ||
// assert.isString(popupUrl); | ||
// }); | ||
// it('Sets authorization URL and response_type', function() { | ||
// var base = settings.authorizationUrl + '?response_type=token'; | ||
// var index = popupUrl.indexOf(base); | ||
// assert.equal(index, 0); | ||
// }); | ||
// it('Sets client_id', function() { | ||
// var clientId = getParam('client_id'); | ||
// assert.equal(clientId, 'test-123%20test'); | ||
// }); | ||
// it('Sets redirect_uri', function() { | ||
// var redirectUrl = getParam('redirect_uri'); | ||
// assert.equal(redirectUrl, 'https%3A%2F%2FredirectUrl.com'); | ||
// }); | ||
// it('Sets scope', function() { | ||
// var scopes = getParam('scope'); | ||
// assert.equal(scopes, 'scope-1%20scope-2'); | ||
// }); | ||
// it('Sets state', function() { | ||
// var state = getParam('state'); | ||
// assert.isString(state); | ||
// }); | ||
// }); | ||
// describe('authDataFromUrl', function() { | ||
// var authData; | ||
// var token; | ||
// var tokenType; | ||
// var expiresIn; | ||
// var state; | ||
// before(function() { | ||
// var settings = { | ||
// authorizationUrl: 'https://authorizationUrl.com', | ||
// clientId: 'test-123 test', | ||
// redirectUrl: 'https://redirectUrl.com', | ||
// scopes: ['scope-1', 'scope-2'] | ||
// }; | ||
// token = 'ya29.GlwpBBGitx7n81P6Jdu1l43Y0M_j7WD0uVQRc3H1v6PyL0Ob6H6UrsWj'; | ||
// token += '-rTMxXtX66_cdEbRJwHyArtR79GIGnIYfhcOBMt8qH96e9oGswGaGPkb1egRZ5UIf_qzFQ'; | ||
// tokenType = 'Bearer'; | ||
// state = '173mwy'; | ||
// expiresIn = '3600'; | ||
// var proxy = new ApiConsoleOauthProxy(settings); | ||
// var url = 'http://localhost:8080/components/oauth-authorization/oauth-popup.html'; | ||
// url += '#state=' + state; | ||
// url += '&access_token=' + token; | ||
// url += '&token_type=' + tokenType; | ||
// url += '&expires_in=' + expiresIn; | ||
// authData = proxy.authDataFromUrl(url); | ||
// }); | ||
// it('Has accessToken', function() { | ||
// assert.isString(authData.accessToken, 'accessToken is string'); | ||
// assert.equal(authData.accessToken, token, 'Token value equals'); | ||
// }); | ||
// it('Has accessToken', function() { | ||
// assert.isString(authData.expiresIn, 'expiresIn is string'); | ||
// assert.equal(authData.expiresIn, expiresIn, 'expiresIn equals ' + expiresIn); | ||
// }); | ||
// it('Has state', function() { | ||
// assert.isString(authData.state, 'state is string'); | ||
// assert.equal(authData.state, state, 'state equals ' + state); | ||
// }); | ||
// it('Has tokenType', function() { | ||
// assert.isString(authData.tokenType, 'tokenType is string'); | ||
// assert.equal(authData.tokenType, tokenType, 'tokenType equals ' + tokenType); | ||
// }); | ||
// }); | ||
// }); | ||
// }); |
/** @typedef {import('../../src/types').ISafePayload} ISafePayload */ | ||
/** @typedef {import('../../src/types').IOAuth2Authorization} IOAuth2Authorization */ | ||
@@ -77,2 +78,32 @@ export class ProxyRequest { | ||
/** | ||
* Dispatches the event for the proxy to handle OAuth 2 authorization. | ||
* @param {IOAuth2Authorization} config | ||
*/ | ||
async proxyOauth2(config) { | ||
const result = await this.page.evaluate(([detail]) => { | ||
const e = new CustomEvent('oauth2-token-requested', { | ||
bubbles: true, | ||
cancelable: true, | ||
detail, | ||
}); | ||
document.body.dispatchEvent(e); | ||
return new Promise((resolve) => { | ||
const handlerSuccess = (e) => { | ||
resolve(e.detail); | ||
window.removeEventListener('oauth2-token-response', handlerSuccess); | ||
window.removeEventListener('oauth2-error', handlerError); | ||
}; | ||
const handlerError = (e) => { | ||
resolve(e.detail); | ||
window.removeEventListener('oauth2-token-response', handlerSuccess); | ||
window.removeEventListener('oauth2-error', handlerError); | ||
}; | ||
window.addEventListener('oauth2-token-response', handlerSuccess); | ||
window.addEventListener('oauth2-error', handlerError); | ||
}); | ||
}, [config]); | ||
return result; | ||
} | ||
/** | ||
* @param {string} url | ||
@@ -79,0 +110,0 @@ * @param {string=} method |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
263827
43
4093