From 3f33f0f6480841558f0c000316757c16612cef0e Mon Sep 17 00:00:00 2001 From: Lukas Nagel Date: Thu, 31 Oct 2024 23:14:58 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=82=20Changed=20to=20authinfo=20endpoi?= =?UTF-8?q?nt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/apis/auth_info_api.dart | 25 +++++++++++++++++++++++++ lib/auth/device_code.dart | 12 ++++++++---- lib/auth/login_manager.dart | 13 +++++++------ lib/globals.dart | 5 ----- lib/main.dart | 10 +++++++--- lib/models/auth/auth_info.dart | 32 ++++++++++++++++++++++++++++++++ 6 files changed, 79 insertions(+), 18 deletions(-) create mode 100644 lib/apis/auth_info_api.dart create mode 100644 lib/models/auth/auth_info.dart diff --git a/lib/apis/auth_info_api.dart b/lib/apis/auth_info_api.dart new file mode 100644 index 0000000..d1a8a18 --- /dev/null +++ b/lib/apis/auth_info_api.dart @@ -0,0 +1,25 @@ +import 'dart:convert'; +import 'package:open_media_server_app/apis/base_api.dart'; +import 'package:http/http.dart' as http; +import 'package:open_media_server_app/globals.dart'; +import 'package:open_media_server_app/models/auth/auth_info.dart'; + +class AuthInfoApi { + Future getAuthInfo() async { + String apiUrl = "${Globals.BaseUrl}/auth/info"; + + var headers = BaseApi.getHeaders(); + + var response = await http.get( + Uri.parse(apiUrl), + headers: headers, + ); + + if (response.statusCode == 200) { + var decoded = json.decode(response.body); + return AuthInfo.fromJson(decoded); + } else { + throw Exception('Failed to load auth info'); + } + } +} diff --git a/lib/auth/device_code.dart b/lib/auth/device_code.dart index a7e9ff0..268ab8d 100644 --- a/lib/auth/device_code.dart +++ b/lib/auth/device_code.dart @@ -1,7 +1,7 @@ import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; -import 'package:open_media_server_app/globals.dart'; +import 'package:open_media_server_app/models/auth/auth_info.dart'; import 'package:qr_flutter/qr_flutter.dart'; class DeviceCode { @@ -22,11 +22,11 @@ class DeviceCode { } } - Future authenticateUser(String clientId, String scope, + Future authenticateUser(AuthInfo authInfo, String scope, String deviceCodeUrl, BuildContext context) async { try { final deviceCodeResponse = - await getDeviceCode(clientId, scope, deviceCodeUrl); + await getDeviceCode(authInfo.clientId, scope, deviceCodeUrl); final userCode = deviceCodeResponse['user_code']; final verificationUri = deviceCodeResponse['verification_uri']; @@ -74,7 +74,11 @@ class DeviceCode { final code = deviceCodeResponse['device_code']; final interval = deviceCodeResponse['interval']; var token = await pollForToken( - code, interval, Globals.ClientId, Globals.TokenUrl); + code, + interval, + authInfo.clientId, + authInfo.tokenUrl, + ); return token; } catch (e) { diff --git a/lib/auth/login_manager.dart b/lib/auth/login_manager.dart index 4c1b423..5d18baf 100644 --- a/lib/auth/login_manager.dart +++ b/lib/auth/login_manager.dart @@ -9,6 +9,7 @@ import 'package:oauth2_client/oauth2_client.dart'; import 'package:open_media_server_app/auth/device_code.dart'; import 'package:open_media_server_app/globals.dart'; import 'package:open_media_server_app/helpers/Preferences.dart'; +import 'package:open_media_server_app/models/auth/auth_info.dart'; import 'package:random_string/random_string.dart'; class LoginManager { @@ -16,13 +17,13 @@ class LoginManager { BaseWebAuth? baseWebAuth; - LoginManager() { + LoginManager(AuthInfo authInfo) { if (Globals.isTv) { // Do nothing } else if (!Globals.isWeb) { client = OAuth2Client( - authorizeUrl: Globals.AuthorizeUrl, - tokenUrl: Globals.TokenUrl, + authorizeUrl: authInfo.authorizeUrl, + tokenUrl: authInfo.tokenUrl, redirectUri: "my.test.app:/oauth2redirect", // TODO customUriScheme: "my.test.app", ); @@ -43,10 +44,10 @@ class LoginManager { // } } - Future login(String clientId, BuildContext context) async { + Future login(AuthInfo authInfo, BuildContext context) async { if (Globals.isTv) { DeviceCode deviceCode = DeviceCode(); - var token = await deviceCode.authenticateUser(Globals.ClientId, "offline_access", Globals.DeviceCodeUrl, context); + var token = await deviceCode.authenticateUser(authInfo, "offline_access", authInfo.deviceCodeUrl, context); Preferences.prefs?.setString("AccessToken", token!); @@ -62,7 +63,7 @@ class LoginManager { } AccessTokenResponse tknResponse = await client.getTokenWithAuthCodeFlow( - clientId: clientId, + clientId: authInfo.clientId, scopes: ["offline_access"], webAuthClient: baseWebAuth, state: state, diff --git a/lib/globals.dart b/lib/globals.dart index 9a35a6d..9faeed1 100644 --- a/lib/globals.dart +++ b/lib/globals.dart @@ -1,11 +1,6 @@ class Globals { static String BaseUrl = "http://localhost:8080"; - static String ClientId = ""; - static String AuthorizeUrl = ""; - static String TokenUrl = ""; - static String DeviceCodeUrl = ""; - static String Title = "Open Media Station"; static String PictureNotFoundUrl = "https://static.vecteezy.com/system/resources/previews/005/337/799/original/icon-image-not-found-free-vector.jpg"; diff --git a/lib/main.dart b/lib/main.dart index b92b0cb..7f6b10c 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -2,6 +2,7 @@ import 'package:device_info_plus/device_info_plus.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:media_kit/media_kit.dart'; +import 'package:open_media_server_app/apis/auth_info_api.dart'; import 'package:open_media_server_app/auth/login_manager.dart'; import 'package:open_media_server_app/gallery.dart'; import 'package:open_media_server_app/globals.dart'; @@ -87,8 +88,11 @@ class HomePage extends StatelessWidget { } Future authenticate(BuildContext context) async { - LoginManager loginManager = LoginManager(); - var token = - await loginManager.login(Globals.ClientId, context); + AuthInfoApi authInfoApi = AuthInfoApi(); + var info = await authInfoApi.getAuthInfo(); + + LoginManager loginManager = LoginManager(info); + + var token = await loginManager.login(info, context); } } diff --git a/lib/models/auth/auth_info.dart b/lib/models/auth/auth_info.dart new file mode 100644 index 0000000..b58d241 --- /dev/null +++ b/lib/models/auth/auth_info.dart @@ -0,0 +1,32 @@ +class AuthInfo { + String authorizeUrl; + String deviceCodeUrl; + String tokenUrl; + String clientId; + + AuthInfo({ + required this.authorizeUrl, + required this.deviceCodeUrl, + required this.tokenUrl, + required this.clientId, + }); + + factory AuthInfo.fromJson(Map json) { + return AuthInfo( + authorizeUrl: json['authorizeUrl'] as String, + deviceCodeUrl: json['deviceCodeUrl'] as String, + tokenUrl: json['tokenUrl'] as String, + clientId: json['clientId'] as String, + ); + } + + // Method for converting an instance to JSON + Map toJson() { + return { + 'authorizeUrl': authorizeUrl, + 'deviceCodeUrl': deviceCodeUrl, + 'tokenUrl': tokenUrl, + 'clientId': clientId, + }; + } +} \ No newline at end of file