Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #821 from derrabauke/fix-oidc-auth-stuff
Browse files Browse the repository at this point in the history
fix: delete unneeded oidc adapter overrides and simplify other auth procedures
  • Loading branch information
czosel authored Feb 17, 2023
2 parents f898e99 + 801e1ae commit 96e12f8
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 35 deletions.
20 changes: 0 additions & 20 deletions app/adapters/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* @submodule timed-adapters
* @public
*/
import { inject as service } from "@ember/service";
import OIDCJSONAPIAdapter from "ember-simple-auth-oidc/adapters/oidc-json-api-adapter";

/**
Expand All @@ -15,24 +14,5 @@ import OIDCJSONAPIAdapter from "ember-simple-auth-oidc/adapters/oidc-json-api-ad
* @public
*/
export default class ApplicationAdapter extends OIDCJSONAPIAdapter {
@service session;
@service router;

namespace = "api/v1";

get headers() {
return { ...this.session.headers };
}

handleResponse(status, ...args) {
if (status === 401) {
if (this.session.isAuthenticated) {
this.session.invalidate();
} else {
this.router.transitionTo("login");
}
}

return super.handleResponse(status, ...args);
}
}
1 change: 1 addition & 0 deletions app/analysis/index/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { cleanParams, toQueryString } from "timed/utils/url";
import config from "../../config/environment";

const rAF = () => {
/* istanbul ignore next */
return new Promise((resolve) => {
window.requestAnimationFrame(resolve);
});
Expand Down
13 changes: 2 additions & 11 deletions app/services/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,10 @@ export default class FetchService extends Service {
}

get headers() {
const headers = {
return {
accept: CONTENT_TYPE,
"content-type": CONTENT_TYPE,
...this.session.headers,
};

if (this.token) {
headers.authorization = `Bearer ${this.token}`;
}

return headers;
}

get token() {
return this.session.data.authenticated?.access_token;
}
}
14 changes: 14 additions & 0 deletions tests/helpers/session-mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Service from "@ember/service";

class SessionMock extends Service {
isAuthenticated = true;
headers = {
authorization: "Bearer TEST1234",
};
}

export default function setupSession(hooks) {
hooks.beforeEach(async function () {
this.owner.register("service:session", SessionMock);
});
}
11 changes: 7 additions & 4 deletions tests/unit/services/fetch-test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { setupTest } from "ember-qunit";
import { module, test } from "qunit";
import setupSession from "timed/tests/helpers/session-mock";

module("Unit | Service | fetch", function (hooks) {
setupTest(hooks);
setupSession(hooks);

test("exists", function (assert) {
const service = this.owner.lookup("service:fetch");
Expand All @@ -13,16 +15,17 @@ module("Unit | Service | fetch", function (hooks) {
const service = this.owner.lookup("service:fetch");
const session = this.owner.lookup("service:session");

session.data.authenticated = { access_token: "test" };

assert.equal(service.get("headers.authorization"), "Bearer test");
assert.equal(
service.get("headers.authorization"),
session.headers.authorization
);
});

test("does not add the auth token to the headers if no token is given", function (assert) {
const service = this.owner.lookup("service:fetch");
const session = this.owner.lookup("service:session");

session.data.authenticated = { access_token: null };
delete session.headers.authorization;

assert.notOk(service.get("headers.authorization"));
});
Expand Down

0 comments on commit 96e12f8

Please sign in to comment.