piral-oauth2
Advanced tools
Comparing version 0.11.1-pre.1050 to 0.11.1-pre.1053
@@ -6,2 +6,31 @@ /** | ||
/** | ||
* The id of the client. Required for the setup of OAuth 2.0. | ||
*/ | ||
clientId: string; | ||
/** | ||
* The client secret. Only required for the `code` flow. | ||
*/ | ||
clientSecret?: string; | ||
/** | ||
* The Uri pointing to the authorization endpoint of the Identity Provider. | ||
*/ | ||
authorizationUri: string; | ||
/** | ||
* The Uri pointing to the access token endpoint of the Identity Provider. | ||
*/ | ||
accessTokenUri?: string; | ||
/** | ||
* The redirect Uri to use. By default the origin with /auth | ||
* is used. | ||
*/ | ||
redirectUri?: string; | ||
/** | ||
* The scopes to be used. | ||
*/ | ||
scopes?: Array<string>; | ||
/** | ||
* The OAuth 2.0 authorization flow type to be used. | ||
*/ | ||
flow?: 'implicit' | 'code'; | ||
/** | ||
* Restricts token sharing such that other integrations, e.g., with | ||
@@ -30,6 +59,2 @@ * fetch would need to be done manually. | ||
/** | ||
* Retrieves the current account. | ||
*/ | ||
account(): Account; | ||
/** | ||
* Gets a token. | ||
@@ -44,5 +69,5 @@ */ | ||
/** | ||
* Sets up a new client wrapping the oauth2 API. | ||
* Sets up a new client wrapping the OAuth 2.0 API. | ||
* @param config The configuration for the client. | ||
*/ | ||
export declare function setupOAuth2Client(config: OAuth2Config): OAuth2Client; |
103
lib/setup.js
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var ClientOAuth2 = require("client-oauth2"); | ||
var callbackName = 'oauth2Cb'; | ||
/** | ||
* Sets up a new client wrapping the oauth2 API. | ||
* Sets up a new client wrapping the OAuth 2.0 API. | ||
* @param config The configuration for the client. | ||
*/ | ||
function setupOAuth2Client(config) { | ||
var _a = config.restrict, restrict = _a === void 0 ? false : _a; | ||
return { | ||
login: function () { | ||
//TODO | ||
}, | ||
logout: function () { | ||
//TODO | ||
}, | ||
account: function () { | ||
//TODO | ||
return undefined; | ||
}, | ||
extendHeaders: function (req) { | ||
if (!restrict) { | ||
//TODO | ||
req.setHeaders(undefined); | ||
var clientId = config.clientId, clientSecret = config.clientSecret, authorizationUri = config.authorizationUri, accessTokenUri = config.accessTokenUri, _a = config.redirectUri, redirectUri = _a === void 0 ? location.origin + "/auth" : _a, _b = config.scopes, scopes = _b === void 0 ? [] : _b, flow = config.flow, _c = config.restrict, restrict = _c === void 0 ? false : _c; | ||
var client = new ClientOAuth2({ | ||
clientId: clientId, | ||
clientSecret: clientSecret, | ||
redirectUri: redirectUri, | ||
authorizationUri: authorizationUri, | ||
accessTokenUri: accessTokenUri, | ||
scopes: scopes, | ||
}); | ||
var currentToken; | ||
if (flow === 'code') { | ||
client.code.getToken(location.href).then(function (token) { return (currentToken = token); }, function () { }); | ||
var retrieveToken_1 = function () { | ||
if (!currentToken) { | ||
return Promise.reject('Not logged in. Please call `login()` to retrieve a token.'); | ||
} | ||
}, | ||
token: function () { | ||
//TODO | ||
return undefined; | ||
}, | ||
}; | ||
if (!currentToken.expired()) { | ||
return Promise.resolve(currentToken.accessToken); | ||
} | ||
return currentToken.refresh().then(function (refreshedToken) { | ||
currentToken = refreshedToken; | ||
return currentToken.accessToken; | ||
}); | ||
}; | ||
return { | ||
login: function () { | ||
window.location.href = client.code.getUri(); | ||
}, | ||
logout: function () { | ||
currentToken = undefined; | ||
}, | ||
extendHeaders: function (req) { | ||
if (!restrict) { | ||
req.setHeaders(retrieveToken_1().then(function (token) { return token && { Authorization: "Bearer " + token }; }, function () { return undefined; })); | ||
} | ||
}, | ||
token: retrieveToken_1, | ||
}; | ||
} | ||
else { | ||
client.token.getToken(location.href).then(function (token) { | ||
var opener = window.opener; | ||
if (opener && typeof opener[callbackName] === 'function') { | ||
opener[callbackName](token); | ||
window.close(); | ||
} | ||
currentToken = token; | ||
}, function () { }); | ||
var retrieveToken_2 = function () { | ||
if (!currentToken) { | ||
return Promise.reject('Not logged in. Please call `login()` to retrieve a token.'); | ||
} | ||
if (!currentToken.expired()) { | ||
return Promise.resolve(currentToken.accessToken); | ||
} | ||
return new Promise(function (res) { | ||
window[callbackName] = function (token) { | ||
currentToken = token; | ||
res(currentToken.accessToken); | ||
}; | ||
window.open(client.token.getUri()); | ||
}); | ||
}; | ||
return { | ||
login: function () { | ||
window.location.href = client.token.getUri(); | ||
}, | ||
logout: function () { | ||
currentToken = undefined; | ||
}, | ||
extendHeaders: function (req) { | ||
if (!restrict) { | ||
req.setHeaders(retrieveToken_2().then(function (token) { return token && { Authorization: "Bearer " + token }; }, function () { return undefined; })); | ||
} | ||
}, | ||
token: retrieveToken_2, | ||
}; | ||
} | ||
} | ||
exports.setupOAuth2Client = setupOAuth2Client; | ||
//# sourceMappingURL=setup.js.map |
{ | ||
"name": "piral-oauth2", | ||
"version": "0.11.1-pre.1050", | ||
"version": "0.11.1-pre.1053", | ||
"description": "Plugin to integrate OAuth 2.0 authentication in Piral.", | ||
@@ -41,4 +41,7 @@ "keywords": [ | ||
}, | ||
"dependencies": { | ||
"client-oauth2": "^4.2.5" | ||
}, | ||
"devDependencies": { | ||
"piral-core": "^0.11.1-pre.1050" | ||
"piral-core": "^0.11.1-pre.1053" | ||
}, | ||
@@ -48,3 +51,3 @@ "peerDependencies": { | ||
}, | ||
"gitHead": "7dfdde4ea9e8e9469ecbbba473b6a0d1f214e001" | ||
"gitHead": "02c384db2162e9ecdf276492fce3c47457e2dc7c" | ||
} |
171
src/setup.ts
@@ -0,1 +1,3 @@ | ||
import * as ClientOAuth2 from 'client-oauth2'; | ||
/** | ||
@@ -6,2 +8,31 @@ * Available configuration options for the OAuth 2.0 plugin. | ||
/** | ||
* The id of the client. Required for the setup of OAuth 2.0. | ||
*/ | ||
clientId: string; | ||
/** | ||
* The client secret. Only required for the `code` flow. | ||
*/ | ||
clientSecret?: string; | ||
/** | ||
* The Uri pointing to the authorization endpoint of the Identity Provider. | ||
*/ | ||
authorizationUri: string; | ||
/** | ||
* The Uri pointing to the access token endpoint of the Identity Provider. | ||
*/ | ||
accessTokenUri?: string; | ||
/** | ||
* The redirect Uri to use. By default the origin with /auth | ||
* is used. | ||
*/ | ||
redirectUri?: string; | ||
/** | ||
* The scopes to be used. | ||
*/ | ||
scopes?: Array<string>; | ||
/** | ||
* The OAuth 2.0 authorization flow type to be used. | ||
*/ | ||
flow?: 'implicit' | 'code'; | ||
/** | ||
* Restricts token sharing such that other integrations, e.g., with | ||
@@ -32,6 +63,2 @@ * fetch would need to be done manually. | ||
/** | ||
* Retrieves the current account. | ||
*/ | ||
account(): Account; | ||
/** | ||
* Gets a token. | ||
@@ -46,30 +73,120 @@ */ | ||
const callbackName = 'oauth2Cb'; | ||
/** | ||
* Sets up a new client wrapping the oauth2 API. | ||
* Sets up a new client wrapping the OAuth 2.0 API. | ||
* @param config The configuration for the client. | ||
*/ | ||
export function setupOAuth2Client(config: OAuth2Config): OAuth2Client { | ||
const { restrict = false } = config; | ||
return { | ||
login() { | ||
//TODO | ||
}, | ||
logout() { | ||
//TODO | ||
}, | ||
account() { | ||
//TODO | ||
return undefined; | ||
}, | ||
extendHeaders(req) { | ||
if (!restrict) { | ||
//TODO | ||
req.setHeaders(undefined); | ||
const { | ||
clientId, | ||
clientSecret, | ||
authorizationUri, | ||
accessTokenUri, | ||
redirectUri = `${location.origin}/auth`, | ||
scopes = [], | ||
flow, | ||
restrict = false, | ||
} = config; | ||
const client = new ClientOAuth2({ | ||
clientId, | ||
clientSecret, | ||
redirectUri, | ||
authorizationUri, | ||
accessTokenUri, | ||
scopes, | ||
}); | ||
let currentToken: ClientOAuth2.Token; | ||
if (flow === 'code') { | ||
client.code.getToken(location.href).then( | ||
token => (currentToken = token), | ||
() => {}, | ||
); | ||
const retrieveToken = () => { | ||
if (!currentToken) { | ||
return Promise.reject('Not logged in. Please call `login()` to retrieve a token.'); | ||
} | ||
}, | ||
token() { | ||
//TODO | ||
return undefined; | ||
}, | ||
}; | ||
if (!currentToken.expired()) { | ||
return Promise.resolve(currentToken.accessToken); | ||
} | ||
return currentToken.refresh().then(refreshedToken => { | ||
currentToken = refreshedToken; | ||
return currentToken.accessToken; | ||
}); | ||
}; | ||
return { | ||
login() { | ||
window.location.href = client.code.getUri(); | ||
}, | ||
logout() { | ||
currentToken = undefined; | ||
}, | ||
extendHeaders(req) { | ||
if (!restrict) { | ||
req.setHeaders( | ||
retrieveToken().then( | ||
token => token && { Authorization: `Bearer ${token}` }, | ||
() => undefined, | ||
), | ||
); | ||
} | ||
}, | ||
token: retrieveToken, | ||
}; | ||
} else { | ||
client.token.getToken(location.href).then( | ||
token => { | ||
const opener = window.opener; | ||
if (opener && typeof opener[callbackName] === 'function') { | ||
opener[callbackName](token); | ||
window.close(); | ||
} | ||
currentToken = token; | ||
}, | ||
() => {}, | ||
); | ||
const retrieveToken = () => { | ||
if (!currentToken) { | ||
return Promise.reject('Not logged in. Please call `login()` to retrieve a token.'); | ||
} | ||
if (!currentToken.expired()) { | ||
return Promise.resolve(currentToken.accessToken); | ||
} | ||
return new Promise<string>(res => { | ||
window[callbackName] = (token: ClientOAuth2.Token) => { | ||
currentToken = token; | ||
res(currentToken.accessToken); | ||
}; | ||
window.open(client.token.getUri()); | ||
}); | ||
}; | ||
return { | ||
login() { | ||
window.location.href = client.token.getUri(); | ||
}, | ||
logout() { | ||
currentToken = undefined; | ||
}, | ||
extendHeaders(req) { | ||
if (!restrict) { | ||
req.setHeaders( | ||
retrieveToken().then( | ||
token => token && { Authorization: `Bearer ${token}` }, | ||
() => undefined, | ||
), | ||
); | ||
} | ||
}, | ||
token: retrieveToken, | ||
}; | ||
} | ||
} |
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
20987
406
2
+ Addedclient-oauth2@^4.2.5
+ Added@servie/events@1.0.0(transitive)
+ Added@types/tough-cookie@4.0.5(transitive)
+ Addedbyte-length@1.0.2(transitive)
+ Addedclient-oauth2@4.3.3(transitive)
+ Addedmake-error@1.3.6(transitive)
+ Addedmake-error-cause@2.3.0(transitive)
+ Addedpopsicle@12.1.2(transitive)
+ Addedpopsicle-content-encoding@1.0.0(transitive)
+ Addedpopsicle-cookie-jar@1.0.1(transitive)
+ Addedpopsicle-redirects@1.1.1(transitive)
+ Addedpopsicle-transport-http@1.2.1(transitive)
+ Addedpopsicle-transport-xhr@2.0.0(transitive)
+ Addedpopsicle-user-agent@1.0.0(transitive)
+ Addedpsl@1.13.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedquerystringify@2.2.0(transitive)
+ Addedrequires-port@1.0.0(transitive)
+ Addedsafe-buffer@5.2.1(transitive)
+ Addedservie@4.3.3(transitive)
+ Addedthrowback@4.1.0(transitive)
+ Addedtough-cookie@4.1.4(transitive)
+ Addedts-expect@1.3.0(transitive)
+ Addeduniversalify@0.2.0(transitive)
+ Addedurl-parse@1.5.10(transitive)