Skip to content

Commit

Permalink
refactor(authenticator): replace ember-ajax with ember-fetch (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornharrtell authored Apr 22, 2020
1 parent a9af00d commit 0bda959
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 81 deletions.
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ jobs:
- ember-release
- ember-beta
- ember-canary
- ember-default-with-jquery
- ember-classic

steps:
Expand Down
77 changes: 50 additions & 27 deletions addon/authenticators/oidc.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { computed } from "@ember/object";
import { later, cancel } from "@ember/runloop";
import { inject as service } from "@ember/service";
import { isServerError, isAbortError, isTimeoutError } from "ember-ajax/errors";
import { isServerErrorResponse, isAbortError } from "ember-fetch/errors";
import config from "ember-simple-auth-oidc/config";
import getAbsoluteUrl from "ember-simple-auth-oidc/utils/absoluteUrl";
import BaseAuthenticator from "ember-simple-auth/authenticators/base";
import Configuration from "ember-simple-auth/configuration";
import fetch from "fetch";
import { resolve } from "rsvp";

const {
Expand All @@ -23,7 +24,6 @@ const {
const getUrl = endpoint => `${getAbsoluteUrl(host)}${endpoint}`;

export default BaseAuthenticator.extend({
ajax: service(),
router: service(),

_upcomingRefresh: null,
Expand All @@ -50,17 +50,27 @@ export default BaseAuthenticator.extend({
);
}

const data = await this.get("ajax").post(getUrl(tokenEndpoint), {
responseType: "application/json",
contentType: "application/x-www-form-urlencoded",
data: {
code,
client_id: clientId,
grant_type: "authorization_code",
redirect_uri: this.redirectUri
}
const bodyObject = {
code,
client_id: clientId,
grant_type: "authorization_code",
redirect_uri: this.redirectUri
};
const body = Object.keys(bodyObject)
.map(k => `${k}=${encodeURIComponent(bodyObject[k])}`)
.join("&");

const response = await fetch(getUrl(tokenEndpoint), {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/x-www-form-urlencoded"
},
body
});

const data = await response.json();

return this._handleAuthResponse(data);
},

Expand Down Expand Up @@ -106,22 +116,31 @@ export default BaseAuthenticator.extend({
*/
async _refresh(refresh_token, retryCount = 0) {
try {
const data = await this.get("ajax").post(getUrl(tokenEndpoint), {
responseType: "application/json",
contentType: "application/x-www-form-urlencoded",
data: {
refresh_token,
client_id: clientId,
grant_type: "refresh_token",
redirect_uri: this.redirectUri
}
const bodyObject = {
refresh_token,
client_id: clientId,
grant_type: "refresh_token",
redirect_uri: this.redirectUri
};
const body = Object.keys(bodyObject)
.map(k => `${k}=${encodeURIComponent(bodyObject[k])}`)
.join("&");

const response = await fetch(getUrl(tokenEndpoint), {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/x-www-form-urlencoded"
},
body
});
if (isServerErrorResponse(response)) throw new Error(response.message);

const data = await response.json();

return this._handleAuthResponse(data);
} catch (e) {
if (
(isServerError(e) || isAbortError(e) || isTimeoutError(e)) &&
retryCount < amountOfRetries - 1
) {
if (isAbortError(e) && retryCount < amountOfRetries - 1) {
return new Promise(resolve => {
later(
this,
Expand Down Expand Up @@ -168,11 +187,15 @@ export default BaseAuthenticator.extend({
* @returns {Object} Object containing the user information
*/
async _getUserinfo(accessToken) {
const userinfo = await this.get("ajax").request(getUrl(userinfoEndpoint), {
headers: { Authorization: `${authPrefix} ${accessToken}` },
responseType: "application/json"
const response = await fetch(getUrl(userinfoEndpoint), {
headers: {
Authorization: `${authPrefix} ${accessToken}`,
Accept: "application/json"
}
});

const userinfo = await response.json();

return userinfo;
},

Expand Down
13 changes: 0 additions & 13 deletions config/ember-try.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,6 @@ module.exports = async function() {
devDependencies: {}
}
},
{
name: "ember-default-with-jquery",
env: {
EMBER_OPTIONAL_FEATURES: JSON.stringify({
"jquery-integration": true
})
},
npm: {
devDependencies: {
"@ember/jquery": "^0.5.1"
}
}
},
{
name: "ember-classic",
env: {
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
}
},
"dependencies": {
"ember-ajax": "^5.0.0",
"ember-fetch": "^8.0.1",
"ember-auto-import": "^1.5.3",
"ember-cli-babel": "^7.13.0",
"ember-cli-htmlbars": "^4.2.2",
Expand All @@ -38,7 +38,6 @@
"devDependencies": {
"@adfinis-sygroup/eslint-config": "1.2.0",
"@adfinis-sygroup/semantic-release-config": "2.1.4",
"@ember/jquery": "1.1.0",
"@ember/optional-features": "1.3.0",
"broccoli-asset-rev": "3.0.0",
"ember-cli": "~3.15.1",
Expand Down
2 changes: 1 addition & 1 deletion tests/dummy/config/optional-features.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"application-template-wrapper": false,
"default-async-observers": true,
"jquery-integration": true,
"jquery-integration": false,
"template-only-glimmer-components": true
}
105 changes: 68 additions & 37 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,15 @@
"@babel/helper-plugin-utils" "^7.0.0"
"@babel/plugin-syntax-typescript" "^7.7.4"

"@babel/plugin-transform-typescript@~7.8.0":
version "7.8.7"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-typescript/-/plugin-transform-typescript-7.8.7.tgz#48bccff331108a7b3a28c3a4adc89e036dc3efda"
integrity sha512-7O0UsPQVNKqpHeHLpfvOG4uXmlw+MOxYvUv6Otc9uH5SYMIxvF6eBdjkWvC3f9G+VXe0RsNExyAQBeTRug/wqQ==
dependencies:
"@babel/helper-create-class-features-plugin" "^7.8.3"
"@babel/helper-plugin-utils" "^7.8.3"
"@babel/plugin-syntax-typescript" "^7.8.3"

"@babel/plugin-transform-unicode-regex@^7.8.3":
version "7.8.3"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-unicode-regex/-/plugin-transform-unicode-regex-7.8.3.tgz#0cef36e3ba73e5c57273effb182f46b91a1ecaad"
Expand Down Expand Up @@ -1150,18 +1159,6 @@
resolved "https://registry.yarnpkg.com/@ember/edition-utils/-/edition-utils-1.2.0.tgz#a039f542dc14c8e8299c81cd5abba95e2459cfa6"
integrity sha512-VmVq/8saCaPdesQmftPqbFtxJWrzxNGSQ+e8x8LLe3Hjm36pJ04Q8LeORGZkAeOhldoUX9seLGmSaHeXkIqoog==

"@ember/[email protected]":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@ember/jquery/-/jquery-1.1.0.tgz#33d062610a5ceaa5c5c8a3187f870d47d6595940"
integrity sha512-zePT3LiK4/2bS4xafrbOlwoLJrDFseOZ95OOuVDyswv8RjFL+9lar+uxX6+jxRb0w900BcQSWP/4nuFSK6HXXw==
dependencies:
broccoli-funnel "^2.0.2"
broccoli-merge-trees "^3.0.2"
ember-cli-babel "^7.11.1"
ember-cli-version-checker "^3.1.3"
jquery "^3.4.1"
resolve "^1.11.1"

"@ember/[email protected]":
version "1.3.0"
resolved "https://registry.yarnpkg.com/@ember/optional-features/-/optional-features-1.3.0.tgz#d7da860417b85a56cec88419f30da5ee1dde2756"
Expand Down Expand Up @@ -1822,7 +1819,7 @@ abbrev@1, abbrev@~1.1.1:
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8"
integrity sha512-nne9/IiQ/hzIhY6pdDnbBtz7DjPTKrY00P/zvPSm5pOFkl6xuGrGnXn/VtTNNfNtAfZ9/1RtehkszU9qcTii0Q==

abortcontroller-polyfill@^1.3.0:
abortcontroller-polyfill@^1.3.0, abortcontroller-polyfill@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/abortcontroller-polyfill/-/abortcontroller-polyfill-1.4.0.tgz#0d5eb58e522a461774af8086414f68e1dda7a6c4"
integrity sha512-3ZFfCRfDzx3GFjO6RAkYx81lPGpUS20ISxux9gLxuKnqafNcFQo59+IoZqpO2WvQlyc287B62HDnDdNYRmlvWA==
Expand Down Expand Up @@ -3315,6 +3312,23 @@ broccoli-concat@^4.2.2:
lodash.omit "^4.1.0"
lodash.uniq "^4.2.0"

broccoli-concat@^4.2.4:
version "4.2.4"
resolved "https://registry.yarnpkg.com/broccoli-concat/-/broccoli-concat-4.2.4.tgz#78e359ddc540b999d815355163bf3cfb6bd67322"
integrity sha512-NgdBIE57r+U/AslBohQr0mCS7PopIWL8dihMI1CzqffQkisAgqWMuddjYmizqRBQlml7crBFaBeUnPDHhf4/RQ==
dependencies:
broccoli-debug "^0.6.5"
broccoli-kitchen-sink-helpers "^0.3.1"
broccoli-plugin "^4.0.2"
ensure-posix-path "^1.0.2"
fast-sourcemap-concat "^2.1.0"
find-index "^1.1.0"
fs-extra "^8.1.0"
fs-tree-diff "^2.0.1"
lodash.merge "^4.6.2"
lodash.omit "^4.1.0"
lodash.uniq "^4.2.0"

broccoli-config-loader@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/broccoli-config-loader/-/broccoli-config-loader-1.0.1.tgz#d10aaf8ebc0cb45c1da5baa82720e1d88d28c80a"
Expand Down Expand Up @@ -5361,14 +5375,6 @@ elliptic@^6.0.0:
minimalistic-assert "^1.0.0"
minimalistic-crypto-utils "^1.0.0"

ember-ajax@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/ember-ajax/-/ember-ajax-5.0.0.tgz#404b17c93194ab37aff86b7b8607d8edab93c9ec"
integrity sha512-Pet0S5VuiO7PDOwyTIF28VHdR7BpugL+jp+6PxNHRQvAq3SkKmVnXN0W06rnOgicH3xX29S46fx7czkKOGB3gA==
dependencies:
ember-cli-babel "^7.5.0"
najax "^1.0.3"

ember-assign-polyfill@^2.6.0:
version "2.6.0"
resolved "https://registry.yarnpkg.com/ember-assign-polyfill/-/ember-assign-polyfill-2.6.0.tgz#07847e3357ee35b33f886a0b5fbec6873f6860eb"
Expand Down Expand Up @@ -5435,7 +5441,7 @@ ember-cli-babel@^6.0.0-beta.4, ember-cli-babel@^6.16.0, ember-cli-babel@^6.3.0,
ember-cli-version-checker "^2.1.2"
semver "^5.5.0"

ember-cli-babel@^7.1.0, ember-cli-babel@^7.11.0, ember-cli-babel@^7.11.1, ember-cli-babel@^7.12.0, ember-cli-babel@^7.13.0, ember-cli-babel@^7.13.2, ember-cli-babel@^7.5.0, ember-cli-babel@^7.7.3:
ember-cli-babel@^7.1.0, ember-cli-babel@^7.11.0, ember-cli-babel@^7.12.0, ember-cli-babel@^7.13.0, ember-cli-babel@^7.13.2, ember-cli-babel@^7.5.0, ember-cli-babel@^7.7.3:
version "7.17.2"
resolved "https://registry.yarnpkg.com/ember-cli-babel/-/ember-cli-babel-7.17.2.tgz#2c7717c6864425be3cd11913c722115fbf1448d2"
integrity sha512-64jRwtRVtEp4ghhew/sHeMsPyUOg3t1TB2dary5mCbKkRasb3L7FwF7xnuzPihaz+AW/v2LqYLALXGCml5XNLA==
Expand Down Expand Up @@ -5649,6 +5655,26 @@ ember-cli-typescript@^3.0.0:
stagehand "^1.0.0"
walk-sync "^2.0.0"

ember-cli-typescript@^3.1.3:
version "3.1.3"
resolved "https://registry.yarnpkg.com/ember-cli-typescript/-/ember-cli-typescript-3.1.3.tgz#a2c7ec6a8a5e57c38eb52d83e36d8e18c7071e60"
integrity sha512-bFi15H60L9TLYfn9XUzi+RAP1gTWHFtVdSy9IHvxXHlCvTlFZ+2rfuugr/f8reQLz9gvJccKc5TyRD7v+uhx0Q==
dependencies:
"@babel/plugin-proposal-nullish-coalescing-operator" "^7.4.4"
"@babel/plugin-proposal-optional-chaining" "^7.6.0"
"@babel/plugin-transform-typescript" "~7.8.0"
ansi-to-html "^0.6.6"
broccoli-stew "^3.0.0"
debug "^4.0.0"
ember-cli-babel-plugin-helpers "^1.0.0"
execa "^3.0.0"
fs-extra "^8.0.0"
resolve "^1.5.0"
rsvp "^4.8.1"
semver "^6.3.0"
stagehand "^1.0.0"
walk-sync "^2.0.0"

[email protected]:
version "3.0.0"
resolved "https://registry.yarnpkg.com/ember-cli-uglify/-/ember-cli-uglify-3.0.0.tgz#8819665b2cc5fe70e3ba9fe7a94645209bc42fd6"
Expand Down Expand Up @@ -5860,6 +5886,25 @@ ember-factory-for-polyfill@^1.3.1:
node-fetch "^2.6.0"
whatwg-fetch "^3.0.0"

ember-fetch@^8.0.1:
version "8.0.1"
resolved "https://registry.yarnpkg.com/ember-fetch/-/ember-fetch-8.0.1.tgz#dc57f3c3ba464e4a8722785f6b44265c46b38020"
integrity sha512-J+knuBFQJlMQUuij2tziMiVcJ+Ss0lCV2XiCQO2cHmG2PUsfvLlTGZ04uUyOXPqrJISxY2W3Et43fQBg4jy7SA==
dependencies:
abortcontroller-polyfill "^1.4.0"
broccoli-concat "^4.2.4"
broccoli-debug "^0.6.5"
broccoli-merge-trees "^4.2.0"
broccoli-rollup "^2.1.1"
broccoli-stew "^3.0.0"
broccoli-templater "^2.0.1"
calculate-cache-key-for-tree "^2.0.0"
caniuse-api "^3.0.0"
ember-cli-babel "^7.13.2"
ember-cli-typescript "^3.1.3"
node-fetch "^2.6.0"
whatwg-fetch "^3.0.0"

ember-get-config@^0.2.2, ember-get-config@^0.2.4:
version "0.2.4"
resolved "https://registry.yarnpkg.com/ember-get-config/-/ember-get-config-0.2.4.tgz#118492a2a03d73e46004ed777928942021fe1ecd"
Expand Down Expand Up @@ -8594,11 +8639,6 @@ java-properties@^1.0.0:
resolved "https://registry.yarnpkg.com/java-properties/-/java-properties-1.0.2.tgz#ccd1fa73907438a5b5c38982269d0e771fe78211"
integrity sha512-qjdpeo2yKlYTH7nFdK0vbZWuTCesk4o63v5iVOlhMQPfuIZQfW/HI35SjfhA+4qpg36rnFSvUK5b1m+ckIblQQ==

jquery-deferred@^0.3.0:
version "0.3.1"
resolved "https://registry.yarnpkg.com/jquery-deferred/-/jquery-deferred-0.3.1.tgz#596eca1caaff54f61b110962b23cafea74c35355"
integrity sha1-WW7KHKr/VPYbEQlisjyv6nTDU1U=

jquery@^3.4.1:
version "3.4.1"
resolved "https://registry.yarnpkg.com/jquery/-/jquery-3.4.1.tgz#714f1f8d9dde4bdfa55764ba37ef214630d80ef2"
Expand Down Expand Up @@ -10106,15 +10146,6 @@ [email protected], mute-stream@~0.0.4:
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.8.tgz#1630c42b2251ff81e2a283de96a5497ea92e5e0d"
integrity sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==

najax@^1.0.3:
version "1.0.4"
resolved "https://registry.yarnpkg.com/najax/-/najax-1.0.4.tgz#63fd8dbf15d18f24dc895b3a16fec66c136b8084"
integrity sha512-wsSacA+RkgY1wxRxXCT3tdqzmamEv9PLeoV/ub9SlLf2RngbPMSqc3A7H35XJDfURC0twMmZsnPdsYPkuuFSVg==
dependencies:
jquery-deferred "^0.3.0"
lodash.defaultsdeep "^4.6.0"
qs "^6.2.0"

nan@^2.12.1:
version "2.14.0"
resolved "https://registry.yarnpkg.com/nan/-/nan-2.14.0.tgz#7818f722027b2459a86f0295d434d1fc2336c52c"
Expand Down Expand Up @@ -11482,7 +11513,7 @@ [email protected]:
resolved "https://registry.yarnpkg.com/qs/-/qs-6.7.0.tgz#41dc1a015e3d581f1621776be31afb2876a9b1bc"
integrity sha512-VCdBRNFTX1fyE7Nb6FYoURo/SPe62QCaAyzJvUjwRaIsc+NePBEniHlvxFmmX56+HZphIGtV0XeCirBtpDrTyQ==

qs@^6.2.0, qs@^6.4.0:
qs@^6.4.0:
version "6.9.1"
resolved "https://registry.yarnpkg.com/qs/-/qs-6.9.1.tgz#20082c65cb78223635ab1a9eaca8875a29bf8ec9"
integrity sha512-Cxm7/SS/y/Z3MHWSxXb8lIFqgqBowP5JMlTUFyJN88y0SGQhVmZnqFK/PeuMX9LzUyWsqqhNxIyg0jlzq946yA==
Expand Down

0 comments on commit 0bda959

Please sign in to comment.