oauth2 support on Android

This commit is contained in:
Denis Arnst 2023-10-13 23:14:52 +02:00
parent e521ddfab6
commit 8b631f7860
6 changed files with 22 additions and 17 deletions

View file

@ -9,8 +9,8 @@ android {
apply from: "../capacitor-cordova-android-plugins/cordova.variables.gradle" apply from: "../capacitor-cordova-android-plugins/cordova.variables.gradle"
dependencies { dependencies {
implementation project(':byteowls-capacitor-oauth2')
implementation project(':capacitor-app') implementation project(':capacitor-app')
implementation project(':capacitor-browser')
implementation project(':capacitor-clipboard') implementation project(':capacitor-clipboard')
implementation project(':capacitor-dialog') implementation project(':capacitor-dialog')
implementation project(':capacitor-haptics') implementation project(':capacitor-haptics')

View file

@ -3,7 +3,7 @@
<string name="app_name">audiobookshelf</string> <string name="app_name">audiobookshelf</string>
<string name="title_activity_main">audiobookshelf</string> <string name="title_activity_main">audiobookshelf</string>
<string name="package_name">com.audiobookshelf.app</string> <string name="package_name">com.audiobookshelf.app</string>
<string name="custom_url_scheme">com.audiobookshelf.app.debug</string> <string name="custom_url_scheme">audiobookshelf</string>
<string name="add_widget">Add widget</string> <string name="add_widget">Add widget</string>
<string name="app_widget_description">Simple widget for audiobookshelf playback</string> <string name="app_widget_description">Simple widget for audiobookshelf playback</string>
<string name="action_jump_forward">Jump Forward</string> <string name="action_jump_forward">Jump Forward</string>

View file

@ -1,12 +1,12 @@
[ [
{
"pkg": "@byteowls/capacitor-oauth2",
"classpath": "com.byteowls.capacitor.oauth2.OAuth2ClientPlugin"
},
{ {
"pkg": "@capacitor/app", "pkg": "@capacitor/app",
"classpath": "com.capacitorjs.plugins.app.AppPlugin" "classpath": "com.capacitorjs.plugins.app.AppPlugin"
}, },
{
"pkg": "@capacitor/browser",
"classpath": "com.capacitorjs.plugins.browser.BrowserPlugin"
},
{ {
"pkg": "@capacitor/clipboard", "pkg": "@capacitor/clipboard",
"classpath": "com.capacitorjs.plugins.clipboard.ClipboardPlugin" "classpath": "com.capacitorjs.plugins.clipboard.ClipboardPlugin"

View file

@ -3,7 +3,7 @@
<string name="app_name">audiobookshelf</string> <string name="app_name">audiobookshelf</string>
<string name="title_activity_main">audiobookshelf</string> <string name="title_activity_main">audiobookshelf</string>
<string name="package_name">com.audiobookshelf.app</string> <string name="package_name">com.audiobookshelf.app</string>
<string name="custom_url_scheme">com.audiobookshelf.app</string> <string name="custom_url_scheme">audiobookshelf</string>
<string name="add_widget">Add widget</string> <string name="add_widget">Add widget</string>
<string name="app_widget_description">Simple widget for audiobookshelf playback</string> <string name="app_widget_description">Simple widget for audiobookshelf playback</string>
<string name="action_jump_forward">Jump Forward</string> <string name="action_jump_forward">Jump Forward</string>

View file

@ -2,12 +2,12 @@
include ':capacitor-android' include ':capacitor-android'
project(':capacitor-android').projectDir = new File('../node_modules/@capacitor/android/capacitor') project(':capacitor-android').projectDir = new File('../node_modules/@capacitor/android/capacitor')
include ':byteowls-capacitor-oauth2'
project(':byteowls-capacitor-oauth2').projectDir = new File('../node_modules/@byteowls/capacitor-oauth2/android')
include ':capacitor-app' include ':capacitor-app'
project(':capacitor-app').projectDir = new File('../node_modules/@capacitor/app/android') project(':capacitor-app').projectDir = new File('../node_modules/@capacitor/app/android')
include ':capacitor-browser'
project(':capacitor-browser').projectDir = new File('../node_modules/@capacitor/browser/android')
include ':capacitor-clipboard' include ':capacitor-clipboard'
project(':capacitor-clipboard').projectDir = new File('../node_modules/@capacitor/clipboard/android') project(':capacitor-clipboard').projectDir = new File('../node_modules/@capacitor/clipboard/android')

View file

@ -79,9 +79,10 @@
<script> <script>
import { App } from '@capacitor/app'; import { App } from '@capacitor/app';
import { Dialog } from '@capacitor/dialog'
import { CapacitorHttp } from '@capacitor/core'
import { Browser } from '@capacitor/browser'; import { Browser } from '@capacitor/browser';
import { Capacitor } from '@capacitor/core';
import { CapacitorHttp } from '@capacitor/core'
import { Dialog } from '@capacitor/dialog'
// Variable which is set to an instance of ServerConnectForm.vue used below of the listener // Variable which is set to an instance of ServerConnectForm.vue used below of the listener
let serverConnectForm = null; let serverConnectForm = null;
@ -91,7 +92,8 @@ App.addListener('appUrlOpen', async (data) => {
const url = new URL(data.url) const url = new URL(data.url)
// audiobookshelf://oauth?code... // audiobookshelf://oauth?code...
if (url.host === 'oauth') { // url.hostname for iOS and url.pathname for android
if (data.url.startsWith('audiobookshelf://oauth')) {
// Extract oauth2 code to be exchanged for a token // Extract oauth2 code to be exchanged for a token
const authCode = url.searchParams.get('code') const authCode = url.searchParams.get('code')
// Extract the state variable // Extract the state variable
@ -101,7 +103,7 @@ App.addListener('appUrlOpen', async (data) => {
await serverConnectForm.oauthExchangeCodeForToken(authCode, state) await serverConnectForm.oauthExchangeCodeForToken(authCode, state)
} }
} else { } else {
console.warn(`[appUrlOpen] Unknown url: ${data.url}`) console.warn(`[appUrlOpen] Unknown url: ${data.url} - host: ${url.hostname} - path: ${url.pathname}`)
} }
}); });
@ -205,7 +207,8 @@ export default {
}, },
}) })
const locationHeader = response.headers["Location"] // Depending on iOS or Android, it can be location or Location...
const locationHeader = response.headers[Object.keys(response.headers).find(key => key.toLowerCase() === 'location')];
if (locationHeader) { if (locationHeader) {
const url = new URL(locationHeader) const url = new URL(locationHeader)
return url return url
@ -226,8 +229,10 @@ export default {
const backendEndpoint = `${this.serverConfig.address}/auth/openid/callback?state=${encodeURIComponent(state)}&code=${encodeURIComponent(code)}`; const backendEndpoint = `${this.serverConfig.address}/auth/openid/callback?state=${encodeURIComponent(state)}&code=${encodeURIComponent(code)}`;
try { try {
// We can close the browser at this point // We can close the browser at this point (does not work on Android)
await Browser.close() if (Capacitor.getPlatform() === 'ios' || Capacitor.getPlatform() === 'web') {
await Browser.close()
}
const response = await CapacitorHttp.get({ const response = await CapacitorHttp.get({
url: backendEndpoint url: backendEndpoint