From 55784c890740049a1570f3b9a53c3393d857ab67 Mon Sep 17 00:00:00 2001 From: Michael Jedich Date: Sun, 7 Jan 2024 13:31:05 +0100 Subject: [PATCH] #54 - Allow custom authorization header request property --- CHANGELOG.md | 5 +++++ README.md | 9 ++++----- package.json | 2 +- plugin.xml | 2 +- src/android/ApkUpdater.java | 4 ++-- src/android/downloader/FileDownloader.java | 9 +++------ src/android/update/UpdateManager.java | 8 ++++---- src/ionic/declarations/ApkUpdater.d.ts | 1 - src/ionic/declarations/interfaces/AuthConfig.d.ts | 11 ----------- src/ionic/declarations/interfaces/Config.d.ts | 4 ++-- www/API.js | 11 ++--------- www/ApkUpdater.js | 4 +--- 12 files changed, 25 insertions(+), 45 deletions(-) delete mode 100644 src/ionic/declarations/interfaces/AuthConfig.d.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 61b39d4..539ad25 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # CHANGELOG +## 5.0.0 - 2024-07-01 + +**WARNING! Breaking Changes:** +- [Changed] `basicAuth` has been replaced by `authorization`, see [example](https://github.com/kolbasa/cordova-plugin-apkupdater#download) of download configuration. ([#54](https://github.com/kolbasa/cordova-plugin-apkupdater/issues/54)). + ## 4.0.3 - 2023-04-21 - [Fixed] `androidx.core` version was lowered for the time being, as otherwise the compileSdkVersion requirements are too high ([#52](https://github.com/kolbasa/cordova-plugin-apkupdater/issues/52)). diff --git a/README.md b/README.md index 0f018e1..7d531fa 100644 --- a/README.md +++ b/README.md @@ -168,11 +168,10 @@ Configuration (optional): ```js const options = { - zipPassword: 'aDzEsCceP3BPO5jy', // If an encrypted zip file is used. - basicAuth: { // Basic access authentication - user: 'username', - password: 'JtE+es2GcHrjTAEU' - }, + // ↓↓↓ If an encrypted zip file is used. + zipPassword: 'aDzEsCceP3BPO5jy', + // ↓↓↓ Authorization token + authorization: "Basic " + btoa('username:password'), // Example for basic access authentication onDownloadProgress: function (e) { console.log( 'Downloading: ' + e.progress + '%', diff --git a/package.json b/package.json index ea4e837..83c78a5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cordova-plugin-apkupdater", - "version": "4.0.3", + "version": "5.0.0", "description": "This plugin allows your Android app to download and install compressed updates without the Google Play Store.", "cordova": { "id": "cordova-plugin-apkupdater", diff --git a/plugin.xml b/plugin.xml index 4f5529e..75dcb09 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,7 +1,7 @@ + id="cordova-plugin-apkupdater" version="5.0.0"> Apk Updater MIT diff --git a/src/android/ApkUpdater.java b/src/android/ApkUpdater.java index c5a5330..668048f 100644 --- a/src/android/ApkUpdater.java +++ b/src/android/ApkUpdater.java @@ -79,10 +79,10 @@ private void download(JSONArray data, CallbackContext callbackContext) { checkIfRunning(); String url = parseString(data.getString(0)); - String basicAuth = parseString(data.getString(1)); + String auth = parseString(data.getString(1)); String zipPassword = parseString(data.getString(2)); - Update update = updateManager.download(url, basicAuth, zipPassword); + Update update = updateManager.download(url, auth, zipPassword); callbackContext.success(update.toJSON()); } catch (Exception e) { callbackContext.error(StackExtractor.format(e)); diff --git a/src/android/downloader/FileDownloader.java b/src/android/downloader/FileDownloader.java index a862f1e..2a83c4f 100644 --- a/src/android/downloader/FileDownloader.java +++ b/src/android/downloader/FileDownloader.java @@ -1,7 +1,5 @@ package de.kolbasa.apkupdater.downloader; -import android.util.Base64; - import java.io.File; import java.io.FileOutputStream; import java.io.IOException; @@ -30,7 +28,7 @@ private void broadcast(Progress progress) { notifyObservers(progress); } - public File download(String fileUrl, File dir, String basicAuth) throws DownloadFailedException { + public File download(String fileUrl, File dir, String auth) throws DownloadFailedException { String fileName = fileUrl.substring(fileUrl.lastIndexOf('/') + 1); File outputFile = new File(dir, fileName); @@ -41,9 +39,8 @@ public File download(String fileUrl, File dir, String basicAuth) throws Download connection.setUseCaches(false); connection.setAllowUserInteraction(false); - if (basicAuth != null) { - basicAuth = new String(Base64.encode(basicAuth.getBytes(), Base64.NO_WRAP)); - connection.setRequestProperty("Authorization", "Basic " + basicAuth); + if (auth != null) { + connection.setRequestProperty("Authorization", auth); } connection.connect(); diff --git a/src/android/update/UpdateManager.java b/src/android/update/UpdateManager.java index 7808203..2affbfa 100644 --- a/src/android/update/UpdateManager.java +++ b/src/android/update/UpdateManager.java @@ -68,13 +68,13 @@ public void reset() throws IOException { } } - private File downloadFile(String path, String basicAuth) throws DownloadFailedException { + private File downloadFile(String path, String auth) throws DownloadFailedException { try { fileDownloader = new FileDownloader(); if (downloadObserver != null) { fileDownloader.addObserver(downloadObserver); } - return fileDownloader.download(path, downloadDir, basicAuth); + return fileDownloader.download(path, downloadDir, auth); } finally { fileDownloader = null; } @@ -123,14 +123,14 @@ public Update getUpdate() throws IOException, UpdateNotFoundException, return getApkInfo(); } - public Update download(String path, String basicAuth, String zipPassword) throws IOException, + public Update download(String path, String auth, String zipPassword) throws IOException, UnzipException, DownloadFailedException, UpdateNotFoundException, InvalidPackageException, PackageManager.NameNotFoundException { try { reset(); - File downloadedFile = downloadFile(path, basicAuth); + File downloadedFile = downloadFile(path, auth); unzipUpdate(downloadedFile, zipPassword); return getUpdate(); diff --git a/src/ionic/declarations/ApkUpdater.d.ts b/src/ionic/declarations/ApkUpdater.d.ts index 5bb6afc..857fa9d 100644 --- a/src/ionic/declarations/ApkUpdater.d.ts +++ b/src/ionic/declarations/ApkUpdater.d.ts @@ -1,5 +1,4 @@ /// -/// /// /// /// diff --git a/src/ionic/declarations/interfaces/AuthConfig.d.ts b/src/ionic/declarations/interfaces/AuthConfig.d.ts deleted file mode 100644 index 9d017fc..0000000 --- a/src/ionic/declarations/interfaces/AuthConfig.d.ts +++ /dev/null @@ -1,11 +0,0 @@ -declare module 'cordova-plugin-apkupdater' { - - interface AuthConfig { - - user: string; - - password: string; - - } - -} diff --git a/src/ionic/declarations/interfaces/Config.d.ts b/src/ionic/declarations/interfaces/Config.d.ts index 5d5cb54..1e6848a 100644 --- a/src/ionic/declarations/interfaces/Config.d.ts +++ b/src/ionic/declarations/interfaces/Config.d.ts @@ -8,9 +8,9 @@ declare module 'cordova-plugin-apkupdater' { zipPassword?: string; /** - * HTTP basic access authentication. + * Header authorization request property. */ - basicAuth?: AuthConfig; + authorization?: string; /** * Monitor download progress. diff --git a/www/API.js b/www/API.js index 7cb37c5..897a0a0 100644 --- a/www/API.js +++ b/www/API.js @@ -21,9 +21,7 @@ module.exports = { * @param {string} url - Your apk or zip-archive * @param {object | undefined} opt - Optional * @param {string=} opt.zipPassword - * @param {object=} opt.basicAuth - * @param {string=} opt.basicAuth.user - * @param {string=} opt.basicAuth.password + * @param {string=} opt.authorization * @param {function({progress: number, bytes: number, bytesWritten: number}): void=} opt.onDownloadProgress * @param {function({progress: number, bytes: number, bytesWritten: number}): void=} opt.onUnzipProgress * @returns {Promise} @@ -39,13 +37,8 @@ module.exports = { exec(opt.onUnzipProgress, emptyFn, PLUGIN, 'addUnzipObserver'); } - var basicAuth; - if (opt.basicAuth != null && opt.basicAuth.user != null && opt.basicAuth.password != null) { - basicAuth = opt.basicAuth.user + ':' + opt.basicAuth.password; - } - return new Promise(function (resolve, reject) { - exec(resolve, reject, PLUGIN, 'download', [url, basicAuth, opt.zipPassword]); + exec(resolve, reject, PLUGIN, 'download', [url, opt.authorization, opt.zipPassword]); }); }, diff --git a/www/ApkUpdater.js b/www/ApkUpdater.js index 59e667a..cf19162 100644 --- a/www/ApkUpdater.js +++ b/www/ApkUpdater.js @@ -20,9 +20,7 @@ module.exports = { * @param {string} url - Your apk or zip-archive * @param {object | undefined} opt - Optional * @param {string=} opt.zipPassword - * @param {object=} opt.basicAuth - * @param {string=} opt.basicAuth.user - * @param {string=} opt.basicAuth.password + * @param {string=} opt.authorization * @param {function({progress: number, bytes: number, bytesWritten: number}): void=} opt.onDownloadProgress * @param {function({progress: number, bytes: number, bytesWritten: number}): void=} opt.onUnzipProgress * @param {function=} success