diff --git a/LICENSE b/LICENSE index 01b4308..a60ace9 100644 --- a/LICENSE +++ b/LICENSE @@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. \ No newline at end of file +SOFTWARE. diff --git a/lib/solid_auth_client.dart b/lib/solid_auth_client.dart index 3e1c797..aa021c4 100644 --- a/lib/solid_auth_client.dart +++ b/lib/solid_auth_client.dart @@ -321,6 +321,14 @@ Future authenticate( } Future logout(logoutUrl) async { + // Clear platform-specific storage (Web: localStorage, Native: SharedPreferences) + try { + await authManager.clearLocalStorage(); + } catch (e) { + debugPrint('logout() => Warning: Failed to clear storage: $e'); + // Continue with logout flow even if storage clearing fails + } + Uri url = Uri.parse(logoutUrl); if (await canLaunchUrl(url)) { diff --git a/lib/src/auth_manager/auth_manager_abstract.dart b/lib/src/auth_manager/auth_manager_abstract.dart index d880fb9..db964bd 100644 --- a/lib/src/auth_manager/auth_manager_abstract.dart +++ b/lib/src/auth_manager/auth_manager_abstract.dart @@ -44,6 +44,7 @@ abstract class AuthManager { createAuthenticator(Client client, List scopes, String dPopToken) {} getOidcWeb() {} userLogout(String logoutUrl) {} + Future clearLocalStorage() async {} // factory constructor to return the correct implementation. factory AuthManager() => getAuthManager(); diff --git a/lib/src/auth_manager/auth_manager_stub.dart b/lib/src/auth_manager/auth_manager_stub.dart index 1f25ffc..6c64e33 100644 --- a/lib/src/auth_manager/auth_manager_stub.dart +++ b/lib/src/auth_manager/auth_manager_stub.dart @@ -28,7 +28,6 @@ library; import 'package:solid_auth/src/auth_manager/auth_manager_abstract.dart'; +import 'package:solid_auth/src/auth_manager/native_auth_manager.dart'; -AuthManager getAuthManager() => throw UnsupportedError( - 'Cannot create a keyfinder without the packages dart:html or package:shared_preferences', - ); +AuthManager getAuthManager() => NativeAuthManager(); diff --git a/lib/src/auth_manager/native_auth_manager.dart b/lib/src/auth_manager/native_auth_manager.dart new file mode 100644 index 0000000..580706f --- /dev/null +++ b/lib/src/auth_manager/native_auth_manager.dart @@ -0,0 +1,79 @@ +/// Native Auth Manager for non-web platforms. +/// +/// Copyright (C) 2025, Software Innovation Institute, ANU. +/// +/// Licensed under the MIT License (the "License"). +/// +/// License: https://choosealicense.com/licenses/mit/. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. +/// +/// Authors: Anushka Vidanage +library; + +import 'package:shared_preferences/shared_preferences.dart'; +import 'package:solid_auth/src/auth_manager/auth_manager_abstract.dart'; +import 'package:solid_auth/src/openid/src/openid.dart'; + +/// Auth manager implementation for native platforms (Linux, Windows, macOS, iOS, Android). +/// +/// This provides platform-specific implementations using SharedPreferences +/// for storage operations on native platforms. +class NativeAuthManager implements AuthManager { + @override + String getKeyValue(String key) { + // Native platforms don't use localStorage + return ''; + } + + @override + getWebUrl() { + // Not applicable for native platforms + return null; + } + + @override + createAuthenticator(Client client, List scopes, String dPopToken) { + // Not applicable for native platforms + return null; + } + + @override + getOidcWeb() { + // Not applicable for native platforms + return null; + } + + @override + userLogout(String logoutUrl) { + // Native platforms handle logout through URL launcher, not window.open + } + + @override + Future clearLocalStorage() async { + try { + final prefs = await SharedPreferences.getInstance(); + await prefs.clear(); + } catch (e) { + throw Exception('Failed to clear SharedPreferences: $e'); + } + } +} + +AuthManager getAuthManager() => NativeAuthManager(); diff --git a/lib/src/auth_manager/web_auth_manager.dart b/lib/src/auth_manager/web_auth_manager.dart index f48d3b2..88065ad 100644 --- a/lib/src/auth_manager/web_auth_manager.dart +++ b/lib/src/auth_manager/web_auth_manager.dart @@ -80,6 +80,15 @@ class WebAuthManager implements AuthManager { final child = window.open(logoutUrl, 'user_logout'); child!.close(); } + + @override + Future clearLocalStorage() async { + try { + windowLoc.localStorage.clear(); + } catch (e) { + throw Exception('Failed to clear localStorage: $e'); + } + } } AuthManager getAuthManager() => WebAuthManager(); diff --git a/pubspec.yaml b/pubspec.yaml index 6619c39..ad179b5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -22,6 +22,7 @@ dependencies: url_launcher: ^6.3.1 uuid: ^4.5.1 web: ^1.1.1 + shared_preferences: ^2.2.2 dev_dependencies: flutter_lints: ^5.0.0