diff --git a/Cargo.lock b/Cargo.lock
index 7affd673..1b53c4b5 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -30,6 +30,7 @@ dependencies = [
"tauri-plugin-opener",
"tauri-plugin-os",
"tauri-plugin-process",
+ "tauri-plugin-safari-auth",
"tauri-plugin-shell",
"tauri-plugin-sign-in-with-apple",
"tauri-plugin-single-instance",
@@ -5331,6 +5332,16 @@ dependencies = [
"tauri-plugin",
]
+[[package]]
+name = "tauri-plugin-safari-auth"
+version = "0.1.0"
+dependencies = [
+ "serde",
+ "tauri",
+ "tauri-plugin",
+ "thiserror 2.0.11",
+]
+
[[package]]
name = "tauri-plugin-shell"
version = "2.2.0"
diff --git a/apps/readest-app/package.json b/apps/readest-app/package.json
index 0029ca6d..ab665454 100644
--- a/apps/readest-app/package.json
+++ b/apps/readest-app/package.json
@@ -1,6 +1,6 @@
{
"name": "@readest/readest-app",
- "version": "0.9.15",
+ "version": "0.9.16",
"private": true,
"scripts": {
"dev": "dotenv -e .env.tauri -- next dev",
diff --git a/apps/readest-app/src-tauri/Cargo.toml b/apps/readest-app/src-tauri/Cargo.toml
index 5549c33c..b4ee4b8b 100644
--- a/apps/readest-app/src-tauri/Cargo.toml
+++ b/apps/readest-app/src-tauri/Cargo.toml
@@ -46,6 +46,7 @@ tauri-plugin-opener = "2.2.2"
tauri-plugin-deep-link = "2"
tauri-plugin-sign-in-with-apple = "1.0.2"
tauri-plugin-haptics = "2.2.3"
+tauri-plugin-safari-auth = { path = "./plugins/tauri-plugin-safari-auth" }
[patch.crates-io]
tauri = { path = "../../../packages/tauri/crates/tauri" }
diff --git a/apps/readest-app/src-tauri/capabilities/default.json b/apps/readest-app/src-tauri/capabilities/default.json
index cfdd2115..ce6feec2 100644
--- a/apps/readest-app/src-tauri/capabilities/default.json
+++ b/apps/readest-app/src-tauri/capabilities/default.json
@@ -74,6 +74,7 @@
"haptics:allow-impact-feedback",
"haptics:allow-notification-feedback",
"haptics:allow-selection-feedback",
+ "safari-auth:default",
"deep-link:default"
]
}
diff --git a/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/.gitignore b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/.gitignore
new file mode 100644
index 00000000..50d8e32e
--- /dev/null
+++ b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/.gitignore
@@ -0,0 +1,17 @@
+/.vs
+.DS_Store
+.Thumbs.db
+*.sublime*
+.idea/
+debug.log
+package-lock.json
+.vscode/settings.json
+yarn.lock
+
+/.tauri
+/target
+Cargo.lock
+node_modules/
+
+dist-js
+dist
diff --git a/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/Cargo.toml b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/Cargo.toml
new file mode 100644
index 00000000..98478bfd
--- /dev/null
+++ b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/Cargo.toml
@@ -0,0 +1,17 @@
+[package]
+name = "tauri-plugin-safari-auth"
+version = "0.1.0"
+authors = [ "chrox" ]
+description = "OAuth with ASWebAuthenticationSession on iOS"
+edition = "2021"
+rust-version = "1.77.2"
+exclude = ["/examples", "/dist-js", "/guest-js", "/node_modules"]
+links = "tauri-plugin-safari-auth"
+
+[dependencies]
+tauri = { version = "2.2.4" }
+serde = "1.0"
+thiserror = "2"
+
+[build-dependencies]
+tauri-plugin = { version = "2.0.3", features = ["build"] }
diff --git a/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/README.md b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/README.md
new file mode 100644
index 00000000..0ad04095
--- /dev/null
+++ b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/README.md
@@ -0,0 +1 @@
+# Tauri Plugin safari-auth
diff --git a/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/build.rs b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/build.rs
new file mode 100644
index 00000000..01c3a936
--- /dev/null
+++ b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/build.rs
@@ -0,0 +1,8 @@
+const COMMANDS: &[&str] = &["auth_with_safari"];
+
+fn main() {
+ tauri_plugin::Builder::new(COMMANDS)
+ .android_path("android")
+ .ios_path("ios")
+ .build();
+}
diff --git a/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/ios/.gitignore b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/ios/.gitignore
new file mode 100644
index 00000000..5922fdaa
--- /dev/null
+++ b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/ios/.gitignore
@@ -0,0 +1,10 @@
+.DS_Store
+/.build
+/Packages
+/*.xcodeproj
+xcuserdata/
+DerivedData/
+.swiftpm/config/registries.json
+.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
+.netrc
+Package.resolved
diff --git a/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/ios/Package.swift b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/ios/Package.swift
new file mode 100644
index 00000000..4c4ee351
--- /dev/null
+++ b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/ios/Package.swift
@@ -0,0 +1,32 @@
+// swift-tools-version:5.3
+// The swift-tools-version declares the minimum version of Swift required to build this package.
+
+import PackageDescription
+
+let package = Package(
+ name: "tauri-plugin-safari-auth",
+ platforms: [
+ .macOS(.v10_13),
+ .iOS(.v13),
+ ],
+ products: [
+ // Products define the executables and libraries a package produces, and make them visible to other packages.
+ .library(
+ name: "tauri-plugin-safari-auth",
+ type: .static,
+ targets: ["tauri-plugin-safari-auth"]),
+ ],
+ dependencies: [
+ .package(name: "Tauri", path: "../.tauri/tauri-api")
+ ],
+ targets: [
+ // Targets are the basic building blocks of a package. A target can define a module or a test suite.
+ // Targets can depend on other targets in this package, and on products in packages this package depends on.
+ .target(
+ name: "tauri-plugin-safari-auth",
+ dependencies: [
+ .byName(name: "Tauri")
+ ],
+ path: "Sources")
+ ]
+)
diff --git a/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/ios/README.md b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/ios/README.md
new file mode 100644
index 00000000..42b76e15
--- /dev/null
+++ b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/ios/README.md
@@ -0,0 +1,3 @@
+# Tauri Plugin safari-auth
+
+A description of this package.
diff --git a/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/ios/Sources/SafariAuthPlugin.swift b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/ios/Sources/SafariAuthPlugin.swift
new file mode 100644
index 00000000..4caa0fbc
--- /dev/null
+++ b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/ios/Sources/SafariAuthPlugin.swift
@@ -0,0 +1,59 @@
+import AuthenticationServices
+import SwiftRs
+import Tauri
+import UIKit
+import WebKit
+
+class RequestArgs: Decodable {
+ let authUrl: String
+}
+
+struct Response: Encodable {
+ let redirectUrl: String
+}
+
+class SafariAuthPlugin: Plugin {
+ private var authSession: ASWebAuthenticationSession?
+
+ @objc public func auth_with_safari(_ invoke: Invoke) throws {
+ let args = try invoke.parseArgs(RequestArgs.self)
+ let authUrl = URL(string: args.authUrl)!
+
+ authSession = ASWebAuthenticationSession(url: authUrl, callbackURLScheme: "readest") {
+ [weak self] callbackURL, error in
+ guard let strongSelf = self else { return }
+
+ if let error = error {
+ Logger.error("Auth session error: \(error.localizedDescription)")
+ invoke.reject(error.localizedDescription)
+ return
+ }
+
+ if let callbackURL = callbackURL {
+ Logger.info("Auth session callback URL: \(callbackURL.absoluteString)")
+ strongSelf.authSession?.cancel()
+ strongSelf.authSession = nil
+ invoke.resolve(["redirectUrl": callbackURL.absoluteString])
+ }
+ }
+
+ if #available(iOS 13.0, *) {
+ authSession?.presentationContextProvider = self
+ }
+
+ let started = authSession?.start() ?? false
+ Logger.info("Auth session start result: \(started)")
+ }
+}
+
+@_cdecl("init_plugin_safari_auth")
+func initPlugin() -> Plugin {
+ return SafariAuthPlugin()
+}
+
+@available(iOS 13.0, *)
+extension SafariAuthPlugin: ASWebAuthenticationPresentationContextProviding {
+ func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
+ return UIApplication.shared.windows.first ?? UIWindow()
+ }
+}
diff --git a/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/ios/Tests/PluginTests/PluginTests.swift b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/ios/Tests/PluginTests/PluginTests.swift
new file mode 100644
index 00000000..978eec27
--- /dev/null
+++ b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/ios/Tests/PluginTests/PluginTests.swift
@@ -0,0 +1,8 @@
+import XCTest
+@testable import SafariAuthPlugin
+
+final class SafariAuthPluginTests: XCTestCase {
+ func testExample() throws {
+ let plugin = SafariAuthPlugin()
+ }
+}
diff --git a/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/permissions/autogenerated/commands/auth_with_safari.toml b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/permissions/autogenerated/commands/auth_with_safari.toml
new file mode 100644
index 00000000..197b63b1
--- /dev/null
+++ b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/permissions/autogenerated/commands/auth_with_safari.toml
@@ -0,0 +1,13 @@
+# Automatically generated - DO NOT EDIT!
+
+"$schema" = "../../schemas/schema.json"
+
+[[permission]]
+identifier = "allow-auth-with-safari"
+description = "Enables the auth_with_safari command without any pre-configured scope."
+commands.allow = ["auth_with_safari"]
+
+[[permission]]
+identifier = "deny-auth-with-safari"
+description = "Denies the auth_with_safari command without any pre-configured scope."
+commands.deny = ["auth_with_safari"]
diff --git a/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/permissions/autogenerated/reference.md b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/permissions/autogenerated/reference.md
new file mode 100644
index 00000000..47dca41b
--- /dev/null
+++ b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/permissions/autogenerated/reference.md
@@ -0,0 +1,41 @@
+## Default Permission
+
+Default permissions for the plugin
+
+- `allow-auth-with-safari`
+
+## Permission Table
+
+
+
+| Identifier |
+Description |
+
+
+
+
+|
+
+`safari-auth:allow-auth-with-safari`
+
+ |
+
+
+Enables the auth_with_safari command without any pre-configured scope.
+
+ |
+
+
+
+|
+
+`safari-auth:deny-auth-with-safari`
+
+ |
+
+
+Denies the auth_with_safari command without any pre-configured scope.
+
+ |
+
+
diff --git a/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/permissions/default.toml b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/permissions/default.toml
new file mode 100644
index 00000000..86dfedad
--- /dev/null
+++ b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/permissions/default.toml
@@ -0,0 +1,3 @@
+[default]
+description = "Default permissions for the plugin"
+permissions = ["allow-auth-with-safari"]
diff --git a/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/permissions/schemas/schema.json b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/permissions/schemas/schema.json
new file mode 100644
index 00000000..4d195db7
--- /dev/null
+++ b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/permissions/schemas/schema.json
@@ -0,0 +1,315 @@
+{
+ "$schema": "http://json-schema.org/draft-07/schema#",
+ "title": "PermissionFile",
+ "description": "Permission file that can define a default permission, a set of permissions or a list of inlined permissions.",
+ "type": "object",
+ "properties": {
+ "default": {
+ "description": "The default permission set for the plugin",
+ "anyOf": [
+ {
+ "$ref": "#/definitions/DefaultPermission"
+ },
+ {
+ "type": "null"
+ }
+ ]
+ },
+ "set": {
+ "description": "A list of permissions sets defined",
+ "type": "array",
+ "items": {
+ "$ref": "#/definitions/PermissionSet"
+ }
+ },
+ "permission": {
+ "description": "A list of inlined permissions",
+ "default": [],
+ "type": "array",
+ "items": {
+ "$ref": "#/definitions/Permission"
+ }
+ }
+ },
+ "definitions": {
+ "DefaultPermission": {
+ "description": "The default permission set of the plugin.\n\nWorks similarly to a permission with the \"default\" identifier.",
+ "type": "object",
+ "required": [
+ "permissions"
+ ],
+ "properties": {
+ "version": {
+ "description": "The version of the permission.",
+ "type": [
+ "integer",
+ "null"
+ ],
+ "format": "uint64",
+ "minimum": 1.0
+ },
+ "description": {
+ "description": "Human-readable description of what the permission does. Tauri convention is to use headings in markdown content for Tauri documentation generation purposes.",
+ "type": [
+ "string",
+ "null"
+ ]
+ },
+ "permissions": {
+ "description": "All permissions this set contains.",
+ "type": "array",
+ "items": {
+ "type": "string"
+ }
+ }
+ }
+ },
+ "PermissionSet": {
+ "description": "A set of direct permissions grouped together under a new name.",
+ "type": "object",
+ "required": [
+ "description",
+ "identifier",
+ "permissions"
+ ],
+ "properties": {
+ "identifier": {
+ "description": "A unique identifier for the permission.",
+ "type": "string"
+ },
+ "description": {
+ "description": "Human-readable description of what the permission does.",
+ "type": "string"
+ },
+ "permissions": {
+ "description": "All permissions this set contains.",
+ "type": "array",
+ "items": {
+ "$ref": "#/definitions/PermissionKind"
+ }
+ }
+ }
+ },
+ "Permission": {
+ "description": "Descriptions of explicit privileges of commands.\n\nIt can enable commands to be accessible in the frontend of the application.\n\nIf the scope is defined it can be used to fine grain control the access of individual or multiple commands.",
+ "type": "object",
+ "required": [
+ "identifier"
+ ],
+ "properties": {
+ "version": {
+ "description": "The version of the permission.",
+ "type": [
+ "integer",
+ "null"
+ ],
+ "format": "uint64",
+ "minimum": 1.0
+ },
+ "identifier": {
+ "description": "A unique identifier for the permission.",
+ "type": "string"
+ },
+ "description": {
+ "description": "Human-readable description of what the permission does. Tauri internal convention is to use headings in markdown content for Tauri documentation generation purposes.",
+ "type": [
+ "string",
+ "null"
+ ]
+ },
+ "commands": {
+ "description": "Allowed or denied commands when using this permission.",
+ "default": {
+ "allow": [],
+ "deny": []
+ },
+ "allOf": [
+ {
+ "$ref": "#/definitions/Commands"
+ }
+ ]
+ },
+ "scope": {
+ "description": "Allowed or denied scoped when using this permission.",
+ "allOf": [
+ {
+ "$ref": "#/definitions/Scopes"
+ }
+ ]
+ },
+ "platforms": {
+ "description": "Target platforms this permission applies. By default all platforms are affected by this permission.",
+ "type": [
+ "array",
+ "null"
+ ],
+ "items": {
+ "$ref": "#/definitions/Target"
+ }
+ }
+ }
+ },
+ "Commands": {
+ "description": "Allowed and denied commands inside a permission.\n\nIf two commands clash inside of `allow` and `deny`, it should be denied by default.",
+ "type": "object",
+ "properties": {
+ "allow": {
+ "description": "Allowed command.",
+ "default": [],
+ "type": "array",
+ "items": {
+ "type": "string"
+ }
+ },
+ "deny": {
+ "description": "Denied command, which takes priority.",
+ "default": [],
+ "type": "array",
+ "items": {
+ "type": "string"
+ }
+ }
+ }
+ },
+ "Scopes": {
+ "description": "An argument for fine grained behavior control of Tauri commands.\n\nIt can be of any serde serializable type and is used to allow or prevent certain actions inside a Tauri command. The configured scope is passed to the command and will be enforced by the command implementation.\n\n## Example\n\n```json { \"allow\": [{ \"path\": \"$HOME/**\" }], \"deny\": [{ \"path\": \"$HOME/secret.txt\" }] } ```",
+ "type": "object",
+ "properties": {
+ "allow": {
+ "description": "Data that defines what is allowed by the scope.",
+ "type": [
+ "array",
+ "null"
+ ],
+ "items": {
+ "$ref": "#/definitions/Value"
+ }
+ },
+ "deny": {
+ "description": "Data that defines what is denied by the scope. This should be prioritized by validation logic.",
+ "type": [
+ "array",
+ "null"
+ ],
+ "items": {
+ "$ref": "#/definitions/Value"
+ }
+ }
+ }
+ },
+ "Value": {
+ "description": "All supported ACL values.",
+ "anyOf": [
+ {
+ "description": "Represents a null JSON value.",
+ "type": "null"
+ },
+ {
+ "description": "Represents a [`bool`].",
+ "type": "boolean"
+ },
+ {
+ "description": "Represents a valid ACL [`Number`].",
+ "allOf": [
+ {
+ "$ref": "#/definitions/Number"
+ }
+ ]
+ },
+ {
+ "description": "Represents a [`String`].",
+ "type": "string"
+ },
+ {
+ "description": "Represents a list of other [`Value`]s.",
+ "type": "array",
+ "items": {
+ "$ref": "#/definitions/Value"
+ }
+ },
+ {
+ "description": "Represents a map of [`String`] keys to [`Value`]s.",
+ "type": "object",
+ "additionalProperties": {
+ "$ref": "#/definitions/Value"
+ }
+ }
+ ]
+ },
+ "Number": {
+ "description": "A valid ACL number.",
+ "anyOf": [
+ {
+ "description": "Represents an [`i64`].",
+ "type": "integer",
+ "format": "int64"
+ },
+ {
+ "description": "Represents a [`f64`].",
+ "type": "number",
+ "format": "double"
+ }
+ ]
+ },
+ "Target": {
+ "description": "Platform target.",
+ "oneOf": [
+ {
+ "description": "MacOS.",
+ "type": "string",
+ "enum": [
+ "macOS"
+ ]
+ },
+ {
+ "description": "Windows.",
+ "type": "string",
+ "enum": [
+ "windows"
+ ]
+ },
+ {
+ "description": "Linux.",
+ "type": "string",
+ "enum": [
+ "linux"
+ ]
+ },
+ {
+ "description": "Android.",
+ "type": "string",
+ "enum": [
+ "android"
+ ]
+ },
+ {
+ "description": "iOS.",
+ "type": "string",
+ "enum": [
+ "iOS"
+ ]
+ }
+ ]
+ },
+ "PermissionKind": {
+ "type": "string",
+ "oneOf": [
+ {
+ "description": "Enables the auth_with_safari command without any pre-configured scope.",
+ "type": "string",
+ "const": "allow-auth-with-safari"
+ },
+ {
+ "description": "Denies the auth_with_safari command without any pre-configured scope.",
+ "type": "string",
+ "const": "deny-auth-with-safari"
+ },
+ {
+ "description": "Default permissions for the plugin",
+ "type": "string",
+ "const": "default"
+ }
+ ]
+ }
+ }
+}
\ No newline at end of file
diff --git a/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/src/commands.rs b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/src/commands.rs
new file mode 100644
index 00000000..ec71c6e3
--- /dev/null
+++ b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/src/commands.rs
@@ -0,0 +1,13 @@
+use tauri::{command, AppHandle, Runtime};
+
+use crate::models::*;
+use crate::Result;
+use crate::SafariAuthExt;
+
+#[command]
+pub(crate) async fn auth_with_safari(
+ app: AppHandle,
+ payload: SafariAuthRequest,
+) -> Result {
+ app.safari_auth().auth_with_safari(payload)
+}
diff --git a/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/src/desktop.rs b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/src/desktop.rs
new file mode 100644
index 00000000..291fdb63
--- /dev/null
+++ b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/src/desktop.rs
@@ -0,0 +1,23 @@
+use serde::de::DeserializeOwned;
+use tauri::{plugin::PluginApi, AppHandle, Runtime};
+
+use crate::models::*;
+
+pub fn init(
+ app: &AppHandle,
+ _api: PluginApi,
+) -> crate::Result> {
+ Ok(SafariAuth(app.clone()))
+}
+
+/// Access to the safari-auth APIs.
+pub struct SafariAuth(AppHandle);
+
+impl SafariAuth {
+ pub fn auth_with_safari(
+ &self,
+ _payload: SafariAuthRequest,
+ ) -> crate::Result {
+ Err(crate::Error::UnsupportedPlatformError)
+ }
+}
diff --git a/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/src/error.rs b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/src/error.rs
new file mode 100644
index 00000000..d61fe8cd
--- /dev/null
+++ b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/src/error.rs
@@ -0,0 +1,23 @@
+use serde::{ser::Serializer, Serialize};
+
+pub type Result = std::result::Result;
+
+#[derive(Debug, thiserror::Error)]
+pub enum Error {
+ #[error("Unsupported platform for this plugin")]
+ UnsupportedPlatformError,
+ #[error(transparent)]
+ Io(#[from] std::io::Error),
+ #[cfg(mobile)]
+ #[error(transparent)]
+ PluginInvoke(#[from] tauri::plugin::mobile::PluginInvokeError),
+}
+
+impl Serialize for Error {
+ fn serialize(&self, serializer: S) -> std::result::Result
+ where
+ S: Serializer,
+ {
+ serializer.serialize_str(self.to_string().as_ref())
+ }
+}
diff --git a/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/src/lib.rs b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/src/lib.rs
new file mode 100644
index 00000000..db56f97f
--- /dev/null
+++ b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/src/lib.rs
@@ -0,0 +1,48 @@
+use tauri::{
+ plugin::{Builder, TauriPlugin},
+ Manager, Runtime,
+};
+
+pub use models::*;
+
+#[cfg(desktop)]
+mod desktop;
+#[cfg(mobile)]
+mod mobile;
+
+mod commands;
+mod error;
+mod models;
+
+pub use error::{Error, Result};
+
+#[cfg(desktop)]
+use desktop::SafariAuth;
+#[cfg(mobile)]
+use mobile::SafariAuth;
+
+/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the safari-auth APIs.
+pub trait SafariAuthExt {
+ fn safari_auth(&self) -> &SafariAuth;
+}
+
+impl> crate::SafariAuthExt for T {
+ fn safari_auth(&self) -> &SafariAuth {
+ self.state::>().inner()
+ }
+}
+
+/// Initializes the plugin.
+pub fn init() -> TauriPlugin {
+ Builder::new("safari-auth")
+ .invoke_handler(tauri::generate_handler![commands::auth_with_safari])
+ .setup(|app, api| {
+ #[cfg(mobile)]
+ let safari_auth = mobile::init(app, api)?;
+ #[cfg(desktop)]
+ let safari_auth = desktop::init(app, api)?;
+ app.manage(safari_auth);
+ Ok(())
+ })
+ .build()
+}
diff --git a/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/src/mobile.rs b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/src/mobile.rs
new file mode 100644
index 00000000..56ead312
--- /dev/null
+++ b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/src/mobile.rs
@@ -0,0 +1,34 @@
+use serde::de::DeserializeOwned;
+use tauri::{
+ plugin::{PluginApi, PluginHandle},
+ AppHandle, Runtime,
+};
+
+use crate::models::*;
+
+#[cfg(target_os = "ios")]
+tauri::ios_plugin_binding!(init_plugin_safari_auth);
+
+// initializes the Kotlin or Swift plugin classes
+pub fn init(
+ _app: &AppHandle,
+ api: PluginApi,
+) -> crate::Result> {
+ #[cfg(target_os = "ios")]
+ let handle = api.register_ios_plugin(init_plugin_safari_auth)?;
+ Ok(SafariAuth(handle))
+}
+
+/// Access to the safari-auth APIs.
+pub struct SafariAuth(PluginHandle);
+
+impl SafariAuth {
+ pub fn auth_with_safari(
+ &self,
+ payload: SafariAuthRequest,
+ ) -> crate::Result {
+ self.0
+ .run_mobile_plugin("auth_with_safari", payload)
+ .map_err(Into::into)
+ }
+}
diff --git a/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/src/models.rs b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/src/models.rs
new file mode 100644
index 00000000..771acf3e
--- /dev/null
+++ b/apps/readest-app/src-tauri/plugins/tauri-plugin-safari-auth/src/models.rs
@@ -0,0 +1,13 @@
+use serde::{Deserialize, Serialize};
+
+#[derive(Debug, Deserialize, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct SafariAuthRequest {
+ pub auth_url: String,
+}
+
+#[derive(Debug, Clone, Default, Deserialize, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct SafariAuthResponse {
+ pub redirect_url: String,
+}
diff --git a/apps/readest-app/src-tauri/src/lib.rs b/apps/readest-app/src-tauri/src/lib.rs
index 00010272..2be54881 100644
--- a/apps/readest-app/src-tauri/src/lib.rs
+++ b/apps/readest-app/src-tauri/src/lib.rs
@@ -106,7 +106,8 @@ pub fn run() {
.plugin(tauri_plugin_http::init())
.plugin(tauri_plugin_os::init())
.plugin(tauri_plugin_dialog::init())
- .plugin(tauri_plugin_fs::init());
+ .plugin(tauri_plugin_fs::init())
+ .plugin(tauri_plugin_safari_auth::init());
#[cfg(desktop)]
let builder = builder.plugin(tauri_plugin_single_instance::init(|app, argv, cwd| {
diff --git a/apps/readest-app/src/app/auth/page.tsx b/apps/readest-app/src/app/auth/page.tsx
index ce11d004..90ae1219 100644
--- a/apps/readest-app/src/app/auth/page.tsx
+++ b/apps/readest-app/src/app/auth/page.tsx
@@ -22,6 +22,7 @@ import { start, cancel, onUrl, onInvalidUrl } from '@fabianlars/tauri-plugin-oau
import { openUrl } from '@tauri-apps/plugin-opener';
import { handleAuthCallback } from '@/helpers/auth';
import { getAppleIdAuth, Scope } from './utils/appleIdAuth';
+import { authWithSafari } from './utils/safariAuth';
type OAuthProvider = 'google' | 'apple' | 'azure' | 'github';
@@ -66,12 +67,16 @@ export default function AuthPage() {
const isOAuthServerRunning = useRef(false);
const [isMounted, setIsMounted] = useState(false);
- const getTauriRedirectTo = () => {
- return process.env.NODE_ENV === 'production' || appService?.isMobile
- ? appService?.isMobile
- ? WEB_AUTH_CALLBACK
- : DEEPLINK_CALLBACK
- : `http://localhost:${port}`; // only for development env on Desktop
+ const getTauriRedirectTo = (isOAuth: boolean) => {
+ if (process.env.NODE_ENV === 'production' || appService?.isMobile) {
+ if (appService?.isIOSApp) {
+ return isOAuth ? DEEPLINK_CALLBACK : WEB_AUTH_CALLBACK;
+ } else if (appService?.isAndroidApp) {
+ return WEB_AUTH_CALLBACK;
+ }
+ return DEEPLINK_CALLBACK;
+ }
+ return `http://localhost:${port}`; // only for development env on Desktop
};
const tauriSignInApple = async () => {
@@ -103,7 +108,7 @@ export default function AuthPage() {
provider,
options: {
skipBrowserRedirect: true,
- redirectTo: getTauriRedirectTo(),
+ redirectTo: getTauriRedirectTo(true),
},
});
@@ -111,7 +116,16 @@ export default function AuthPage() {
console.error('Authentication error:', error);
return;
}
- await openUrl(data.url);
+ // Open the OAuth URL in a ASWebAuthenticationSession on iOS to comply with Apple's guidelines
+ // for other platforms, open the OAuth URL in the default browser
+ if (appService?.isIOSApp) {
+ const res = await authWithSafari({ authUrl: data.url });
+ if (res) {
+ handleOAuthUrl(res.redirectUrl);
+ }
+ } else {
+ await openUrl(data.url);
+ }
};
const handleOAuthUrl = async (url: string) => {
@@ -318,16 +332,20 @@ export default function AuthPage() {
Icon={FaGithub}
label={_('Sign in with GitHub')}
/>
-
-
+ {!appService?.isIOSApp && (
+
+ )}
) : (
diff --git a/apps/readest-app/src/app/auth/utils/safariAuth.ts b/apps/readest-app/src/app/auth/utils/safariAuth.ts
new file mode 100644
index 00000000..080177a3
--- /dev/null
+++ b/apps/readest-app/src/app/auth/utils/safariAuth.ts
@@ -0,0 +1,17 @@
+import { invoke } from '@tauri-apps/api/core';
+
+export interface SafariAuthRequest {
+ authUrl: string;
+}
+
+export interface SafariAuthResponse {
+ redirectUrl: string;
+}
+
+export async function authWithSafari(request: SafariAuthRequest): Promise {
+ const result = await invoke('plugin:safari-auth|auth_with_safari', {
+ payload: request,
+ });
+
+ return result;
+}
diff --git a/apps/readest-app/src/pages/api/deepl/translate.ts b/apps/readest-app/src/pages/api/deepl/translate.ts
index 2445355d..81168d99 100644
--- a/apps/readest-app/src/pages/api/deepl/translate.ts
+++ b/apps/readest-app/src/pages/api/deepl/translate.ts
@@ -21,7 +21,7 @@ const getUserAndToken = async (authHeader: string | undefined) => {
const getDeepLAPIKey = (keys: string | undefined) => {
const keyArray = keys?.split(',') ?? [];
- return keyArray[Math.floor(Math.random() * keyArray.length)];
+ return keyArray.length ? keyArray[Math.floor(Math.random() * keyArray.length)] : '';
};
const handler = async (req: NextApiRequest, res: NextApiResponse) => {