Skip to content

Commit

Permalink
upgrade of demo app to new authentication model
Browse files Browse the repository at this point in the history
  • Loading branch information
bardram committed Jun 10, 2024
1 parent b61b14a commit 02b3ce4
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 62 deletions.
70 changes: 43 additions & 27 deletions apps/carp_mobile_sensing_app/lib/src/blocs/carp_backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<DeploymentMode, String> 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<void> 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());
Expand All @@ -62,12 +78,12 @@ class CarpBackend {

/// Authenticate to the CAWS host.
Future<CarpUser> 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.
Expand Down
4 changes: 2 additions & 2 deletions apps/carp_mobile_sensing_app/lib/src/blocs/sensing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions apps/carp_mobile_sensing_app/lib/src/blocs/sensing_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
2 changes: 1 addition & 1 deletion apps/carp_mobile_sensing_app/test/CAMS_app_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 22 additions & 29 deletions apps/carp_mobile_sensing_app/test/caws_app_test.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 {
Expand All @@ -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,
);
Expand Down

0 comments on commit 02b3ce4

Please sign in to comment.