Skip to content

Commit

Permalink
🛂 Changed to authinfo endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
LNA-DEV committed Oct 31, 2024
1 parent 212b3e0 commit 3f33f0f
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 18 deletions.
25 changes: 25 additions & 0 deletions lib/apis/auth_info_api.dart
Original file line number Diff line number Diff line change
@@ -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<AuthInfo> 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');
}
}
}
12 changes: 8 additions & 4 deletions lib/auth/device_code.dart
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -22,11 +22,11 @@ class DeviceCode {
}
}

Future<String?> authenticateUser(String clientId, String scope,
Future<String?> 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'];

Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 7 additions & 6 deletions lib/auth/login_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,21 @@ 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 {
late OAuth2Client client;

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",
);
Expand All @@ -43,10 +44,10 @@ class LoginManager {
// }
}

Future<String?> login(String clientId, BuildContext context) async {
Future<String?> 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!);

Expand All @@ -62,7 +63,7 @@ class LoginManager {
}

AccessTokenResponse tknResponse = await client.getTokenWithAuthCodeFlow(
clientId: clientId,
clientId: authInfo.clientId,
scopes: ["offline_access"],
webAuthClient: baseWebAuth,
state: state,
Expand Down
5 changes: 0 additions & 5 deletions lib/globals.dart
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
10 changes: 7 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
}
32 changes: 32 additions & 0 deletions lib/models/auth/auth_info.dart
Original file line number Diff line number Diff line change
@@ -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<String, dynamic> 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<String, dynamic> toJson() {
return {
'authorizeUrl': authorizeUrl,
'deviceCodeUrl': deviceCodeUrl,
'tokenUrl': tokenUrl,
'clientId': clientId,
};
}
}

0 comments on commit 3f33f0f

Please sign in to comment.