Skip to content

Commit

Permalink
#54 - Allow custom authorization header request property
Browse files Browse the repository at this point in the history
  • Loading branch information
kolbasa committed Jan 7, 2024
1 parent f98e939 commit 55784c8
Show file tree
Hide file tree
Showing 12 changed files with 25 additions and 45 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)).
Expand Down
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 + '%',
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>

<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova-plugin-apkupdater" version="4.0.3">
id="cordova-plugin-apkupdater" version="5.0.0">

<name>Apk Updater</name>
<license>MIT</license>
Expand Down
4 changes: 2 additions & 2 deletions src/android/ApkUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
9 changes: 3 additions & 6 deletions src/android/downloader/FileDownloader.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions src/android/update/UpdateManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
Expand Down
1 change: 0 additions & 1 deletion src/ionic/declarations/ApkUpdater.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/// <reference path="interfaces/App.d.ts" />
/// <reference path="interfaces/AuthConfig.d.ts" />
/// <reference path="interfaces/Config.d.ts" />
/// <reference path="interfaces/Progress.d.ts" />
/// <reference path="interfaces/Update.d.ts" />
Expand Down
11 changes: 0 additions & 11 deletions src/ionic/declarations/interfaces/AuthConfig.d.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/ionic/declarations/interfaces/Config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 2 additions & 9 deletions www/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<object>}
Expand All @@ -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]);
});
},

Expand Down
4 changes: 1 addition & 3 deletions www/ApkUpdater.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 55784c8

Please sign in to comment.