oidc-jwt-client
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -35,3 +35,3 @@ export interface OidcJwtClientOptions { | ||
*/ | ||
haveSessionToken(): boolean; | ||
hasSessionToken(): boolean; | ||
/** | ||
@@ -38,0 +38,0 @@ * Fetch a fresh access token. |
@@ -1,2 +0,2 @@ | ||
export * from "./client"; | ||
export * from "./react-client"; | ||
export * from './client'; | ||
export * from './react-client'; |
@@ -31,4 +31,4 @@ import { createContext, useMemo, useEffect, createElement, useState, useContext } from 'react'; | ||
return Object.keys(params) | ||
.map(function (k) { return encodeURIComponent(k) + "=" + encodeURIComponent(params[k]); }) | ||
.join("&"); | ||
.map(function (k) { return encodeURIComponent(k) + '=' + encodeURIComponent(params[k]); }) | ||
.join('&'); | ||
} | ||
@@ -38,4 +38,4 @@ var OidcJwtClientImpl = /** @class */ (function () { | ||
var _a, _b; | ||
this.csrfTokenStorageKey = "oidc_jwt_provider_token"; | ||
this.baseUrl = options.url.replace(/\/$/, ""); | ||
this.csrfTokenStorageKey = 'oidc_jwt_provider_token'; | ||
this.baseUrl = options.url.replace(/\/$/, ''); | ||
this.csrfToken = (_a = sessionStorage.getItem(this.csrfTokenStorageKey)) !== null && _a !== void 0 ? _a : null; | ||
@@ -47,5 +47,5 @@ this.authorizationDefaults = (_b = options.authorizationDefaults) !== null && _b !== void 0 ? _b : {}; | ||
headers: { | ||
Authorization: "Bearer " + this.csrfToken, | ||
Authorization: 'Bearer ' + this.csrfToken, | ||
}, | ||
credentials: "include", | ||
credentials: 'include', | ||
}).then(function (response) { | ||
@@ -58,10 +58,10 @@ return response.json(); | ||
var match = window.location.search.match(/[?&]token=([^&]+)/); | ||
if (match) { | ||
this.setSessionToken(match[1]); | ||
if (redirect || typeof redirect === "undefined") { | ||
window.location.href = window.location.href | ||
.replace(/([?&])token=([^&]+)/, "$1") | ||
.replace(/\?$/, ""); | ||
return true; | ||
} | ||
if (!match) | ||
return false; | ||
this.setSessionToken(match[1]); | ||
if (redirect || typeof redirect === 'undefined') { | ||
window.location.href = window.location.href | ||
.replace(/([?&])token=([^&]+)/, '$1') | ||
.replace(/\?$/, ''); | ||
return true; | ||
} | ||
@@ -78,6 +78,5 @@ return false; | ||
if (!queryParams.redirect_uri) { | ||
queryParams.redirect_uri = window.location.href.replace(/([?&])token=([^&]+)/, "$1"); | ||
queryParams.redirect_uri = window.location.href.replace(/([?&])token=([^&]+)/, '$1'); | ||
} | ||
window.location.href = | ||
this.baseUrl + "/authorize?" + buildQuerystring(queryParams); | ||
window.location.href = this.baseUrl + '/authorize?' + buildQuerystring(queryParams); | ||
}; | ||
@@ -89,6 +88,5 @@ OidcJwtClientImpl.prototype.logout = function (params) { | ||
} | ||
window.location.href = | ||
this.baseUrl + "/logout?" + buildQuerystring(params); | ||
window.location.href = this.baseUrl + '/logout?' + buildQuerystring(params); | ||
}; | ||
OidcJwtClientImpl.prototype.haveSessionToken = function () { | ||
OidcJwtClientImpl.prototype.hasSessionToken = function () { | ||
return !!this.csrfToken; | ||
@@ -98,3 +96,3 @@ }; | ||
var fetchedAt = new Date().getTime(); | ||
this.accessTokenCache = this.fetchJsonWithAuth(this.baseUrl + "/token").then(function (result) { | ||
this.accessTokenCache = this.fetchJsonWithAuth(this.baseUrl + '/token').then(function (result) { | ||
if (!result.token) { | ||
@@ -106,4 +104,4 @@ return { value: result, validUntil: null }; | ||
if (claims && | ||
typeof claims.iat === "number" && | ||
typeof claims.exp === "number") { | ||
typeof claims.iat === 'number' && | ||
typeof claims.exp === 'number') { | ||
validUntil = fetchedAt + 1000 * (claims.exp - claims.iat); | ||
@@ -116,6 +114,6 @@ } | ||
OidcJwtClientImpl.prototype.fetchUserInfo = function () { | ||
this.userInfoCache = this.fetchJsonWithAuth(this.baseUrl + "/userinfo").then(function (result) { | ||
this.userInfoCache = this.fetchJsonWithAuth(this.baseUrl + '/userinfo').then(function (result) { | ||
var _a; | ||
if (result.status && result.status === "error") { | ||
throw new Error((_a = result.message) !== null && _a !== void 0 ? _a : "Unknown error fetching userinfo"); | ||
if (result.status && result.status === 'error') { | ||
throw new Error((_a = result.message) !== null && _a !== void 0 ? _a : 'Unknown error fetching userinfo'); | ||
} | ||
@@ -176,3 +174,3 @@ return result; | ||
function isClientOptions(obj) { | ||
return "url" in obj; | ||
return 'url' in obj; | ||
} | ||
@@ -190,8 +188,6 @@ var OidcJwtProvider = function (props) { | ||
useEffect(function () { | ||
if (client.receiveSessionToken()) { | ||
return; | ||
} | ||
client.receiveSessionToken(); | ||
}, [client]); | ||
useEffect(function () { | ||
if (client.haveSessionToken()) { | ||
if (client.hasSessionToken()) { | ||
if (shouldMonitorAccessTokens) { | ||
@@ -205,3 +201,3 @@ client.monitorAccessToken(); | ||
if (!shouldRequireLogin) { | ||
params.prompt = "none"; | ||
params.prompt = 'none'; | ||
} | ||
@@ -238,2 +234,3 @@ client.authorize(params); | ||
}); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, deps); | ||
@@ -248,14 +245,12 @@ return value; | ||
var client = useAuthClient(); | ||
return useMemo(function () { | ||
return { | ||
authorize: function (params) { | ||
if (params === void 0) { params = {}; } | ||
client === null || client === void 0 ? void 0 : client.authorize(params); | ||
}, | ||
logout: function (params) { | ||
if (params === void 0) { params = {}; } | ||
client === null || client === void 0 ? void 0 : client.logout(params); | ||
}, | ||
}; | ||
}, [client]); | ||
return useMemo(function () { return ({ | ||
authorize: function (params) { | ||
if (params === void 0) { params = {}; } | ||
client === null || client === void 0 ? void 0 : client.authorize(params); | ||
}, | ||
logout: function (params) { | ||
if (params === void 0) { params = {}; } | ||
client === null || client === void 0 ? void 0 : client.logout(params); | ||
}, | ||
}); }, [client]); | ||
} | ||
@@ -265,5 +260,4 @@ function useAuthUserInfo() { | ||
return usePromiseResult(function () { | ||
if (!client) { | ||
if (!client) | ||
return null; | ||
} | ||
return client.getUserInfo(); | ||
@@ -275,5 +269,4 @@ }, [client]); | ||
return usePromiseResult(function () { | ||
if (!client) { | ||
if (!client) | ||
return null; | ||
} | ||
return client.getAccessToken().then(function (info) { var _a; return (_a = info === null || info === void 0 ? void 0 : info.claims) !== null && _a !== void 0 ? _a : null; }); | ||
@@ -286,10 +279,7 @@ }, [client]); | ||
return function () { | ||
if (client) { | ||
return client | ||
.getAccessToken() | ||
.then(function (result) { var _a; return (_a = result === null || result === void 0 ? void 0 : result.token) !== null && _a !== void 0 ? _a : null; }); | ||
} | ||
else { | ||
if (!client) | ||
return Promise.resolve(null); | ||
} | ||
return client | ||
.getAccessToken() | ||
.then(function (result) { var _a; return (_a = result === null || result === void 0 ? void 0 : result.token) !== null && _a !== void 0 ? _a : null; }); | ||
}; | ||
@@ -296,0 +286,0 @@ }, [client]); |
@@ -35,4 +35,4 @@ 'use strict'; | ||
return Object.keys(params) | ||
.map(function (k) { return encodeURIComponent(k) + "=" + encodeURIComponent(params[k]); }) | ||
.join("&"); | ||
.map(function (k) { return encodeURIComponent(k) + '=' + encodeURIComponent(params[k]); }) | ||
.join('&'); | ||
} | ||
@@ -42,4 +42,4 @@ var OidcJwtClientImpl = /** @class */ (function () { | ||
var _a, _b; | ||
this.csrfTokenStorageKey = "oidc_jwt_provider_token"; | ||
this.baseUrl = options.url.replace(/\/$/, ""); | ||
this.csrfTokenStorageKey = 'oidc_jwt_provider_token'; | ||
this.baseUrl = options.url.replace(/\/$/, ''); | ||
this.csrfToken = (_a = sessionStorage.getItem(this.csrfTokenStorageKey)) !== null && _a !== void 0 ? _a : null; | ||
@@ -51,5 +51,5 @@ this.authorizationDefaults = (_b = options.authorizationDefaults) !== null && _b !== void 0 ? _b : {}; | ||
headers: { | ||
Authorization: "Bearer " + this.csrfToken, | ||
Authorization: 'Bearer ' + this.csrfToken, | ||
}, | ||
credentials: "include", | ||
credentials: 'include', | ||
}).then(function (response) { | ||
@@ -62,10 +62,10 @@ return response.json(); | ||
var match = window.location.search.match(/[?&]token=([^&]+)/); | ||
if (match) { | ||
this.setSessionToken(match[1]); | ||
if (redirect || typeof redirect === "undefined") { | ||
window.location.href = window.location.href | ||
.replace(/([?&])token=([^&]+)/, "$1") | ||
.replace(/\?$/, ""); | ||
return true; | ||
} | ||
if (!match) | ||
return false; | ||
this.setSessionToken(match[1]); | ||
if (redirect || typeof redirect === 'undefined') { | ||
window.location.href = window.location.href | ||
.replace(/([?&])token=([^&]+)/, '$1') | ||
.replace(/\?$/, ''); | ||
return true; | ||
} | ||
@@ -82,6 +82,5 @@ return false; | ||
if (!queryParams.redirect_uri) { | ||
queryParams.redirect_uri = window.location.href.replace(/([?&])token=([^&]+)/, "$1"); | ||
queryParams.redirect_uri = window.location.href.replace(/([?&])token=([^&]+)/, '$1'); | ||
} | ||
window.location.href = | ||
this.baseUrl + "/authorize?" + buildQuerystring(queryParams); | ||
window.location.href = this.baseUrl + '/authorize?' + buildQuerystring(queryParams); | ||
}; | ||
@@ -93,6 +92,5 @@ OidcJwtClientImpl.prototype.logout = function (params) { | ||
} | ||
window.location.href = | ||
this.baseUrl + "/logout?" + buildQuerystring(params); | ||
window.location.href = this.baseUrl + '/logout?' + buildQuerystring(params); | ||
}; | ||
OidcJwtClientImpl.prototype.haveSessionToken = function () { | ||
OidcJwtClientImpl.prototype.hasSessionToken = function () { | ||
return !!this.csrfToken; | ||
@@ -102,3 +100,3 @@ }; | ||
var fetchedAt = new Date().getTime(); | ||
this.accessTokenCache = this.fetchJsonWithAuth(this.baseUrl + "/token").then(function (result) { | ||
this.accessTokenCache = this.fetchJsonWithAuth(this.baseUrl + '/token').then(function (result) { | ||
if (!result.token) { | ||
@@ -110,4 +108,4 @@ return { value: result, validUntil: null }; | ||
if (claims && | ||
typeof claims.iat === "number" && | ||
typeof claims.exp === "number") { | ||
typeof claims.iat === 'number' && | ||
typeof claims.exp === 'number') { | ||
validUntil = fetchedAt + 1000 * (claims.exp - claims.iat); | ||
@@ -120,6 +118,6 @@ } | ||
OidcJwtClientImpl.prototype.fetchUserInfo = function () { | ||
this.userInfoCache = this.fetchJsonWithAuth(this.baseUrl + "/userinfo").then(function (result) { | ||
this.userInfoCache = this.fetchJsonWithAuth(this.baseUrl + '/userinfo').then(function (result) { | ||
var _a; | ||
if (result.status && result.status === "error") { | ||
throw new Error((_a = result.message) !== null && _a !== void 0 ? _a : "Unknown error fetching userinfo"); | ||
if (result.status && result.status === 'error') { | ||
throw new Error((_a = result.message) !== null && _a !== void 0 ? _a : 'Unknown error fetching userinfo'); | ||
} | ||
@@ -180,3 +178,3 @@ return result; | ||
function isClientOptions(obj) { | ||
return "url" in obj; | ||
return 'url' in obj; | ||
} | ||
@@ -194,8 +192,6 @@ var OidcJwtProvider = function (props) { | ||
React.useEffect(function () { | ||
if (client.receiveSessionToken()) { | ||
return; | ||
} | ||
client.receiveSessionToken(); | ||
}, [client]); | ||
React.useEffect(function () { | ||
if (client.haveSessionToken()) { | ||
if (client.hasSessionToken()) { | ||
if (shouldMonitorAccessTokens) { | ||
@@ -209,3 +205,3 @@ client.monitorAccessToken(); | ||
if (!shouldRequireLogin) { | ||
params.prompt = "none"; | ||
params.prompt = 'none'; | ||
} | ||
@@ -242,2 +238,3 @@ client.authorize(params); | ||
}); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, deps); | ||
@@ -252,14 +249,12 @@ return value; | ||
var client = useAuthClient(); | ||
return React.useMemo(function () { | ||
return { | ||
authorize: function (params) { | ||
if (params === void 0) { params = {}; } | ||
client === null || client === void 0 ? void 0 : client.authorize(params); | ||
}, | ||
logout: function (params) { | ||
if (params === void 0) { params = {}; } | ||
client === null || client === void 0 ? void 0 : client.logout(params); | ||
}, | ||
}; | ||
}, [client]); | ||
return React.useMemo(function () { return ({ | ||
authorize: function (params) { | ||
if (params === void 0) { params = {}; } | ||
client === null || client === void 0 ? void 0 : client.authorize(params); | ||
}, | ||
logout: function (params) { | ||
if (params === void 0) { params = {}; } | ||
client === null || client === void 0 ? void 0 : client.logout(params); | ||
}, | ||
}); }, [client]); | ||
} | ||
@@ -269,5 +264,4 @@ function useAuthUserInfo() { | ||
return usePromiseResult(function () { | ||
if (!client) { | ||
if (!client) | ||
return null; | ||
} | ||
return client.getUserInfo(); | ||
@@ -279,5 +273,4 @@ }, [client]); | ||
return usePromiseResult(function () { | ||
if (!client) { | ||
if (!client) | ||
return null; | ||
} | ||
return client.getAccessToken().then(function (info) { var _a; return (_a = info === null || info === void 0 ? void 0 : info.claims) !== null && _a !== void 0 ? _a : null; }); | ||
@@ -290,10 +283,7 @@ }, [client]); | ||
return function () { | ||
if (client) { | ||
return client | ||
.getAccessToken() | ||
.then(function (result) { var _a; return (_a = result === null || result === void 0 ? void 0 : result.token) !== null && _a !== void 0 ? _a : null; }); | ||
} | ||
else { | ||
if (!client) | ||
return Promise.resolve(null); | ||
} | ||
return client | ||
.getAccessToken() | ||
.then(function (result) { var _a; return (_a = result === null || result === void 0 ? void 0 : result.token) !== null && _a !== void 0 ? _a : null; }); | ||
}; | ||
@@ -300,0 +290,0 @@ }, [client]); |
@@ -1,3 +0,3 @@ | ||
import * as React from "react"; | ||
import { OidcJwtClient, OidcJwtClientOptions } from "./client"; | ||
import * as React from 'react'; | ||
import { OidcJwtClient, OidcJwtClientOptions } from './client'; | ||
export interface OidcJwtProviderProps { | ||
@@ -4,0 +4,0 @@ client: OidcJwtClient | OidcJwtClientOptions; |
{ | ||
"name": "oidc-jwt-client", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Fetch JWTs for API access from oidc-jwt-provider", | ||
@@ -11,5 +11,12 @@ "main": "dist/index.js", | ||
], | ||
"author": "Sebastiaan Besselsen <s.besselsen@sdu.nl>", | ||
"license": "MIT", | ||
"contributors": [ | ||
"Lennard Westerveld <L.Westerveld@sdu.nl>", | ||
"Daphne Smit <d.smit@sdu.nl>" | ||
], | ||
"scripts": { | ||
"format": "prettier --config .prettierrc 'src/**/*.ts' --write", | ||
"lint": "eslint . --ext .ts", | ||
"lint": "eslint src --ext .ts,.tsx,.js,.jsx", | ||
"lint-fix": "eslint --fix src --ext .ts,.tsx,.js,.jsx", | ||
"type-check": "tsc --noEmit", | ||
"build": "rimraf dist; rollup -c; rimraf \"dist/**/*.stories.d.ts\"; rimraf \"dist/stories\"", | ||
@@ -21,4 +28,2 @@ "storybook": "start-storybook -p 6006", | ||
}, | ||
"author": "Sebastiaan Besselsen <s.besselsen@sdu.nl>", | ||
"license": "MIT", | ||
"devDependencies": { | ||
@@ -34,5 +39,15 @@ "@babel/core": "^7.11.5", | ||
"babel-loader": "^8.1.0", | ||
"eslint": "^7.7.0", | ||
"eslint-config-prettier": "^6.11.0", | ||
"eslint-plugin-prettier": "^3.1.4", | ||
"eslint": "^6.8.0", | ||
"eslint-config-react-app": "^5.2.1", | ||
"eslint-config-standard": "^14.1.1", | ||
"eslint-import-resolver-typescript": "^2.3.0", | ||
"eslint-plugin-flowtype": "^5.2.0", | ||
"eslint-plugin-import": "^2.22.0", | ||
"eslint-plugin-jsx-a11y": "^6.3.1", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-promise": "^4.2.1", | ||
"eslint-plugin-react": "^7.20.6", | ||
"eslint-plugin-react-hooks": "^4.1.2", | ||
"eslint-plugin-simple-import-sort": "^5.0.3", | ||
"eslint-plugin-standard": "^4.0.1", | ||
"husky": "^4.2.5", | ||
@@ -49,2 +64,3 @@ "prettier": "^2.1.1", | ||
"rollup-plugin-typescript2": "^0.27.2", | ||
"sdu-react-scripts": "^1.2.0", | ||
"semantic-release": "^17.1.1", | ||
@@ -68,5 +84,11 @@ "typescript": "^4.0.2" | ||
}, | ||
"lint-staged": { | ||
"**/*.(js|ts|tsx)": [ | ||
"eslint --fix" | ||
] | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "npm run format && npm run lint" | ||
"pre-commit": "npx lint-staged", | ||
"pre-push": "npm run type-check" | ||
} | ||
@@ -73,0 +95,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
54184
9
1
67
36
634