Skip to content

Commit

Permalink
Move aai envs from .env to constants file (#1262)
Browse files Browse the repository at this point in the history
  • Loading branch information
thePeras authored Jul 22, 2024
2 parents 8322384 + 440302e commit 99212cc
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 74 deletions.
4 changes: 0 additions & 4 deletions uni/assets/env/.env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
# Plausbile
PLAUSIBLE_URL=https://plausible.example.com
PLAUSIBLE_DOMAIN=your.plausible.domain

# AAI - Federated Authentication
REALM=https://open-id.up.pt/realms/sigarra
CLIENT_ID=mobile-app-uni
126 changes: 63 additions & 63 deletions uni/lib/controller/background_workers/notifications.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,69 +68,6 @@ class NotificationManager {

static const Duration _notificationWorkerPeriod = Duration(hours: 1);

static Future<void> updateAndTriggerNotifications() async {
PreferencesController.prefs = await SharedPreferences.getInstance();
final userInfo = await PreferencesController.getPersistentUserInfo();
final faculties = PreferencesController.getUserFaculties();
final refreshToken = await PreferencesController.getSessionRefreshToken();

if (faculties.isEmpty) {
return;
}
if (userInfo == null && refreshToken == null) {
return; // Session not persistent
}

Session? session;
if (userInfo != null) {
session = await NetworkRouter.login(
userInfo.item1,
userInfo.item2,
faculties,
persistentSession: false,
);
}

if (refreshToken != null) {
final token = await NetworkRouter.getAccessToken(refreshToken);
final studentNumber = await PreferencesController.getUserNumber();
if (token == null || studentNumber == null) {
return;
}
session = await NetworkRouter.loginWithToken(
token,
studentNumber,
faculties,
persistentSession: false,
);
}

if (session == null) {
return;
}

// Get the .json file that contains the last time that the
// notification has ran
await _initFlutterNotificationsPlugin();
final notificationStorage = await NotificationTimeoutStorage.create();

for (final value in notificationMap.values) {
final notification = value();
final lastRan = notificationStorage
.getLastTimeNotificationExecuted(notification.uniqueID);
if (lastRan.add(notification.timeout).isBefore(DateTime.now())) {
await notification.displayNotificationIfPossible(
session,
_localNotificationsPlugin,
);
await notificationStorage.addLastTimeNotificationExecuted(
notification.uniqueID,
DateTime.now(),
);
}
}
}

Future<void> initializeNotifications() async {
// guarantees that the execution is only done
// once in the lifetime of the app.
Expand Down Expand Up @@ -203,4 +140,67 @@ class NotificationManager {
);
}
}

static Future<void> updateAndTriggerNotifications() async {
PreferencesController.prefs = await SharedPreferences.getInstance();
final userInfo = await PreferencesController.getPersistentUserInfo();
final faculties = PreferencesController.getUserFaculties();
final refreshToken = await PreferencesController.getSessionRefreshToken();

if (faculties.isEmpty) {
return;
}
if (userInfo == null && refreshToken == null) {
return; // Session not persistent
}

Session? session;
if (userInfo != null) {
session = await NetworkRouter.login(
userInfo.item1,
userInfo.item2,
faculties,
persistentSession: false,
);
}

if (refreshToken != null) {
final token = await NetworkRouter.getAccessToken(refreshToken);
final studentNumber = await PreferencesController.getUserNumber();
if (token == null || studentNumber == null) {
return;
}
session = await NetworkRouter.loginWithToken(
token,
studentNumber,
faculties,
persistentSession: false,
);
}

if (session == null) {
return;
}

// Get the .json file that contains the last time that the
// notification has ran
await _initFlutterNotificationsPlugin();
final notificationStorage = await NotificationTimeoutStorage.create();

for (final value in notificationMap.values) {
final notification = value();
final lastRan = notificationStorage
.getLastTimeNotificationExecuted(notification.uniqueID);
if (lastRan.add(notification.timeout).isBefore(DateTime.now())) {
await notification.displayNotificationIfPossible(
session,
_localNotificationsPlugin,
);
await notificationStorage.addLastTimeNotificationExecuted(
notification.uniqueID,
DateTime.now(),
);
}
}
}
}
6 changes: 2 additions & 4 deletions uni/lib/controller/networking/network_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:http/http.dart' as http;
import 'package:http/http.dart';
import 'package:logger/logger.dart';
import 'package:openid_client/openid_client.dart';
import 'package:synchronized/synchronized.dart';
import 'package:uni/controller/local_storage/preferences_controller.dart';
import 'package:uni/model/entities/session.dart';
import 'package:uni/utils/constants.dart';
import 'package:uni/view/navigation_service.dart';

extension UriString on String {
Expand Down Expand Up @@ -198,7 +198,6 @@ class NetworkRouter {

/// Get a new accessing Refresh with the refresh token
static Future<String?> getAccessToken(String refreshToken) async {
final realm = dotenv.env['REALM'] ?? '';
final issuer = await Issuer.discover(Uri.parse(realm));
if (issuer.metadata.tokenEndpoint == null) {
Logger().e('Re-login failed: token endpoint not found');
Expand All @@ -210,8 +209,7 @@ class NetworkRouter {
body: {
'grant_type': 'refresh_token',
'refresh_token': refreshToken,
'client_id': dotenv.env['CLIENT_ID'],
'client_secret': dotenv.env['CLIENT_SECRET'],
'client_id': clientId,
},
);

Expand Down
5 changes: 2 additions & 3 deletions uni/lib/model/providers/startup/session_provider.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:async';

import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:logger/logger.dart';
import 'package:openid_client/openid_client.dart';
import 'package:uni/controller/background_workers/notifications.dart';
Expand All @@ -14,6 +13,7 @@ import 'package:uni/model/entities/session.dart';
import 'package:uni/model/providers/state_provider_notifier.dart';
import 'package:uni/model/providers/state_providers.dart';
import 'package:uni/model/request_status.dart';
import 'package:uni/utils/constants.dart';
import 'package:url_launcher/url_launcher.dart';

class SessionProvider extends StateProviderNotifier<Session> {
Expand Down Expand Up @@ -120,11 +120,10 @@ class SessionProvider extends StateProviderNotifier<Session> {
}) async {
_persistentSession = persistentSession;

final realm = dotenv.env['REALM'] ?? '';
final issuer = await Issuer.discover(Uri.parse(realm));
final client = Client(
issuer,
dotenv.env['CLIENT_ID']!,
clientId,
);

_flow = Flow.authorizationCodeWithPKCE(
Expand Down
3 changes: 3 additions & 0 deletions uni/lib/utils/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ const faculties = [
'fpceup',
'icbas',
];

const realm = 'https://open-id.up.pt/realms/sigarra';
const clientId = 'mobile-app-uni';

0 comments on commit 99212cc

Please sign in to comment.