From 4a5ebf637891b5b9e857facf6e1abbe6d72dcc0a Mon Sep 17 00:00:00 2001 From: Shuo Wu Date: Tue, 11 Jan 2022 16:15:46 +0000 Subject: [PATCH] Supports okta-auth-js v6 By defining minimum supported version at build time OKTA-457553 <<>> Artifact: okta-vue Files changed count: 8 PR Link: "https://github.com/okta/okta-vue/pull/80" --- CHANGELOG.md | 6 +++++ env.js | 4 --- jest.config.js | 1 + package.json | 7 ++--- rollup.config.js | 6 ++++- src/okta-vue.ts | 18 ++++++++----- test/specs/OktaVue.spec.js | 28 +++++++++++++++++-- yarn.lock | 55 ++++++++++++++++++++++++++++++++++++-- 8 files changed, 106 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5af3d7d..244e625 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ [authState]: https://github.com/okta/okta-auth-js#authstatemanager +# 5.1.0 + +### Others + +- [#80](https://github.com/okta/okta-vue/pull/80) Set okta-auth-js minimum supported version as 5.3.1, `AuthSdkError` will be thrown if oktaAuth instance cannot meet the version requirement + # 5.0.2 ### Bug Fixes diff --git a/env.js b/env.js index 7920b4c..0ca3135 100644 --- a/env.js +++ b/env.js @@ -4,7 +4,6 @@ const PACKAGE = require('./package.json') const path = require('path') const dotenv = require('dotenv') const fs = require('fs') -const semver = require('semver') // Read environment variables from "testenv". Override environment vars if they are already set. const TESTENV = path.resolve(__dirname, 'testenv') @@ -17,9 +16,6 @@ if (fs.existsSync(TESTENV)) { } process.env.CLIENT_ID = process.env.CLIENT_ID || process.env.SPA_CLIENT_ID -const authJsVersion = PACKAGE.peerDependencies['@okta/okta-auth-js']; -process.env.AUTH_JS_MAJOR_VERSION = semver.minVersion(authJsVersion).major; - module.exports = (overrides = {}) => { const PORT = overrides.port || process.env.PORT || 3000 const BASE_URI = process.env.BASE_URI || `http://localhost:${PORT}` diff --git a/jest.config.js b/jest.config.js index ff47c1a..2cabf51 100644 --- a/jest.config.js +++ b/jest.config.js @@ -13,6 +13,7 @@ module.exports = { ], globals: { 'PACKAGE': ENV.packageInfo, + 'AUTH_JS': { minSupportedVersion: '5.3.1' }, 'ts-jest': { diagnostics: { warnOnly: true diff --git a/package.json b/package.json index 919d8eb..583dac0 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,8 @@ }, "homepage": "https://github.com/okta/okta-vue#readme", "dependencies": { - "@babel/runtime": "^7.12.5" + "@babel/runtime": "^7.12.5", + "compare-versions": "^4.1.2" }, "devDependencies": { "@babel/core": "^7.12.3", @@ -45,6 +46,7 @@ "@babel/preset-env": "^7.12.1", "@okta/okta-auth-js": "^5.8.0", "@rollup/plugin-babel": "^5.2.1", + "@rollup/plugin-commonjs": "^21.0.1", "@rollup/plugin-replace": "^2.3.4", "@typescript-eslint/eslint-plugin": "^2.33.0", "@typescript-eslint/parser": "^2.33.0", @@ -76,7 +78,6 @@ "rollup-plugin-terser": "^7.0.2", "rollup-plugin-typescript2": "^0.29.0", "rollup-plugin-vue": "^6.0.0-beta.11", - "semver": "^7.3.5", "shelljs": "0.8.3", "ts-jest": "^26.4.4", "typescript": "^4.1.2", @@ -111,4 +112,4 @@ "vue": "^3.0.0", "vue-router": "^4.0.3" } -} \ No newline at end of file +} diff --git a/rollup.config.js b/rollup.config.js index cfed9c7..c686718 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -3,6 +3,7 @@ import replace from '@rollup/plugin-replace' import { terser } from 'rollup-plugin-terser' import cleanup from 'rollup-plugin-cleanup' import typescript from 'rollup-plugin-typescript2' +import commonjs from '@rollup/plugin-commonjs' import pkg from './package.json' const ENV = require('./env')() @@ -23,9 +24,12 @@ const makeExternalPredicate = externalArr => { const input = 'src/index.ts' const commonPlugins = [ + commonjs(), replace({ PACKAGE: JSON.stringify(ENV.packageInfo), - 'process.env.AUTH_JS_MAJOR_VERSION': JSON.stringify(process.env.AUTH_JS_MAJOR_VERSION) + AUTH_JS: JSON.stringify({ + minSupportedVersion: '5.3.1' + }) }), cleanup() ] diff --git a/src/okta-vue.ts b/src/okta-vue.ts index b6c4351..b47d5bf 100644 --- a/src/okta-vue.ts +++ b/src/okta-vue.ts @@ -13,6 +13,7 @@ import { App } from 'vue' import { Router, RouteLocationNormalized } from 'vue-router' import { AuthSdkError, OktaAuth, AuthState, toRelativeUrl } from '@okta/okta-auth-js' +import { compare } from 'compare-versions'; import { OktaVueOptions, OnAuthRequiredFunction } from './types' // constants are defined in webpack.config.js @@ -21,6 +22,10 @@ declare const PACKAGE: { version: string; } +declare const AUTH_JS: { + minSupportedVersion: string; +} + let _oktaAuth: OktaAuth let _onAuthRequired: OnAuthRequiredFunction let _router: Router @@ -77,19 +82,18 @@ function install (app: App, { _onAuthRequired = onAuthRequired if (oktaAuth._oktaUserAgent) { - // check major version of auth-js - const oktaAuthVersion = oktaAuth._oktaUserAgent.getVersion(); - const oktaAuthMajorVersion = oktaAuthVersion?.split('.')[0]; - if (oktaAuthMajorVersion && oktaAuthMajorVersion !== process.env.AUTH_JS_MAJOR_VERSION) { + const isAuthJsSupported = compare(oktaAuth._oktaUserAgent.getVersion(), AUTH_JS.minSupportedVersion, '>='); + if (!isAuthJsSupported) { throw new AuthSdkError(` - Passed in oktaAuth is not compatible with the SDK, - okta-auth-js version ${process.env.AUTH_JS_MAJOR_VERSION}.x is the current supported version. - `); + Passed in oktaAuth is not compatible with the SDK, + minimum supported okta-auth-js version is ${AUTH_JS.minSupportedVersion}. + `); } // customize user agent oktaAuth._oktaUserAgent.addEnvironment(`${PACKAGE.name}/${PACKAGE.version}`); } else { + // TODO: just throw based on the minimum supported auth-js version in the next major version console.warn('_oktaUserAgent is not available on auth SDK instance. Please use okta-auth-js@^5.3.1 .'); } diff --git a/test/specs/OktaVue.spec.js b/test/specs/OktaVue.spec.js index f731cdb..1921979 100644 --- a/test/specs/OktaVue.spec.js +++ b/test/specs/OktaVue.spec.js @@ -22,6 +22,7 @@ describe('OktaVue', () => { let oktaAuth let wrapper let mockRouter + let originalConsole function setupOktaAuth () { oktaAuth = new OktaAuth({ @@ -48,9 +49,17 @@ describe('OktaVue', () => { } beforeEach(() => { + originalConsole = global.console + global.console = { + warn: jest.fn() + } setupOktaAuth() }) + afterEach(() => { + global.console = originalConsole + }) + it('should add environment to oktaAuth\'s _oktaUserAgent', () => { bootstrap() const userAgent = wrapper.vm.$auth._oktaUserAgent.getHttpHeader()['X-Okta-User-Agent-Extended']; @@ -59,11 +68,26 @@ describe('OktaVue', () => { ).toBeGreaterThan(-1); }) - it('throws when provided OktaAuth instance of unsupported version', () => { - oktaAuth._oktaUserAgent.getVersion = jest.fn().mockReturnValue('okta-auth-js/99.0.42'); + it('should not throw when provided OktaAuth instance greater or equal than minimum supported version', () => { + oktaAuth._oktaUserAgent.getVersion = jest.fn().mockReturnValue('5.3.1'); + expect(() => bootstrap()).not.toThrow(AuthSdkError); + oktaAuth._oktaUserAgent.getVersion = jest.fn().mockReturnValue('5.8.0'); + expect(() => bootstrap()).not.toThrow(AuthSdkError); + oktaAuth._oktaUserAgent.getVersion = jest.fn().mockReturnValue('6.0.0'); + expect(() => bootstrap()).not.toThrow(AuthSdkError); + }) + + it('throws when provided OktaAuth instance less than minimum supported version', () => { + oktaAuth._oktaUserAgent.getVersion = jest.fn().mockReturnValue('1.0.0'); expect(() => bootstrap()).toThrow(AuthSdkError); }) + it('logs warning when oktaAuth._oktaUserAgent is not available', () => { + delete oktaAuth._oktaUserAgent; + bootstrap(); + expect(global.console.warn).toHaveBeenCalledWith('_oktaUserAgent is not available on auth SDK instance. Please use okta-auth-js@^5.3.1 .'); + }); + describe('restoreOriginalUri', () => { const mockOriginalUri = 'http://localhost/fakepath' it('should call restoreOriginalUri callback if provided when calls restoreOriginalUri', () => { diff --git a/yarn.lock b/yarn.lock index 1f7c52f..40be393 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1278,6 +1278,19 @@ "@babel/helper-module-imports" "^7.10.4" "@rollup/pluginutils" "^3.1.0" +"@rollup/plugin-commonjs@^21.0.1": + version "21.0.1" + resolved "https://registry.yarnpkg.com/@rollup/plugin-commonjs/-/plugin-commonjs-21.0.1.tgz#1e57c81ae1518e4df0954d681c642e7d94588fee" + integrity sha512-EA+g22lbNJ8p5kuZJUYyhhDK7WgJckW5g4pNN7n4mAFUM96VuwUnNT3xr2Db2iCZPI1pJPbGyfT5mS9T1dHfMg== + dependencies: + "@rollup/pluginutils" "^3.1.0" + commondir "^1.0.1" + estree-walker "^2.0.1" + glob "^7.1.6" + is-reference "^1.2.1" + magic-string "^0.25.7" + resolve "^1.17.0" + "@rollup/plugin-replace@^2.3.4": version "2.4.2" resolved "https://registry.yarnpkg.com/@rollup/plugin-replace/-/plugin-replace-2.4.2.tgz#a2d539314fbc77c244858faa523012825068510a" @@ -1412,6 +1425,11 @@ resolved "https://registry.yarnpkg.com/@types/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz#1ee30d79544ca84d68d4b3cdb0af4f205663dd2d" integrity sha512-OCutwjDZ4aFS6PB1UZ988C4YgwlBHJd6wCeQqaLdmadZ/7e+w79+hbMUFC1QXDNCmdyoRfAFdm0RypzwR+Qpag== +"@types/estree@*": + version "0.0.50" + resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.50.tgz#1e0caa9364d3fccd2931c3ed96fdbeaa5d4cca83" + integrity sha512-C6N5s2ZFtuZRj54k2/zyRhNDjJwwcViAM3Nbm8zjBpbqAdZ00mr0CFxvSKeO8Y/e03WVFLpQMdHYVfUd6SB+Hw== + "@types/estree@0.0.39": version "0.0.39" resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f" @@ -3415,6 +3433,11 @@ commondir@^1.0.1: resolved "https://registry.yarnpkg.com/commondir/-/commondir-1.0.1.tgz#ddd800da0c66127393cca5950ea968a3aaf1253b" integrity sha1-3dgA2gxmEnOTzKWVDqloo6rxJTs= +compare-versions@^4.1.2: + version "4.1.2" + resolved "https://registry.yarnpkg.com/compare-versions/-/compare-versions-4.1.2.tgz#a7b1678c897000d03a70a0e01efee43e7b04dda7" + integrity sha512-LAfbAbAgjnIwPsr2fvJLfrSyqAhK5nj/ffIg7a5aigry9RXJfNzVnOu0Egw8Z+G8LMDu1Qig2q48bpBzjyjZoQ== + component-emitter@^1.2.1: version "1.3.0" resolved "https://registry.yarnpkg.com/component-emitter/-/component-emitter-1.3.0.tgz#16e4070fba8ae29b679f2215853ee181ab2eabc0" @@ -6309,6 +6332,13 @@ is-core-module@^2.2.0, is-core-module@^2.4.0: dependencies: has "^1.0.3" +is-core-module@^2.8.0: + version "2.8.1" + resolved "https://registry.yarnpkg.com/is-core-module/-/is-core-module-2.8.1.tgz#f59fdfca701d5879d0a6b100a40aa1560ce27211" + integrity sha512-SdNCUs284hr40hFTFP6l0IfZ/RSrMXF3qgoRHd3/79unUTvrFO/JoXwkGm+5J/Oe3E/b5GsnG330uUNgRpu1PA== + dependencies: + has "^1.0.3" + is-data-descriptor@^0.1.4: version "0.1.4" resolved "https://registry.yarnpkg.com/is-data-descriptor/-/is-data-descriptor-0.1.4.tgz#0b5ee648388e2c860282e793f1856fec3f301b56" @@ -6494,6 +6524,13 @@ is-potential-custom-element-name@^1.0.1: resolved "https://registry.yarnpkg.com/is-potential-custom-element-name/-/is-potential-custom-element-name-1.0.1.tgz#171ed6f19e3ac554394edf78caa05784a45bebb5" integrity sha512-bCYeRA2rVibKZd+s2625gGnGF/t7DSqDs4dP7CrLA1m7jKWz6pps0LpYLJN8Q64HtmPKJ1hrN3nzPNKFEKOUiQ== +is-reference@^1.2.1: + version "1.2.1" + resolved "https://registry.yarnpkg.com/is-reference/-/is-reference-1.2.1.tgz#8b2dac0b371f4bc994fdeaba9eb542d03002d0b7" + integrity sha512-U82MsXXiFIrjCK4otLT+o2NA2Cd2g5MLoOVXUZjIOhLurrRxpEXzI8O0KZHr3IjLvlAH1kTPYSuqer5T9ZVBKQ== + dependencies: + "@types/estree" "*" + is-regex@^1.0.4, is-regex@^1.1.3: version "1.1.3" resolved "https://registry.yarnpkg.com/is-regex/-/is-regex-1.1.3.tgz#d029f9aff6448b93ebbe3f33dac71511fdcbef9f" @@ -8645,7 +8682,7 @@ path-key@^3.0.0, path-key@^3.1.0: resolved "https://registry.yarnpkg.com/path-key/-/path-key-3.1.1.tgz#581f6ade658cbba65a0d3380de7753295054f375" integrity sha512-ojmeN0qd+y0jszEtoY48r0Peq5dwMEkIlCOu6Q5f41lfkswXuKtYrhgoTpLnyIcHm24Uhqx+5Tqm2InSwLhE6Q== -path-parse@^1.0.6: +path-parse@^1.0.6, path-parse@^1.0.7: version "1.0.7" resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.7.tgz#fbc114b60ca42b30d9daf5858e4bd68bbedb6735" integrity sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw== @@ -9749,6 +9786,15 @@ resolve@^1.1.6, resolve@^1.10.0, resolve@^1.10.1, resolve@^1.13.1, resolve@^1.14 is-core-module "^2.2.0" path-parse "^1.0.6" +resolve@^1.17.0: + version "1.21.0" + resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.21.0.tgz#b51adc97f3472e6a5cf4444d34bc9d6b9037591f" + integrity sha512-3wCbTpk5WJlyE4mSOtDLhqQmGFi0/TD9VPwmiolnk8U0wRgMEktqCXd3vy5buTO3tljvalNvKrjHEfrd2WpEKA== + dependencies: + is-core-module "^2.8.0" + path-parse "^1.0.7" + supports-preserve-symlinks-flag "^1.0.0" + restore-cursor@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/restore-cursor/-/restore-cursor-2.0.0.tgz#9f7ee287f82fd326d4fd162923d62129eee0dfaf" @@ -10002,7 +10048,7 @@ semver@7.0.0: resolved "https://registry.yarnpkg.com/semver/-/semver-7.0.0.tgz#5f3ca35761e47e05b206c6daff2cf814f0316b8e" integrity sha512-+GB6zVA9LWh6zovYQLALHwv5rb2PHGlJi3lfiqIHxR0uuwCgefcOJc59v9fv1w8GbStwxuuqqAjI9NMAOOgq1A== -semver@7.x, semver@^7.2.1, semver@^7.3.2, semver@^7.3.5: +semver@7.x, semver@^7.2.1, semver@^7.3.2: version "7.3.5" resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.5.tgz#0b621c879348d8998e4b0e4be94b3f12e6018ef7" integrity sha512-PoeGJYh8HK4BTO/a9Tf6ZG3veo/A7ZVsYrSA6J8ny9nb3B1VrpkuN+z9OE5wfE5p6H4LchYZsegiQgbJD94ZFQ== @@ -10704,6 +10750,11 @@ supports-hyperlinks@^2.0.0: has-flag "^4.0.0" supports-color "^7.0.0" +supports-preserve-symlinks-flag@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz#6eda4bd344a3c94aea376d4cc31bc77311039e09" + integrity sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w== + svgo@^1.0.0: version "1.3.2" resolved "https://registry.yarnpkg.com/svgo/-/svgo-1.3.2.tgz#b6dc511c063346c9e415b81e43401145b96d4167"