From 02b3ce4524833aba40601c8b26dfc7cceded01a5 Mon Sep 17 00:00:00 2001 From: bardram Date: Mon, 10 Jun 2024 21:51:41 +0200 Subject: [PATCH] upgrade of demo app to new authentication model --- .../lib/src/blocs/carp_backend.dart | 70 ++++++++++++------- .../lib/src/blocs/sensing.dart | 4 +- .../lib/src/blocs/sensing_bloc.dart | 6 +- .../test/CAMS_app_test.dart | 2 +- .../test/caws_app_test.dart | 51 ++++++-------- 5 files changed, 71 insertions(+), 62 deletions(-) diff --git a/apps/carp_mobile_sensing_app/lib/src/blocs/carp_backend.dart b/apps/carp_mobile_sensing_app/lib/src/blocs/carp_backend.dart index a38677c3..5d09cd5f 100644 --- a/apps/carp_mobile_sensing_app/lib/src/blocs/carp_backend.dart +++ b/apps/carp_mobile_sensing_app/lib/src/blocs/carp_backend.dart @@ -6,53 +6,69 @@ part of mobile_sensing_app; class CarpBackend { static const String HOST_URI = "carp.computerome.dk"; + /// The URIs of the CARP Web Service (CAWS) host for each [DeploymentMode]. static const Map uris = { - DeploymentMode.development: 'dev', - DeploymentMode.staging: 'stage', - DeploymentMode.production: '', + DeploymentMode.dev: 'dev.carp.dk', + DeploymentMode.test: 'test.carp.dk', + DeploymentMode.production: 'carp.computerome.dk', }; static final CarpBackend _instance = CarpBackend._(); CarpBackend._() : super(); factory CarpBackend() => _instance; - CarpApp? _app; - /// The signed in user - CarpUser? get user => CarpService().currentUser; + CarpUser? get user => CarpAuthService().currentUser; /// The username of the signed in user. - String? get username => CarpService().currentUser.username; + String? get username => CarpAuthService().currentUser.username; - /// The URI of the CANS server - depending on deployment mode. + /// The URI of the CAWS server - depending on deployment mode. Uri get uri => Uri( scheme: 'https', - host: HOST_URI, + host: uris[bloc.deploymentMode], + ); + + /// The URI of the CAWS authentication service. + /// + /// Of the form: + /// https://dev.carp.dk/auth/realms/Carp/ + Uri get authUri => Uri( + scheme: 'https', + host: uris[bloc.deploymentMode], pathSegments: [ 'auth', - uris[bloc.deploymentMode]!, 'realms', 'Carp', ], ); - CarpApp? get app => _app; + /// The CAWS app configuration. + late final CarpApp _app = CarpApp( + name: "CAWS @ DTU", + uri: uri, + studyId: bloc.studyId, + studyDeploymentId: bloc.studyDeploymentId, + ); + + CarpApp get app => _app; + + /// The authentication configuration + CarpAuthProperties get authProperties => CarpAuthProperties( + authURL: uri, + clientId: 'studies-app', + redirectURI: Uri.parse('carp-studies-auth://auth'), + // For authentication at CAWS the path is '/auth/realms/Carp' + discoveryURL: uri.replace(pathSegments: [ + 'auth', + 'realms', + 'Carp', + ]), + ); Future initialize() async { - _app = CarpApp( - name: 'CAWS -${bloc.deploymentMode.name}', - uri: uri.replace(pathSegments: [uris[bloc.deploymentMode]!]), - authURL: uri, - clientId: 'carp-webservices-dart', - redirectURI: Uri.parse('carp-studies-auth://auth'), - discoveryURL: uri.replace(pathSegments: [ - ...uri.pathSegments, - '.well-known', - 'openid-configuration' - ]), - ); - - CarpService().configure(app!); + await CarpAuthService().configure(authProperties); + CarpService().configure(app); // register CARP as a data backend where data can be uploaded DataManagerRegistry().register(CarpDataManagerFactory()); @@ -62,12 +78,12 @@ class CarpBackend { /// Authenticate to the CAWS host. Future authenticate() async { - var response = await CarpService().authenticate(); + var user = await CarpAuthService().authenticate(); // Configure the participation service in order to get the invitations CarpParticipationService().configureFrom(CarpService()); - return response; + return user; } /// Get the study invitation. diff --git a/apps/carp_mobile_sensing_app/lib/src/blocs/sensing.dart b/apps/carp_mobile_sensing_app/lib/src/blocs/sensing.dart index af747221..5e2c541d 100644 --- a/apps/carp_mobile_sensing_app/lib/src/blocs/sensing.dart +++ b/apps/carp_mobile_sensing_app/lib/src/blocs/sensing.dart @@ -105,8 +105,8 @@ class Sensing { break; case DeploymentMode.production: - case DeploymentMode.staging: - case DeploymentMode.development: + case DeploymentMode.test: + case DeploymentMode.dev: // Use the CARP deployment service which can download a protocol from CAWS CarpDeploymentService().configureFrom(CarpService()); deploymentService = CarpDeploymentService(); diff --git a/apps/carp_mobile_sensing_app/lib/src/blocs/sensing_bloc.dart b/apps/carp_mobile_sensing_app/lib/src/blocs/sensing_bloc.dart index 8f1dfbb9..f2890fab 100644 --- a/apps/carp_mobile_sensing_app/lib/src/blocs/sensing_bloc.dart +++ b/apps/carp_mobile_sensing_app/lib/src/blocs/sensing_bloc.dart @@ -163,9 +163,9 @@ enum DeploymentMode { /// Use the CAWS production server to get the study deployment and store data. production, - /// Use the CAWS staging server to get the study deployment and store data. - staging, + /// Use the CAWS test server to get the study deployment and store data. + test, /// Use the CAWS development server to get the study deployment and store data. - development, + dev, } diff --git a/apps/carp_mobile_sensing_app/test/CAMS_app_test.dart b/apps/carp_mobile_sensing_app/test/CAMS_app_test.dart index 83e2cc81..ec7bf915 100644 --- a/apps/carp_mobile_sensing_app/test/CAMS_app_test.dart +++ b/apps/carp_mobile_sensing_app/test/CAMS_app_test.dart @@ -45,7 +45,7 @@ void main() { CarpDataManager(); // configure the BLOC w. deployment and data format - bloc.deploymentMode = DeploymentMode.development; + bloc.deploymentMode = DeploymentMode.dev; bloc.dataFormat = NameSpace.CARP; // generate the protocol to be used in testing below diff --git a/apps/carp_mobile_sensing_app/test/caws_app_test.dart b/apps/carp_mobile_sensing_app/test/caws_app_test.dart index 4d961a4c..d27ab054 100644 --- a/apps/carp_mobile_sensing_app/test/caws_app_test.dart +++ b/apps/carp_mobile_sensing_app/test/caws_app_test.dart @@ -1,6 +1,3 @@ -// import 'dart:convert'; -// import 'dart:io'; -// import 'package:test/test.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:carp_serializable/carp_serializable.dart'; @@ -17,15 +14,15 @@ import 'package:carp_apps_package/apps.dart'; import 'package:carp_backend/carp_backend.dart'; import 'package:carp_webservices/carp_auth/carp_auth.dart'; import 'package:carp_webservices/carp_services/carp_services.dart'; +import 'package:shared_preferences/shared_preferences.dart'; -// import '../lib/main.dart'; import 'credentials.dart'; void main() { - TestWidgetsFlutterBinding.ensureInitialized(); + SharedPreferences.setMockInitialValues({}); CarpMobileSensing.ensureInitialized(); - CarpApp app; + // CarpApp app; late CarpUser user; setUp(() async { @@ -46,41 +43,37 @@ void main() { // create a data manager in order to register the json functions CarpDataManager(); - var uri = Uri( + /// The URI of the CAWS server + final uri = Uri( scheme: 'https', - host: 'carp.computerome.dk', - pathSegments: [ - 'auth', - 'dev', - 'realms', - 'Carp', - ], + host: 'dev.carp.dk', ); - // app = CarpApp( - // name: "Test", - // uri: Uri.parse(uri), - // oauth: OAuthEndPoint(clientID: clientID, clientSecret: clientSecret), - // ); + /// The CAWS app configuration. + final app = CarpApp( + name: "CAWS @ DTU", + uri: uri, + studyId: testStudyId, + studyDeploymentId: testDeploymentId, + ); - app = CarpApp( - name: "Test", - uri: uri.replace(pathSegments: ['dev']), + /// The authentication configuration + final authProperties = CarpAuthProperties( authURL: uri, - clientId: 'carp-webservices-dart', + clientId: 'studies-app', redirectURI: Uri.parse('carp-studies-auth://auth'), + // For authentication at CAWS the path is '/auth/realms/Carp' discoveryURL: uri.replace(pathSegments: [ - ...uri.pathSegments, - '.well-known', - 'openid-configuration' + 'auth', + 'realms', + 'Carp', ]), - studyId: testStudyId, - studyDeploymentId: testDeploymentId, ); + await CarpAuthService().configure(authProperties); CarpService().configure(app); - user = await CarpService().authenticateWithUsernamePassword( + user = await CarpAuthService().authenticateWithUsernamePassword( username: username, password: password, );