-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🛂 Basic authentication implementation
- Loading branch information
Showing
22 changed files
with
456 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import 'package:open_media_server_app/helpers/Preferences.dart'; | ||
|
||
class BaseApi { | ||
static Map<String, String> getHeaders() { | ||
return { | ||
"Authorization": "Bearer ${Preferences.prefs?.getString("AccessToken")}" | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// 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:flutter/foundation.dart'; | ||
import 'package:oauth2_client/access_token_response.dart'; | ||
import 'package:oauth2_client/interfaces.dart'; | ||
import 'package:oauth2_client/oauth2_client.dart'; | ||
import 'package:open_media_server_app/globals.dart'; | ||
import 'package:open_media_server_app/helpers/Preferences.dart'; | ||
import 'package:random_string/random_string.dart'; | ||
|
||
class LoginManager { | ||
late OAuth2Client client; | ||
|
||
BaseWebAuth? baseWebAuth; | ||
|
||
LoginManager() { | ||
if (!Globals.isWeb) { | ||
client = OAuth2Client( | ||
authorizeUrl: Globals.AuthorizeUrl, | ||
tokenUrl: Globals.TokenUrl, | ||
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, | ||
// ); | ||
} | ||
|
||
// if (kIsWeb) { | ||
// baseWebAuth = CustomWebBase(); | ||
// } | ||
} | ||
|
||
Future<String?> login(String clientId, String clientSecret) async { | ||
var state = Preferences.prefs?.getString("OAuth_State"); | ||
var codeVerifier = Preferences.prefs?.getString("OAuth_CodeVerifier"); | ||
|
||
if (kIsWeb && codeVerifier == null) { | ||
codeVerifier = randomAlphaNumeric(80); | ||
Preferences.prefs?.setString("OAuth_CodeVerifier", codeVerifier); | ||
} | ||
|
||
AccessTokenResponse tknResponse = await client.getTokenWithAuthCodeFlow( | ||
clientId: clientId, | ||
clientSecret: Uri.encodeQueryComponent(clientSecret), | ||
scopes: ["offline_access"], | ||
webAuthClient: baseWebAuth, | ||
state: state, | ||
codeVerifier: codeVerifier, | ||
); | ||
|
||
var refreshToken = tknResponse.refreshToken; | ||
if (refreshToken != null) { | ||
Preferences.prefs?.setString("RefreshToken", refreshToken); | ||
} | ||
|
||
Preferences.prefs?.setString("AccessToken", tknResponse.accessToken!); | ||
|
||
return tknResponse.accessToken; | ||
} | ||
|
||
Future<String?> refreshAsync() async { | ||
String clientId = Preferences.prefs!.getString("ClientId")!; | ||
String clientSecret = Preferences.prefs!.getString("ClientSecret")!; | ||
|
||
var tknResponse = await client.refreshToken( | ||
Preferences.prefs!.getString("RefreshToken")!, | ||
clientId: clientId, | ||
clientSecret: Uri.encodeQueryComponent(clientSecret), | ||
); | ||
|
||
Preferences.prefs?.setString("AccessToken", tknResponse.accessToken!); | ||
Preferences.prefs?.setString("RefreshToken", tknResponse.refreshToken!); | ||
|
||
return tknResponse.accessToken; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,16 @@ | ||
class Globals { | ||
static String BaseUrl = "http://localhost:8080"; | ||
|
||
static String ClientId = ""; | ||
static String ClientSecret = ""; | ||
static String AuthorizeUrl = ""; | ||
static String TokenUrl = ""; | ||
|
||
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"; | ||
|
||
static bool isTv = false; | ||
static bool isMobile = false; | ||
static bool isWeb = false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
|
||
class Preferences { | ||
static SharedPreferences? prefs; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.