Skip to content

Commit

Permalink
🛂 Authentication in web
Browse files Browse the repository at this point in the history
  • Loading branch information
LNA-DEV committed Nov 1, 2024
1 parent 8b07541 commit 9074f8e
Show file tree
Hide file tree
Showing 14 changed files with 125 additions and 17 deletions.
26 changes: 26 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "OpenMediaStation.FE.MovieTV",
"request": "launch",
"type": "dart",
"args": ["-d", "chrome","--web-port", "8000"]
},
{
"name": "OpenMediaStation.FE.MovieTV (profile mode)",
"request": "launch",
"type": "dart",
"flutterMode": "profile"
},
{
"name": "OpenMediaStation.FE.MovieTV (release mode)",
"request": "launch",
"type": "dart",
"flutterMode": "release"
}
]
}
Binary file added assets/OMS logo 16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/OMS logo 512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions lib/auth/auth_globals.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:flutter/foundation.dart';
import 'package:open_media_server_app/globals.dart';

class AuthGlobals {
static String get redirectUriWeb {
if (kDebugMode) {
return "http://localhost:8000/redirect.html";
} else {
return "https://${Globals.BaseUrl}/redirect.html";
}
}
static String? appLoginCodeRoute;
}
33 changes: 16 additions & 17 deletions lib/auth/login_manager.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// import 'package:fedodo_general/globals/preferences.dart';
// import 'package:fedodo_general/widgets/auth/oauth_handler/custom_web_base_dummy.dart'
// if (dart.library.html) '../oauth_handler/custom_web_base.dart';
import 'package:open_media_server_app/auth/auth_globals.dart';
import 'package:open_media_server_app/auth/oauth_handler/custom_web_base_dummy.dart'
if (dart.library.html) './oauth_handler/custom_web_base.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:oauth2_client/access_token_response.dart';
Expand All @@ -27,27 +27,26 @@ class LoginManager {
redirectUri: "my.test.app:/oauth2redirect", // TODO
customUriScheme: "my.test.app",
);
} else {
// client = OAuth2Client(
// authorizeUrl:
// "https://auth.${Preferences.prefs!.getString("DomainName")}/oauth/authorize",
// tokenUrl:
// "https://auth.${Preferences.prefs!.getString("DomainName")}/oauth/token",
// redirectUri: AuthGlobals.redirectUriWeb,
// // refreshUrl: "https://auth.${GlobalSettings.domainName}/oauth/token",
// customUriScheme: Uri.parse(AuthGlobals.redirectUriWeb).authority,
// );
} else if (Globals.isWeb) {
client = OAuth2Client(
authorizeUrl: authInfo.authorizeUrl,
tokenUrl: authInfo.tokenUrl,
redirectUri: AuthGlobals.redirectUriWeb,
// refreshUrl: "https://auth.${GlobalSettings.domainName}/oauth/token",
customUriScheme: Uri.parse(AuthGlobals.redirectUriWeb).authority,
);
}

// if (kIsWeb) {
// baseWebAuth = CustomWebBase();
// }
if (kIsWeb) {
baseWebAuth = CustomWebBase();
}
}

Future<String?> login(AuthInfo authInfo, BuildContext context) async {
if (Globals.isTv) {
DeviceCode deviceCode = DeviceCode();
var token = await deviceCode.authenticateUser(authInfo, "offline_access", authInfo.deviceCodeUrl, context);
var token = await deviceCode.authenticateUser(
authInfo, "offline_access", authInfo.deviceCodeUrl, context);

Preferences.prefs?.setString("AccessToken", token!);

Expand Down
39 changes: 39 additions & 0 deletions lib/auth/oauth_handler/custom_web_base.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import 'package:oauth2_client/interfaces.dart';
import 'package:open_media_server_app/auth/auth_globals.dart';
// ignore: avoid_web_libraries_in_flutter
import 'dart:html' as html;

import 'package:open_media_server_app/helpers/Preferences.dart';

class CustomWebBase implements BaseWebAuth {
@override
Future<String> authenticate({
required String callbackUrlScheme,
required String url,
required String redirectUrl,
Map<String, dynamic>? opts,
}) async {

if(AuthGlobals.appLoginCodeRoute == null){
Uri uri = Uri.parse(url);

String state = uri.queryParameters["state"]!;

Preferences.prefs?.setString("OAuth_State", state);

html.window.open(url, "_self");

await Future.delayed(const Duration(seconds: 10));

return "";
}else{
Preferences.prefs?.remove("OAuth_State");
var temp = AuthGlobals.appLoginCodeRoute;
AuthGlobals.appLoginCodeRoute = null;

return html.window.origin! + temp.toString();
}


}
}
13 changes: 13 additions & 0 deletions lib/auth/oauth_handler/custom_web_base_dummy.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:oauth2_client/interfaces.dart';

class CustomWebBase implements BaseWebAuth {
@override
Future<String> authenticate({
required String callbackUrlScheme,
required String url,
required String redirectUrl,
Map<String, dynamic>? opts,
}) {
throw UnimplementedError();
}
}
8 changes: 8 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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/auth_globals.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 @@ -46,6 +47,13 @@ class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
onGenerateRoute: (settings) {
if (settings.name?.contains("code") ?? false) {
AuthGlobals.appLoginCodeRoute = settings.name;
}

return null;
},
title: Globals.Title,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(
Expand Down
Binary file modified web/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified web/icons/Icon-192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified web/icons/Icon-512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified web/icons/Icon-maskable-192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified web/icons/Icon-maskable-512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions web/redirect.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<script>
window.onload = function() {
const urlParams = new URLSearchParams(window.location.search);
const code = urlParams.get('code');
if(code) {
let newUrl = window.location.origin + "/#/auth?" + urlParams.toString();
window.location.href = newUrl;
}
}
</script>

0 comments on commit 9074f8e

Please sign in to comment.