@cimpress/simple-auth-wrapper
Advanced tools
Comparing version 6.4.3 to 6.4.4
@@ -27,3 +27,3 @@ "use strict"; | ||
_ref$audience = _ref.audience, | ||
audience = _ref$audience === undefined ? 'https://api.cimpress.io/' : _ref$audience, | ||
audience = _ref$audience === undefined ? "https://api.cimpress.io/" : _ref$audience, | ||
_ref$scope = _ref.scope, | ||
@@ -60,2 +60,6 @@ scope = _ref$scope === undefined ? "offline_access" : _ref$scope; | ||
value: function handleAuthentication() { | ||
var _ref3 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, | ||
_ref3$performRedirect = _ref3.performRedirect, | ||
performRedirect = _ref3$performRedirect === undefined ? true : _ref3$performRedirect; | ||
var authorizationCode = localStorage.getItem("authorizationCode"); | ||
@@ -70,3 +74,3 @@ var nextUri = localStorage.getItem("nextUri"); | ||
} else if (this.wasAuth0Redirect()) { | ||
return this.attemptRedirect(); | ||
return this.handleRedirect({ performRedirect: performRedirect }); | ||
} | ||
@@ -83,16 +87,20 @@ | ||
}, { | ||
key: "attemptRedirect", | ||
value: function attemptRedirect() { | ||
key: "handleRedirect", | ||
value: function handleRedirect(_ref4) { | ||
var performRedirect = _ref4.performRedirect; | ||
var parsedUrl = new URL(window.location.href); | ||
var code = parsedUrl.searchParams.get("code"); | ||
localStorage.setItem("authorizationCode", code); | ||
var authorizationCode = parsedUrl.searchParams.get("code"); | ||
var state = parsedUrl.searchParams.get("state"); | ||
var nextUri = atob(state); | ||
localStorage.setItem("nextUri", nextUri); | ||
window.location = nextUri || "/"; | ||
if (performRedirect) { | ||
localStorage.setItem("authorizationCode", authorizationCode); | ||
localStorage.setItem("nextUri", nextUri); | ||
window.location = nextUri || "/"; | ||
return _Promise.resolve({}); | ||
return _Promise.resolve({}); | ||
} | ||
return _Promise.resolve({ authorizationCode: authorizationCode, redirectUri: this.redirectUri, nextUri: nextUri }); | ||
} | ||
@@ -99,0 +107,0 @@ }]); |
{ | ||
"name": "@cimpress/simple-auth-wrapper", | ||
"version": "6.4.3", | ||
"version": "6.4.4", | ||
"description": "A simple utility class to wrap basic Auth0 functionality", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -133,2 +133,6 @@ # MEX Simple Auth Wrapper v. 3.0 | ||
`handleAuthentication()` Resumes the authorization code grant flow. Should be called at the base of your application, or at the `redirectUri` specified to continue the flow. Returns a Promise of `{ authorizationCode, redirectUri, nextUri }` | ||
`handleAuthentication(options)` Resumes the authorization code grant flow. Should be called at the base of your application, or at the `redirectUri` specified to continue the flow. Returns a Promise of `{ authorizationCode, redirectUri, nextUri }` | ||
| Name | Type | Default | Description | | ||
|-------------------|---------|---------|--------------------------------------------------------------------------| | ||
| `performRedirect` | Boolean | true | Indicates if the wrapper should redirect back to the specified `nextUri` | |
@@ -1,2 +0,2 @@ | ||
import { WebAuth } from 'auth0-js'; | ||
import { WebAuth } from "auth0-js"; | ||
@@ -8,3 +8,2 @@ // if promise hasn't been polyfilled polyfill it just for this file | ||
export default class AuthorizationCodeGrant { | ||
constructor({ | ||
@@ -14,3 +13,3 @@ clientID, | ||
domain = "cimpress.auth0.com", | ||
audience = 'https://api.cimpress.io/', | ||
audience = "https://api.cimpress.io/", | ||
scope = "offline_access" | ||
@@ -38,3 +37,5 @@ } = {}) { | ||
handleAuthentication() { | ||
handleAuthentication({ | ||
performRedirect = true | ||
} = {}) { | ||
const authorizationCode = localStorage.getItem("authorizationCode"); | ||
@@ -48,6 +49,6 @@ const nextUri = localStorage.getItem("nextUri"); | ||
return _Promise.resolve({ authorizationCode, redirectUri: this.redirectUri, nextUri }); | ||
} | ||
} | ||
else if (this.wasAuth0Redirect()) { | ||
return this.attemptRedirect(); | ||
return this.handleRedirect({ performRedirect }); | ||
} | ||
@@ -63,16 +64,18 @@ | ||
attemptRedirect() { | ||
handleRedirect({ performRedirect }) { | ||
const parsedUrl = new URL(window.location.href); | ||
const code = parsedUrl.searchParams.get("code"); | ||
localStorage.setItem("authorizationCode", code); | ||
const authorizationCode = parsedUrl.searchParams.get("code"); | ||
const state = parsedUrl.searchParams.get("state"); | ||
const nextUri = atob(state); | ||
localStorage.setItem("nextUri", nextUri); | ||
window.location = nextUri || "/"; | ||
if (performRedirect) { | ||
localStorage.setItem("authorizationCode", authorizationCode); | ||
localStorage.setItem("nextUri", nextUri); | ||
window.location = nextUri || "/"; | ||
return _Promise.resolve({}); | ||
return _Promise.resolve({}); | ||
} | ||
return _Promise.resolve({ authorizationCode, redirectUri: this.redirectUri, nextUri }); | ||
} | ||
} | ||
} |
@@ -16,3 +16,3 @@ import AuthorizationCodeGrant from "../src/authorizationcodegrant"; | ||
const authorizeUserSpy = jest.spyOn(testModule.auth0, "authorize"); | ||
const attemptRedirectSpy = jest.spyOn(testModule, "attemptRedirect"); | ||
const handleRedirectSpy = jest.spyOn(testModule, "handleRedirect"); | ||
@@ -26,3 +26,3 @@ return testModule | ||
.then(() => { | ||
expect(attemptRedirectSpy).toHaveBeenCalled(); | ||
expect(handleRedirectSpy).toHaveBeenCalled(); | ||
return testModule.handleAuthentication(); | ||
@@ -42,3 +42,2 @@ }) | ||
test("should redirect when an authorization code is in the query string", () => { | ||
const authorizationCode = "code"; | ||
@@ -48,7 +47,7 @@ const nextUri = "/someroute"; | ||
const attemptRedirectSpy = jest.spyOn(testModule, "attemptRedirect"); | ||
const handleRedirectSpy = jest.spyOn(testModule, "handleRedirect"); | ||
return testModule.handleAuthentication() | ||
.then(() => { | ||
expect(attemptRedirectSpy).toHaveBeenCalled(); | ||
expect(handleRedirectSpy).toHaveBeenCalled(); | ||
expect(localStorage.getItem("authorizationCode")).toBe(authorizationCode); | ||
@@ -74,2 +73,15 @@ expect(localStorage.getItem("nextUri")).toBe(nextUri); | ||
test("should return the authorization code when redirection is disabled", () => { | ||
const expectedAuthorizationCode = "code"; | ||
const expectedNextUri = "/someroute"; | ||
window.history.pushState({}, "", `/?code=${expectedAuthorizationCode}&state=${btoa(expectedNextUri)}`); | ||
return testModule.handleAuthentication({ performRedirect: false }) | ||
.then(({ authorizationCode, nextUri, redirectUri }) => { | ||
expect(authorizationCode).toBe(expectedAuthorizationCode); | ||
expect(nextUri).toBe(expectedNextUri); | ||
expect(redirectUri).toBe(testModule.redirectUri); | ||
}); | ||
}); | ||
test("should noop if no redirect occurred and nothing is in localStorage", () => { | ||
@@ -76,0 +88,0 @@ return testModule.handleAuthentication() |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
295232
1567
137