@uportal/open-id-connect
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -1,1 +0,1 @@ | ||
'use strict';Object.defineProperty(exports,'__esModule',{value:!0}),exports.tokenize=tokenize;var _axios=require('axios'),_jsonwebtoken=require('jsonwebtoken');function _asyncToGenerator(a){return function(){var b=a.apply(this,arguments);return new Promise(function(a,c){function d(e,f){try{var g=b[e](f),h=g.value}catch(a){return void c(a)}return g.done?void a(h):Promise.resolve(h).then(function(a){d('next',a)},function(a){d('throw',a)})}return d('next')})}}var token=null;exports.default=function(){var a=_asyncToGenerator(regeneratorRuntime.mark(function a(){var b,c,d=0<arguments.length&&arguments[0]!==void 0?arguments[0]:{},e=d.userInfoApiUrl,f=e===void 0?'/uPortal/api/v5-1/userinfo':e,g=d.timeout,h=g===void 0?5e4:g,i=1<arguments.length&&arguments[1]!==void 0?arguments[1]:function(){};return regeneratorRuntime.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:if(null===token){a.next=2;break}return a.abrupt('return',token);case 2:return a.prev=2,a.next=5,(0,_axios.get)(f,{responseType:'text'});case 5:return b=a.sent,c=b.data,token=tokenize(c),setTimeout(function(){token=null},h),i(null,token),a.abrupt('return',token);case 13:if(a.prev=13,a.t0=a['catch'](2),i){a.next=17;break}throw a.t0;case 17:i(a.t0);case 18:case'end':return a.stop();}},a,this,[[2,13]])}));return function(){return a.apply(this,arguments)}}();function tokenize(a){return{encoded:a,decoded:(0,_jsonwebtoken.decode)(a)}} | ||
'use strict';var _slicedToArray=function(){function a(a,b){var c=[],d=!0,e=!1,f=void 0;try{for(var g,h=a[Symbol.iterator]();!(d=(g=h.next()).done)&&(c.push(g.value),!(b&&c.length===b));d=!0);}catch(a){e=!0,f=a}finally{try{!d&&h['return']&&h['return']()}finally{if(e)throw f}}return c}return function(b,c){if(Array.isArray(b))return b;if(Symbol.iterator in Object(b))return a(b,c);throw new TypeError('Invalid attempt to destructure non-iterable instance')}}();Object.defineProperty(exports,'__esModule',{value:!0});exports.tokenize=tokenize;var _axios=require('axios'),_jsonwebtoken=require('jsonwebtoken');function _asyncToGenerator(a){return function(){var b=a.apply(this,arguments);return new Promise(function(a,c){function d(e,f){try{var g=b[e](f),h=g.value}catch(a){return void c(a)}return g.done?void a(h):Promise.resolve(h).then(function(a){d('next',a)},function(a){d('throw',a)})}return d('next')})}}var token=null;exports.default=function(){var a=_asyncToGenerator(regeneratorRuntime.mark(function a(){var b,c,d=0<arguments.length&&arguments[0]!==void 0?arguments[0]:{},e=d.userInfoApiUrl,f=e===void 0?'/uPortal/api/v5-1/userinfo':e,g=d.timeout,h=g===void 0?5e4:g,i=d.propertyTransforms,j=i===void 0?{}:i,k=1<arguments.length&&arguments[1]!==void 0?arguments[1]:function(){};return regeneratorRuntime.wrap(function(a){for(;;)switch(a.prev=a.next){case 0:if(null===token){a.next=2;break}return a.abrupt('return',token);case 2:return a.prev=2,a.next=5,(0,_axios.get)(f,{responseType:'text'});case 5:return b=a.sent,c=b.data,token=tokenize(c),Object.entries(j).forEach(function(a){var b=_slicedToArray(a,2),c=b[0],d=b[1];token[c]=d(token[c])}),setTimeout(function(){token=null},h),k(null,token),a.abrupt('return',token);case 14:if(a.prev=14,a.t0=a['catch'](2),k){a.next=18;break}throw a.t0;case 18:k(a.t0);case 19:case'end':return a.stop();}},a,this,[[2,14]])}));return function(){return a.apply(this,arguments)}}();function tokenize(a){return{encoded:a,decoded:(0,_jsonwebtoken.decode)(a)}} |
{ | ||
"name": "@uportal/open-id-connect", | ||
"description": "A shared Open ID Connect authorization helper for uPortal", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"repository": { | ||
@@ -6,0 +6,0 @@ "type": "git", |
@@ -38,3 +38,3 @@ # uPortal Open ID Connect | ||
```js | ||
var oidc = require("@uportal/open-id-connect"); | ||
var oidc = require('@uportal/open-id-connect'); | ||
@@ -69,2 +69,3 @@ // with a promise | ||
// with default values | ||
try { | ||
@@ -77,2 +78,17 @@ const {encoded, decoded} = await oidc(); | ||
} | ||
// with options | ||
try { | ||
const {encoded, decoded} = await oidc({ | ||
userInfoApiUrl: '/uPortal/api/v5-1/userinfo', | ||
timeout: 5000, | ||
propertyTransforms: { | ||
example: JSON.parse | ||
} | ||
}); | ||
console.log(encoded); | ||
console.log(decoded); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
``` | ||
@@ -89,2 +105,5 @@ | ||
* (optional) _number_ timeout - time until token should be renewed | ||
* (optional) _object_ propertyTransforms - transforms to apply to specific properties | ||
* _string_ `key` - name of property to be transformed | ||
* _function_ `value` - function to apply to property | ||
* (optional) **Callback** | ||
@@ -91,0 +110,0 @@ * _Error_ `err` - null if resonse is okay, error object otherwise |
@@ -12,2 +12,4 @@ import {get} from 'axios'; | ||
* invalid | ||
* @property {Object} [propertyTransforms] - apply additional transforms to | ||
* specific properties | ||
*/ | ||
@@ -32,3 +34,7 @@ | ||
export default async function openIdConnect( | ||
{userInfoApiUrl = '/uPortal/api/v5-1/userinfo', timeout = 50000} = {}, | ||
{ | ||
userInfoApiUrl = '/uPortal/api/v5-1/userinfo', | ||
timeout = 50000, | ||
propertyTransforms = {}, | ||
} = {}, | ||
callback = () => {} | ||
@@ -48,2 +54,7 @@ ) { | ||
// Allow for additional transforms to be applied to specific properties | ||
Object.entries(propertyTransforms).forEach(([property, transform]) => { | ||
token[property] = transform(token[property]); | ||
}); | ||
// automatically clear token after expiration | ||
@@ -50,0 +61,0 @@ setTimeout(() => { |
8517
76
115