-
Notifications
You must be signed in to change notification settings - Fork 9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): persist cookie based apiKey in document.cookie (#8689)
Refs #8683
- Loading branch information
Showing
10 changed files
with
269 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* @prettier | ||
*/ | ||
export const loaded = (oriAction, system) => (payload) => { | ||
const { getConfigs, authActions } = system | ||
const configs = getConfigs() | ||
|
||
oriAction(payload) | ||
|
||
// check if we should restore authorization data from localStorage | ||
if (configs.persistAuthorization) { | ||
const authorized = localStorage.getItem("authorized") | ||
if (authorized) { | ||
authActions.restoreAuthorization({ | ||
authorized: JSON.parse(authorized), | ||
}) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* @prettier | ||
*/ | ||
|
||
/** | ||
* `authorize` and `logout` wrapped actions provide capacity | ||
* to persist cookie based apiKey in document.cookie. | ||
* | ||
* `persistAuthorization` SwaggerUI options needs to set to `true` | ||
* for document.cookie persistence to work. | ||
*/ | ||
export const authorize = (oriAction, system) => (payload) => { | ||
oriAction(payload) | ||
|
||
const configs = system.getConfigs() | ||
|
||
if (!configs.persistAuthorization) return | ||
|
||
// create cookie | ||
try { | ||
const [{ schema, value }] = Object.values(payload) | ||
const isApiKeyAuth = schema.get("type") === "apiKey" | ||
const isInCookie = schema.get("in") === "cookie" | ||
const isApiKeyInCookie = isApiKeyAuth && isInCookie | ||
|
||
if (isApiKeyInCookie) { | ||
document.cookie = `${schema.get("name")}=${value}; SameSite=None; Secure` | ||
} | ||
} catch (error) { | ||
console.error( | ||
"Error persisting cookie based apiKey in document.cookie.", | ||
error | ||
) | ||
} | ||
} | ||
|
||
export const logout = (oriAction, system) => (payload) => { | ||
const configs = system.getConfigs() | ||
const authorized = system.authSelectors.authorized() | ||
|
||
// deleting cookie | ||
try { | ||
if (configs.persistAuthorization && Array.isArray(payload)) { | ||
payload.forEach((authorizedName) => { | ||
const auth = authorized.get(authorizedName, {}) | ||
const isApiKeyAuth = auth.getIn(["schema", "type"]) === "apiKey" | ||
const isInCookie = auth.getIn(["schema", "in"]) === "cookie" | ||
const isApiKeyInCookie = isApiKeyAuth && isInCookie | ||
|
||
if (isApiKeyInCookie) { | ||
const cookieName = auth.getIn(["schema", "name"]) | ||
document.cookie = `${cookieName}=; Max-Age=-99999999` | ||
} | ||
}) | ||
} | ||
} catch (error) { | ||
console.error( | ||
"Error deleting cookie based apiKey from document.cookie.", | ||
error | ||
) | ||
} | ||
|
||
oriAction(payload) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
test/unit/core/plugins/auth/configs-extensions/wrap-actions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { loaded } from "corePlugins/auth/configs-extensions/wrap-actions" | ||
|
||
describe("loaded hook", () => { | ||
describe("authorization data restoration", () => { | ||
beforeEach(() => { | ||
localStorage.clear() | ||
}) | ||
it("retrieve `authorized` value from `localStorage`", () => { | ||
const system = { | ||
getConfigs: () => ({ | ||
persistAuthorization: true | ||
}), | ||
authActions: { | ||
|
||
} | ||
} | ||
jest.spyOn(Object.getPrototypeOf(window.localStorage), "getItem") | ||
|
||
loaded(jest.fn(), system)() | ||
expect(localStorage.getItem).toHaveBeenCalled() | ||
expect(localStorage.getItem).toHaveBeenCalledWith("authorized") | ||
}) | ||
it("restore authorization data when a value exists", () => { | ||
const system = { | ||
getConfigs: () => ({ | ||
persistAuthorization: true | ||
}), | ||
authActions: { | ||
restoreAuthorization: jest.fn(() => {}) | ||
} | ||
} | ||
const mockData = {"api_key": {}} | ||
localStorage.setItem("authorized", JSON.stringify(mockData)) | ||
loaded(jest.fn(), system)() | ||
expect(system.authActions.restoreAuthorization).toHaveBeenCalled() | ||
expect(system.authActions.restoreAuthorization).toHaveBeenCalledWith({ | ||
authorized: mockData | ||
}) | ||
}) | ||
}) | ||
}) |
3 changes: 1 addition & 2 deletions
3
...it/core/plugins/auth/wrap-spec-actions.js → ...gins/auth/spec-extensions/wrap-actions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/** | ||
* @prettier | ||
*/ | ||
import { fromJS } from "immutable" | ||
import { authorize, logout } from "corePlugins/auth/wrap-actions" | ||
|
||
describe("Cookie based apiKey persistence in document.cookie", () => { | ||
beforeEach(() => { | ||
let cookieJar = "" | ||
jest.spyOn(document, "cookie", "set").mockImplementation((cookie) => { | ||
cookieJar += cookie | ||
}) | ||
jest.spyOn(document, "cookie", "get").mockImplementation(() => cookieJar) | ||
}) | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks() | ||
}) | ||
|
||
describe("given persistAuthorization=true", () => { | ||
it("should persist cookie in document.cookie", () => { | ||
const system = { | ||
getConfigs: () => ({ | ||
persistAuthorization: true, | ||
}), | ||
} | ||
const payload = { | ||
api_key: { | ||
schema: fromJS({ | ||
type: "apiKey", | ||
name: "apiKeyCookie", | ||
in: "cookie", | ||
}), | ||
value: "test", | ||
}, | ||
} | ||
|
||
authorize(jest.fn(), system)(payload) | ||
|
||
expect(document.cookie).toEqual( | ||
"apiKeyCookie=test; SameSite=None; Secure" | ||
) | ||
}) | ||
|
||
it("should delete cookie from document.cookie", () => { | ||
const payload = fromJS({ | ||
api_key: { | ||
schema: { | ||
type: "apiKey", | ||
name: "apiKeyCookie", | ||
in: "cookie", | ||
}, | ||
value: "test", | ||
}, | ||
}) | ||
const system = { | ||
getConfigs: () => ({ | ||
persistAuthorization: true, | ||
}), | ||
authSelectors: { | ||
authorized: () => payload, | ||
}, | ||
} | ||
|
||
logout(jest.fn(), system)(["api_key"]) | ||
|
||
expect(document.cookie).toEqual("apiKeyCookie=; Max-Age=-99999999") | ||
}) | ||
}) | ||
|
||
describe("given persistAuthorization=false", () => { | ||
it("shouldn't persist cookie in document.cookie", () => { | ||
const system = { | ||
getConfigs: () => ({ | ||
persistAuthorization: false, | ||
}), | ||
} | ||
const payload = { | ||
api_key: { | ||
schema: fromJS({ | ||
type: "apiKey", | ||
name: "apiKeyCookie", | ||
in: "cookie", | ||
}), | ||
value: "test", | ||
}, | ||
} | ||
|
||
authorize(jest.fn(), system)(payload) | ||
|
||
expect(document.cookie).toEqual("") | ||
}) | ||
|
||
it("should delete cookie from document.cookie", () => { | ||
const payload = fromJS({ | ||
api_key: { | ||
schema: { | ||
type: "apiKey", | ||
name: "apiKeyCookie", | ||
in: "cookie", | ||
}, | ||
value: "test", | ||
}, | ||
}) | ||
const system = { | ||
getConfigs: () => ({ | ||
persistAuthorization: false, | ||
}), | ||
authSelectors: { | ||
authorized: () => payload, | ||
}, | ||
} | ||
|
||
logout(jest.fn(), system)(["api_key"]) | ||
|
||
expect(document.cookie).toEqual("") | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters