feat(auth): prepare MSAL browser integration
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import "msal_bridge.dart";
|
||||
|
||||
enum AuthenticationMode { demo, azure }
|
||||
|
||||
class AuthService {
|
||||
@@ -17,6 +19,7 @@ class AuthService {
|
||||
static const String demoResponsableEmail = "karim.benali@ensup.eu";
|
||||
|
||||
static String? _demoEmail;
|
||||
static bool _azureInitialized = false;
|
||||
|
||||
static AuthenticationMode get mode {
|
||||
return switch (_configuredMode) {
|
||||
@@ -40,9 +43,31 @@ class AuthService {
|
||||
_demoEmail = null;
|
||||
}
|
||||
|
||||
static Map<String, String> requestHeaders() {
|
||||
static Future<void> signInWithMicrosoft() async {
|
||||
await _initializeAzure();
|
||||
await MsalBridge.signIn();
|
||||
}
|
||||
|
||||
static Future<bool> hasMicrosoftAccount() async {
|
||||
await _initializeAzure();
|
||||
return MsalBridge.hasAccount();
|
||||
}
|
||||
|
||||
static Future<void> signOut() async {
|
||||
if (mode == AuthenticationMode.azure) {
|
||||
throw StateError("La session Microsoft n'est pas encore initialisee.");
|
||||
await _initializeAzure();
|
||||
await MsalBridge.signOut();
|
||||
return;
|
||||
}
|
||||
|
||||
clearSession();
|
||||
}
|
||||
|
||||
static Future<Map<String, String>> requestHeaders() async {
|
||||
if (mode == AuthenticationMode.azure) {
|
||||
await _initializeAzure();
|
||||
final token = await MsalBridge.getAccessToken();
|
||||
return {"authorization": "Bearer $token"};
|
||||
}
|
||||
|
||||
final email = _demoEmail;
|
||||
@@ -53,6 +78,28 @@ class AuthService {
|
||||
return {"x-user-email": email};
|
||||
}
|
||||
|
||||
static Future<void> _initializeAzure() async {
|
||||
if (mode != AuthenticationMode.azure) {
|
||||
throw StateError("MSAL est disponible uniquement en mode Azure.");
|
||||
}
|
||||
if (_azureInitialized) {
|
||||
return;
|
||||
}
|
||||
if (azureTenantId.isEmpty ||
|
||||
azureSpaClientId.isEmpty ||
|
||||
azureApiScope.isEmpty) {
|
||||
throw StateError("La configuration Azure du frontend est incomplete.");
|
||||
}
|
||||
|
||||
await MsalBridge.initialize(
|
||||
tenantId: azureTenantId,
|
||||
clientId: azureSpaClientId,
|
||||
scope: azureApiScope,
|
||||
redirectUri: Uri.base.origin,
|
||||
);
|
||||
_azureInitialized = true;
|
||||
}
|
||||
|
||||
static void _ensureDemoMode() {
|
||||
if (mode != AuthenticationMode.demo) {
|
||||
throw StateError(
|
||||
|
||||
Reference in New Issue
Block a user