feat(frontend): simulate authenticated user sessions
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
enum AuthenticationMode { demo, azure }
|
||||
|
||||
class AuthService {
|
||||
AuthService._();
|
||||
|
||||
static const String _configuredMode = String.fromEnvironment(
|
||||
"AUTH_MODE",
|
||||
defaultValue: "demo",
|
||||
);
|
||||
static const String azureTenantId = String.fromEnvironment("AZURE_TENANT_ID");
|
||||
static const String azureSpaClientId = String.fromEnvironment(
|
||||
"AZURE_SPA_CLIENT_ID",
|
||||
);
|
||||
static const String azureApiScope = String.fromEnvironment("AZURE_API_SCOPE");
|
||||
|
||||
static const String demoStudentEmail = "lucas.martin@ensitech.eu";
|
||||
static const String demoResponsableEmail = "karim.benali@ensup.eu";
|
||||
|
||||
static String? _demoEmail;
|
||||
|
||||
static AuthenticationMode get mode {
|
||||
return switch (_configuredMode) {
|
||||
"demo" => AuthenticationMode.demo,
|
||||
"azure" => AuthenticationMode.azure,
|
||||
_ => throw StateError("AUTH_MODE doit valoir demo ou azure."),
|
||||
};
|
||||
}
|
||||
|
||||
static void selectDemoStudent() {
|
||||
_ensureDemoMode();
|
||||
_demoEmail = demoStudentEmail;
|
||||
}
|
||||
|
||||
static void selectDemoResponsable() {
|
||||
_ensureDemoMode();
|
||||
_demoEmail = demoResponsableEmail;
|
||||
}
|
||||
|
||||
static void clearSession() {
|
||||
_demoEmail = null;
|
||||
}
|
||||
|
||||
static Map<String, String> requestHeaders() {
|
||||
if (mode == AuthenticationMode.azure) {
|
||||
throw StateError("La session Microsoft n'est pas encore initialisee.");
|
||||
}
|
||||
|
||||
final email = _demoEmail;
|
||||
if (email == null) {
|
||||
throw StateError("Aucun utilisateur de demonstration selectionne.");
|
||||
}
|
||||
|
||||
return {"x-user-email": email};
|
||||
}
|
||||
|
||||
static void _ensureDemoMode() {
|
||||
if (mode != AuthenticationMode.demo) {
|
||||
throw StateError(
|
||||
"Une identite de demonstration est interdite en mode Azure.",
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user