diff --git a/lib/service/Analytics.dart b/lib/service/Analytics.dart index 34a2a26e..1c57aeac 100644 --- a/lib/service/Analytics.dart +++ b/lib/service/Analytics.dart @@ -621,7 +621,7 @@ class Analytics with Service implements NotificationsListener { Future_sendPacket(String packet) async { if (packet != null) { try { - final response = (Config().loggingUrl != null) ? await Network().post(Config().loggingUrl, body: packet, headers: { "Accept": "application/json", "Content-type":"application/json" }, auth: NetworkAuth.App, sendAnalytics: false) : null; + final response = (Config().loggingUrl != null) ? await Network().post(Config().loggingUrl, body: packet, headers: { "Accept": "application/json", "Content-type":"application/json" }, auth: Network.AppAuth, sendAnalytics: false) : null; return (response != null) && ((response.statusCode == 200) || (response.statusCode == 201)); } catch (e) { diff --git a/lib/service/Auth.dart b/lib/service/Auth.dart index 9954a587..adb6a1b7 100644 --- a/lib/service/Auth.dart +++ b/lib/service/Auth.dart @@ -317,7 +317,7 @@ class Auth with Service implements NotificationsListener { var headers = { "Content-Type": "application/json" }; - final response = await Network().post(url, body: body, headers: headers, auth: NetworkAuth.App); + final response = await Network().post(url, body: body, headers: headers, auth: Network.AppAuth); if (response != null) { return (response.statusCode >= 200 && response.statusCode <= 300); } @@ -417,7 +417,7 @@ class Auth with Service implements NotificationsListener { "Content-Type": "application/json" }; final response = await Network().post( - '${Config().rokwireAuthUrl}/phone-verify', body: phoneVerifyBody, headers: headers, auth: NetworkAuth.App); + '${Config().rokwireAuthUrl}/phone-verify', body: phoneVerifyBody, headers: headers, auth: Network.AppAuth); if ((response != null) && (response.statusCode >= 200 && response.statusCode <= 300)) { Map jsonData = AppJson.decode(response.body); @@ -482,10 +482,6 @@ class Auth with Service implements NotificationsListener { return _rokmetroUser; } - AuthToken get userSignToken { - return _authToken; //RokmetroAuth: _rokmetroToken - } - UserPiiData get userPiiData { return _userPiiData; } @@ -577,7 +573,7 @@ class Auth with Service implements NotificationsListener { PhoneToken phoneToken = (optAuthToken is PhoneToken) ? optAuthToken : null; if ((Config().healthUrl != null) && (phoneToken?.phone != null)) { String url = "${Config().healthUrl}/covid19/rosters/phone/${phoneToken.phone}"; - Http.Response userDataResp = await Network().get(url, auth: NetworkAuth.App); + Http.Response userDataResp = await Network().get(url, auth: Network.AppAuth); if ((userDataResp != null) && (userDataResp.statusCode == 200)) { Map responseJson = AppJson.decodeMap(userDataResp.body); //TMP: return AuthUser(uin: '000000000'); @@ -700,7 +696,7 @@ class Auth with Service implements NotificationsListener { final response = (url != null) ? await Network().put(url, headers: {'Content-Type':'application/json'}, body: body, - auth: NetworkAuth.User + auth: Network.ShibbolethUserAuth ) : null; String responseBody = ((response != null) && (response.statusCode == 200)) ? response.body : null; @@ -725,7 +721,7 @@ class Auth with Service implements NotificationsListener { await Network().delete(url, headers: {'Content-Type':'application/json'}, - auth: NetworkAuth.User + auth: Network.ShibbolethUserAuth ).whenComplete((){ _applyUserPiiData(null, null); }); @@ -972,20 +968,17 @@ class Auth with Service implements NotificationsListener { // Refresh Auth Token - Future refreshUserSignToken() { - return _refreshAuthToken(); //RokmetroAuth: _refreshRokmetroToken(); - } - - Future _refreshAuthToken() async { + Future refreshAuthToken() async { if (isShibbolethLoggedIn) { - await _refreshShibbolethAuthToken(); + return await _refreshShibbolethAuthToken(); } else { // We do not support this currently + return null; } } - Future _refreshShibbolethAuthToken() async { + Future _refreshShibbolethAuthToken() async { if ((_authToken is ShibbolethToken) && (Config().shibbolethOidcTokenUrl != null) && (Config().shibbolethClientId != null) && (Config().shibbolethClientSecret != null)) { if(_refreshTokenFuture != null){ Log.d("Auth: will await refresh token"); @@ -1016,6 +1009,7 @@ class Auth with Service implements NotificationsListener { Log.d("Auth: did refresh token: ${authToken?.idToken}"); Storage().authToken = _authToken = token; NotificationService().notify(notifyAuthTokenChanged); + return token; } } catch(e) { @@ -1024,14 +1018,16 @@ class Auth with Service implements NotificationsListener { } } } + return null; } - /*Future _refreshRokmetroToken() async { + Future refreshRokmetroToken() async { RokmetroToken newRokmetroToken = await _loadRokmetroToken(optAuthToken: _authToken); if (newRokmetroToken?.idToken != null) { Storage().rokmetroToken = _rokmetroToken = newRokmetroToken; } - }*/ + return newRokmetroToken; + } // Deep Links diff --git a/lib/service/Exposure.dart b/lib/service/Exposure.dart index 91bb6589..91fe92b4 100644 --- a/lib/service/Exposure.dart +++ b/lib/service/Exposure.dart @@ -659,7 +659,7 @@ class Exposure with Service implements NotificationsListener { Future reportTEKs(List teks) async { String url = (Config().healthUrl != null) ? "${Config().healthUrl}/covid19/trace/report" : null; String post = (url != null) ? AppJson.encode(ExposureTEK.listToJson(teks)) : null; - Response response = (url != null) ? await Network().post(url, body: post, auth: NetworkAuth.App) : null; + Response response = (url != null) ? await Network().post(url, body: post, auth: Network.AppAuth) : null; return (response?.statusCode == 200); } @@ -683,7 +683,7 @@ class Exposure with Service implements NotificationsListener { url += '?$params'; } - Response response = (url != null) ? await Network().get(url, auth: NetworkAuth.App) : null; + Response response = (url != null) ? await Network().get(url, auth: Network.AppAuth) : null; String responseString = (response?.statusCode == 200) ? response.body : null; List responseJson = (responseString != null) ? AppJson.decodeList(responseString) : null; return (responseJson != null) ? ExposureTEK.listFromJson(responseJson) : null; @@ -1205,7 +1205,7 @@ class Exposure with Service implements NotificationsListener { Response response = await Network().post( Config().exposureLogUrl, body: AppJson.encode(upload), - auth: NetworkAuth.App); + auth: Network.AppAuth); return response?.statusCode == 200; } return null; diff --git a/lib/service/FirebaseMessaging.dart b/lib/service/FirebaseMessaging.dart index f25cfacb..b7076453 100644 --- a/lib/service/FirebaseMessaging.dart +++ b/lib/service/FirebaseMessaging.dart @@ -208,7 +208,7 @@ class FirebaseMessaging with Service implements NotificationsListener { if (Config().sportsServiceUrl != null) { String url = "${Config().sportsServiceUrl}/api/subscribe"; String body = json.encode({'token': _token, 'topic': topic}); - Response response = await Network().post(url, body: body, auth: NetworkAuth.App, headers: { Network.RokwireAppId: Config().appId }); + Response response = await Network().post(url, body: body, auth: Network.AppAuth, headers: { Network.RokwireAppId: Config().appId }); if ((response != null) && (response.statusCode == 200)) { Log.d("FCM: Succesfully subscribed for $topic topic"); Storage().addFirebaseSubscriptionTopic(topic); @@ -237,7 +237,7 @@ class FirebaseMessaging with Service implements NotificationsListener { if (Config().sportsServiceUrl != null) { String url = "${Config().sportsServiceUrl}/api/unsubscribe"; String body = json.encode({'token': _token, 'topic': topic}); - Response response = await Network().post(url, body: body, auth: NetworkAuth.App, headers: { Network.RokwireAppId: Config().appId }); + Response response = await Network().post(url, body: body, auth: Network.AppAuth, headers: { Network.RokwireAppId: Config().appId }); if ((response != null) && (response.statusCode == 200)) { Log.d("FCM: Succesfully unsubscribed from $topic topic"); Storage().removeFirebaseSubscriptionTopic(topic); @@ -257,7 +257,7 @@ class FirebaseMessaging with Service implements NotificationsListener { if (Config().sportsServiceUrl != null) { String url = "${Config().sportsServiceUrl}/api/message"; String body = json.encode({'topic': topic, 'message': message}); - final response = await Network().post(url, timeout: 10, body: body, auth: NetworkAuth.App, headers: { + final response = await Network().post(url, timeout: 10, body: body, auth: Network.AppAuth, headers: { "Accept": "application/json", "content-type": "application/json", Network.RokwireAppId : Config().appId diff --git a/lib/service/FlexUI.dart b/lib/service/FlexUI.dart index 52ddfdd8..03de2454 100644 --- a/lib/service/FlexUI.dart +++ b/lib/service/FlexUI.dart @@ -197,7 +197,7 @@ class FlexUI with Service implements NotificationsListener { try { String body = json.encode(post); _httpClient = httpClient = Http.Client(); - Http.Response response = await Network().get(url, body:body, auth: NetworkAuth.App, client: _httpClient); + Http.Response response = await Network().get(url, body:body, auth: Network.AppAuth, client: _httpClient); int responseCode = response?.statusCode ?? -1; String responseBody = response?.body; Log.d('FlexUI: GET $url\n$body\nResponse $responseCode:\n$responseBody\n'); diff --git a/lib/service/Groups.dart b/lib/service/Groups.dart index 21dbee77..b602bfd4 100644 --- a/lib/service/Groups.dart +++ b/lib/service/Groups.dart @@ -71,7 +71,7 @@ class Groups /* with Service */ { Future> get categories async { String url = '${Config().groupsUrl}/group-categories'; try { - Response response = await Network().get(url, auth: NetworkAuth.App, headers: _apiHeader); + Response response = await Network().get(url, auth: Network.AppAuth, headers: _apiHeader); int responseCode = response?.statusCode ?? -1; String responseBody = response?.body; List categoriesJson = ((response != null) && (responseCode == 200)) ? jsonDecode(responseBody) : null; @@ -90,7 +90,7 @@ class Groups /* with Service */ { Future> loadGroups({bool myGroups = false}) async { String url = myGroups ? '${Config().groupsUrl}/user/groups' : '${Config().groupsUrl}/groups'; try { - Response response = await Network().get(url, auth: myGroups ? NetworkAuth.User : (Auth().isShibbolethLoggedIn) ? NetworkAuth.User : NetworkAuth.App, headers: _apiHeader); + Response response = await Network().get(url, auth: myGroups ? Network.ShibbolethUserAuth : (Auth().isShibbolethLoggedIn) ? Network.ShibbolethUserAuth : Network.AppAuth, headers: _apiHeader); int responseCode = response?.statusCode ?? -1; String responseBody = response?.body; List groupsJson = ((response != null) && (responseCode == 200)) ? jsonDecode(responseBody) : null; @@ -107,7 +107,7 @@ class Groups /* with Service */ { if(AppString.isStringNotEmpty(groupId)) { String url = '${Config().groupsUrl}/groups/$groupId'; try { - Response response = await Network().get(url, auth: Auth().isShibbolethLoggedIn ? NetworkAuth.User : NetworkAuth.App,headers: _apiHeader); + Response response = await Network().get(url, auth: Auth().isShibbolethLoggedIn ? Network.ShibbolethUserAuth : Network.AppAuth,headers: _apiHeader); int responseCode = response?.statusCode ?? -1; String responseBody = response?.body; Map groupsJson = ((response != null) && (responseCode == 200)) ? jsonDecode(responseBody) : null; @@ -128,7 +128,7 @@ class Groups /* with Service */ { json["creator_name"] = Auth()?.authUser?.fullName ?? ""; json["creator_photo_url"] = ""; String body = jsonEncode(json); - Response response = await Network().post(url, auth: NetworkAuth.User, body: body, headers: _apiHeader); + Response response = await Network().post(url, auth: Network.ShibbolethUserAuth, body: body, headers: _apiHeader); int responseCode = response?.statusCode ?? -1; String responseBody = response?.body; Map jsonData = ((response != null) && (responseCode == 200)) ? jsonDecode(responseBody) : null; @@ -150,7 +150,7 @@ class Groups /* with Service */ { try { Map json = group.toJson(); String body = jsonEncode(json); - Response response = await Network().put(url, auth: NetworkAuth.User, body: body, headers: _apiHeader); + Response response = await Network().put(url, auth: Network.ShibbolethUserAuth, body: body, headers: _apiHeader); if((response?.statusCode ?? -1) == 200){ NotificationService().notify(notifyGroupUpdated, group.id); return true; @@ -175,7 +175,7 @@ class Groups /* with Service */ { json["member_answers"] = AppCollection.isCollectionNotEmpty(answers) ? answers.map((e) => e.toJson()).toList() : []; String body = jsonEncode(json); - Response response = await Network().post(url, auth: NetworkAuth.User, body: body, headers: _apiHeader); + Response response = await Network().post(url, auth: Network.ShibbolethUserAuth, body: body, headers: _apiHeader); if((response?.statusCode ?? -1) == 200){ NotificationService().notify(notifyGroupUpdated, group.id); return true; @@ -191,7 +191,7 @@ class Groups /* with Service */ { if(groupId != null) { String url = '${Config().groupsUrl}/group/$groupId/pending-members'; try { - Response response = await Network().delete(url, auth: NetworkAuth.User, headers: _apiHeader); + Response response = await Network().delete(url, auth: Network.ShibbolethUserAuth, headers: _apiHeader); if((response?.statusCode ?? -1) == 200){ NotificationService().notify(notifyGroupUpdated, groupId); return true; @@ -207,7 +207,7 @@ class Groups /* with Service */ { if(groupId != null) { String url = '${Config().groupsUrl}/group/$groupId/members'; try { - Response response = await Network().delete(url, auth: NetworkAuth.User, headers: _apiHeader); + Response response = await Network().delete(url, auth: Network.ShibbolethUserAuth, headers: _apiHeader); if((response?.statusCode ?? -1) == 200){ NotificationService().notify(notifyGroupUpdated, groupId); return true; @@ -225,7 +225,7 @@ class Groups /* with Service */ { String body = jsonEncode(bodyMap); String url = '${Config().groupsUrl}/memberships/$memberId/approval'; try { - Response response = await Network().put(url, auth: NetworkAuth.User, body: body, headers: _apiHeader); + Response response = await Network().put(url, auth: Network.ShibbolethUserAuth, body: body, headers: _apiHeader); if((response?.statusCode ?? -1) == 200){ NotificationService().notify(notifyGroupUpdated, groupId); return true; @@ -243,7 +243,7 @@ class Groups /* with Service */ { String body = jsonEncode(bodyMap); String url = '${Config().groupsUrl}/memberships/$memberId'; try { - Response response = await Network().put(url, auth: NetworkAuth.User, body: body, headers: _apiHeader); + Response response = await Network().put(url, auth: Network.ShibbolethUserAuth, body: body, headers: _apiHeader); if((response?.statusCode ?? -1) == 200){ NotificationService().notify(notifyGroupUpdated, groupId); return true; @@ -259,7 +259,7 @@ class Groups /* with Service */ { if(AppString.isStringNotEmpty(groupId) && AppString.isStringNotEmpty(memberId)) { String url = '${Config().groupsUrl}/memberships/$memberId'; try { - Response response = await Network().delete(url, auth: NetworkAuth.User, headers: _apiHeader); + Response response = await Network().delete(url, auth: Network.ShibbolethUserAuth, headers: _apiHeader); if((response?.statusCode ?? -1) == 200){ NotificationService().notify(notifyGroupUpdated, groupId); return true; @@ -277,7 +277,7 @@ class Groups /* with Service */ { if(AppString.isStringNotEmpty(groupId)) { String url = '${Config().groupsUrl}/group/$groupId/events'; try { - Response response = await Network().get(url, auth: NetworkAuth.User, headers: _apiHeader); + Response response = await Network().get(url, auth: Network.ShibbolethUserAuth, headers: _apiHeader); if((response?.statusCode ?? -1) == 200){ //Successfully loaded ids String responseBody = response?.body; @@ -308,7 +308,7 @@ class Groups /* with Service */ { try { Map bodyMap = {"event_id":eventId}; String body = jsonEncode(bodyMap); - Response response = await Network().post(url, auth: NetworkAuth.User,body: body, headers: _apiHeader); + Response response = await Network().post(url, auth: Network.ShibbolethUserAuth,body: body, headers: _apiHeader); if((response?.statusCode ?? -1) == 200){ NotificationService().notify(notifyGroupUpdated, groupId); return true; @@ -324,7 +324,7 @@ class Groups /* with Service */ { if(AppString.isStringNotEmpty(groupId) && AppString.isStringNotEmpty(eventId)) { String url = '${Config().groupsUrl}/group/$groupId/event/$eventId'; try { - Response response = await Network().delete(url, auth: NetworkAuth.User, headers: _apiHeader); + Response response = await Network().delete(url, auth: Network.ShibbolethUserAuth, headers: _apiHeader); if((response?.statusCode ?? -1) == 200){ NotificationService().notify(notifyGroupUpdated, groupId); return true; diff --git a/lib/service/Health.dart b/lib/service/Health.dart index b83af45a..d4e5766d 100644 --- a/lib/service/Health.dart +++ b/lib/service/Health.dart @@ -348,7 +348,7 @@ class Health with Service implements NotificationsListener { Future _loadUserFromNet() async { if (this._isUserAuthenticated && (Config().healthUrl != null)) { String url = "${Config().healthUrl}/covid19/user"; - Response response = await Network().get(url, auth: NetworkAuth.User); + Response response = await Network().get(url, auth: Network.RokmetroUserAuth); if (response?.statusCode == 200) { HealthUser user = HealthUser.fromJson(AppJson.decodeMap(response.body)); // Return user or null if does not exist for sure. return user; @@ -362,7 +362,7 @@ class Health with Service implements NotificationsListener { if (this._isUserAuthenticated && (Config().healthUrl != null)) { String url = "${Config().healthUrl}/covid19/login"; String post = AppJson.encode(user?.toJson()); - Response response = await Network().post(url, body: post, auth: NetworkAuth.User); + Response response = await Network().post(url, body: post, auth: Network.RokmetroUserAuth); if ((response != null) && (response.statusCode == 200)) { return true; } @@ -373,7 +373,7 @@ class Health with Service implements NotificationsListener { Future _clearUserFromNet() async { if (this._isUserAuthenticated && (Config().healthUrl != null)) { String url = "${Config().healthUrl}/covid19/user/clear"; - Response response = await Network().get(url, auth: NetworkAuth.User); + Response response = await Network().get(url, auth: Network.RokmetroUserAuth); if (response?.statusCode == 200) { _clearUser(); return true; @@ -646,7 +646,7 @@ class Health with Service implements NotificationsListener { Future _loadUserTestMonitorInterval() async { if (this._isUserAuthenticated && (Config().healthUrl != null)) { String url = "${Config().healthUrl}/covid19/uin-override"; - Response response = await Network().get(url, auth: NetworkAuth.HealthUserAccount); + Response response = await Network().get(url, auth: Network.HealthUserAuth); if (response?.statusCode == 200) { Map responseJson = AppJson.decodeMap(response.body); return (responseJson != null) ? responseJson['interval'] : null; @@ -673,7 +673,7 @@ class Health with Service implements NotificationsListener { Future _loadStatusFromNet() async { if (this._isUserReadAuthenticated && (Config().healthUrl != null)) { String url = "${Config().healthUrl}/covid19/v2/app-version/2.2/statuses"; - Response response = await Network().get(url, auth: NetworkAuth.HealthUserAccount); + Response response = await Network().get(url, auth: Network.HealthUserAuth); if (response?.statusCode == 200) { return await HealthStatus.decryptedFromJson(AppJson.decodeMap(response.body), _userPrivateKey); } @@ -686,7 +686,7 @@ class Health with Service implements NotificationsListener { String url = "${Config().healthUrl}/covid19/v2/app-version/2.2/statuses"; HealthStatus encryptedStatus = await status?.encrypted(_user?.publicKey); String post = AppJson.encode(encryptedStatus?.toJson()); - Response response = await Network().put(url, body: post, auth: NetworkAuth.HealthUserAccount); + Response response = await Network().put(url, body: post, auth: Network.HealthUserAuth); if (response?.statusCode == 200) { return true; } @@ -697,7 +697,7 @@ class Health with Service implements NotificationsListener { Future _clearNetStatus() async { if (this._isUserAuthenticated && (Config().healthUrl != null)) { String url = "${Config().healthUrl}/covid19/v2/app-version/2.2/statuses"; - Response response = await Network().delete(url, auth: NetworkAuth.HealthUserAccount); + Response response = await Network().delete(url, auth: Network.HealthUserAuth); if (response?.statusCode == 200) { _saveStatusToStorage(_status = _previousStatus = null); _notify(notifyStatusUpdated); @@ -936,7 +936,7 @@ class Health with Service implements NotificationsListener { Future _clearNetHistory() async { if (this._isUserAuthenticated && (Config().healthUrl != null)) { String url = "${Config().healthUrl}/covid19/v2/histories"; - Response response = await Network().delete(url, auth: NetworkAuth.HealthUserAccount); + Response response = await Network().delete(url, auth: Network.HealthUserAuth); if (response?.statusCode == 200) { _history = []; await _saveHistoryJsonStringToCache(AppJson.encode(HealthHistory.listToJson(_history))); @@ -948,7 +948,7 @@ class Health with Service implements NotificationsListener { Future _loadHistoryJsonStringFromNet() async { String url = (this._isUserReadAuthenticated && (Config().healthUrl != null)) ? "${Config().healthUrl}/covid19/v2/histories" : null; - Response response = (url != null) ? await Network().get(url, auth: NetworkAuth.HealthUserAccount) : null; + Response response = (url != null) ? await Network().get(url, auth: Network.HealthUserAuth) : null; return (response?.statusCode == 200) ? response.body : null; } @@ -956,7 +956,7 @@ class Health with Service implements NotificationsListener { if (this._isUserWriteAuthenticated && (Config().healthUrl != null)) { String url = "${Config().healthUrl}/covid19/v2/histories"; String post = AppJson.encode(history?.toJson()); - Response response = await Network().post(url, body: post, auth: NetworkAuth.HealthUserAccount); + Response response = await Network().post(url, body: post, auth: Network.HealthUserAuth); HealthHistory historyEntry = (response?.statusCode == 200) ? await HealthHistory.decryptedFromJson(AppJson.decode(response.body), _historyPrivateKeys) : null; if ((_history != null) && (historyEntry != null)) { _history.add(historyEntry); @@ -972,7 +972,7 @@ class Health with Service implements NotificationsListener { if (this._isUserWriteAuthenticated && (Config().healthUrl != null)) { String url = "${Config().healthUrl}/covid19/v2/histories/${history.id}"; String post = AppJson.encode(history?.toJson()); - Response response = await Network().put(url, body: post, auth: NetworkAuth.HealthUserAccount); + Response response = await Network().put(url, body: post, auth: Network.HealthUserAuth); HealthHistory historyEntry = (response?.statusCode == 200) ? await HealthHistory.decryptedFromJson(AppJson.decode(response.body), _historyPrivateKeys) : null; if ((_history != null) && (historyEntry != null) && HealthHistory.updateInList(_history, historyEntry)) { HealthHistory.sortListDescending(_history); @@ -1041,7 +1041,7 @@ class Health with Service implements NotificationsListener { if (0 < params.length) { url += "?$params"; } - Response response = await Network().get(url, auth: NetworkAuth.HealthUserAccount); + Response response = await Network().get(url, auth: Network.HealthUserAuth); String responseString = (response?.statusCode == 200) ? response.body : null; List responseJson = (responseString != null) ? AppJson.decodeList(responseString) : null; return (responseJson != null) ? await HealthPendingEvent.listFromJson(responseJson, _userPrivateKey) : null; @@ -1052,7 +1052,7 @@ class Health with Service implements NotificationsListener { Future _markPendingEventAsProcessed(HealthPendingEvent event) async { String url = (this._isUserAuthenticated && Config().healthUrl != null) ? "${Config().healthUrl}/covid19/ctests/${event.id}" : null; String post = AppJson.encode({'processed' : true}); - Response response = (url != null) ? await Network().put(url, body:post, auth: NetworkAuth.HealthUserAccount) : null; + Response response = (url != null) ? await Network().put(url, body:post, auth: Network.HealthUserAuth) : null; if (response?.statusCode == 200) { return true; } @@ -1381,7 +1381,7 @@ class Health with Service implements NotificationsListener { Future> _loadCounties({ bool guidelines }) async { String url = (Config().healthUrl != null) ? "${Config().healthUrl}/covid19/counties" : null; - Response response = (url != null) ? await Network().get(url, auth: NetworkAuth.App) : null; + Response response = (url != null) ? await Network().get(url, auth: Network.AppAuth) : null; String responseBody = (response?.statusCode == 200) ? response.body : null; List responseJson = (responseBody != null) ? AppJson.decodeList(responseBody) : null; return (responseJson != null) ? HealthCounty.listFromJson(responseJson, guidelines: guidelines) : null; @@ -1389,7 +1389,7 @@ class Health with Service implements NotificationsListener { Future _loadCounty({String countyId, bool guidelines }) async { String url = ((countyId != null) && (Config().healthUrl != null)) ? "${Config().healthUrl}/covid19/counties/$countyId" : null; - Response response = (url != null) ? await Network().get(url, auth: NetworkAuth.App) : null; + Response response = (url != null) ? await Network().get(url, auth: Network.AppAuth) : null; String responseBody = (response?.statusCode == 200) ? response.body : null; Map responseJson = (responseBody != null) ? AppJson.decodeMap(responseBody) : null; return (responseJson != null) ? HealthCounty.fromJson(responseJson, guidelines: guidelines) : null; @@ -1467,7 +1467,7 @@ class Health with Service implements NotificationsListener { countyId = countyId ?? _county?.id; String url = ((countyId != null) && (Config().healthUrl != null)) ? "${Config().healthUrl}/covid19/crules/county/$countyId" : null; String appVersion = AppVersion.majorVersion(Config().appVersion, 2); - Response response = (url != null) ? await Network().get(url, auth: NetworkAuth.App, headers: { Network.RokwireAppVersion : appVersion }) : null; + Response response = (url != null) ? await Network().get(url, auth: Network.AppAuth, headers: { Network.RokwireAppVersion : appVersion }) : null; return (response?.statusCode == 200) ? response.body : null; } @@ -1516,7 +1516,7 @@ class Health with Service implements NotificationsListener { Future> _loadBuildingAccessRules({String countyId}) async { countyId = countyId ?? _county?.id; String url = ((countyId != null) && (Config().healthUrl != null)) ? "${Config().healthUrl}/covid19/access-rules/county/$countyId" : null; - Response response = (url != null) ? await Network().get(url, auth: NetworkAuth.App) : null; + Response response = (url != null) ? await Network().get(url, auth: Network.AppAuth) : null; String responseBody = (response?.statusCode == 200) ? response.body : null; return (responseBody != null) ? AppJson.decodeMap(responseBody) : null; } @@ -1549,7 +1549,7 @@ class Health with Service implements NotificationsListener { 'date': healthDateTimeToString(dateUtc), 'access': access }); - Response response = (url != null) ? await Network().put(url, body: post, auth: NetworkAuth.HealthUserAccount) : null; + Response response = (url != null) ? await Network().put(url, body: post, auth: Network.HealthUserAuth) : null; return (response?.statusCode == 200); } return false; @@ -1574,7 +1574,7 @@ class Health with Service implements NotificationsListener { }); url = url.substring(0, url.length - 1); } - Response response = (url != null) ? await Network().get(url, auth: NetworkAuth.App) : null; + Response response = (url != null) ? await Network().get(url, auth: Network.AppAuth) : null; String responseString = (response?.statusCode == 200) ? response.body : null; List responseJson = (responseString != null) ? AppJson.decode(responseString) : null; return (responseJson != null) ? HealthTestType.listFromJson(responseJson) : null; @@ -1589,7 +1589,7 @@ class Health with Service implements NotificationsListener { url += "/county/$countyId"; } - Response response = (url != null) ? await Network().get(url, auth: NetworkAuth.App) : null; + Response response = (url != null) ? await Network().get(url, auth: Network.AppAuth) : null; String responseString = (response?.statusCode == 200) ? response.body : null; List responseJson = (responseString != null) ? AppJson.decode(responseString) : null; return (responseJson != null) ? HealthServiceProvider.listFromJson(responseJson) : null; @@ -1607,7 +1607,7 @@ class Health with Service implements NotificationsListener { url += (countyId != null ? "&" : "?") + "provider-id=$providerId"; } - Response response = (url != null) ? await Network().get(url, auth: NetworkAuth.App) : null; + Response response = (url != null) ? await Network().get(url, auth: Network.AppAuth) : null; String responseString = (response?.statusCode == 200) ? response.body : null; List responseJson = (responseString != null) ? AppJson.decode(responseString) : null; return (responseJson != null) ? HealthServiceLocation.listFromJson(responseJson) : null; @@ -1619,7 +1619,7 @@ class Health with Service implements NotificationsListener { if ((url != null) && (locationId != null)) url += "/$locationId"; - Response response = (url != null) ? await Network().get(url, auth: NetworkAuth.App) : null; + Response response = (url != null) ? await Network().get(url, auth: Network.AppAuth) : null; String responseString = (response?.statusCode == 200) ? response.body : null; Map responseJson = (responseString != null) ? AppJson.decode(responseString) : null; return (responseJson != null) ? HealthServiceLocation.fromJson(responseJson) : null; diff --git a/lib/service/Network.dart b/lib/service/Network.dart index b95ef67f..316f8cb3 100644 --- a/lib/service/Network.dart +++ b/lib/service/Network.dart @@ -20,6 +20,7 @@ import 'dart:typed_data'; import 'package:cookie_jar/cookie_jar.dart'; import 'package:http/http.dart' as Http; +import 'package:illinois/model/Auth.dart'; import 'package:illinois/service/Auth.dart'; import 'package:illinois/service/Connectivity.dart'; import 'package:illinois/service/Analytics.dart'; @@ -30,20 +31,30 @@ import 'package:illinois/utils/Utils.dart'; import 'FirebaseCrashlytics.dart'; -enum NetworkAuth { +/*enum NetworkAuth { App, - User, + AuthUser, + RokmetroUser, HealthUserAccount -} +}*/ class Network { + // Headers static const String RokwireApiKey = 'ROKWIRE-API-KEY'; static const String RokwireHSApiKey = 'ROKWIRE-HS-API-KEY'; static const String RokwireAccountId = 'ROKWIRE-ACC-ID'; static const String RokwireAppId = 'APP'; static const String RokwireAppVersion = 'V'; + // Auth + static const int AppAuth = 1; + static const int ShibbolethUserAuth = 2; + static const int RokmetroUserAuth = 4; + static const int HealthAccountAuth = 8; + + static const int HealthUserAuth = RokmetroUserAuth | HealthAccountAuth; + static final Network _network = new Network._internal(); factory Network() { return _network; @@ -105,7 +116,7 @@ class Network { return null; } - Future _get(url, { String body, Encoding encoding, Map headers, NetworkAuth auth, int timeout, Http.Client client} ) async { + Future _get(url, { String body, Encoding encoding, Map headers, int auth, int timeout, Http.Client client} ) async { if (Connectivity().isNotOffline) { try { if (url != null) { @@ -137,11 +148,11 @@ class Network { return null; } - Future get(url, { String body, Encoding encoding, Map headers, NetworkAuth auth, Http.Client client, int timeout = 60, bool sendAnalytics = true, String analyticsUrl, bool analyticsAnonymous }) async { + Future get(url, { String body, Encoding encoding, Map headers, int auth, Http.Client client, int timeout = 60, bool sendAnalytics = true, String analyticsUrl, bool analyticsAnonymous }) async { Http.Response response = await _get(url, headers: headers, body: body, encoding: encoding, auth: auth, client: client, timeout: timeout); if (_requiresTokenRefresh(response, auth)) { - await Auth().refreshUserSignToken(); + await _refreshToken(auth); response = await _get(url, headers: headers, body: body, encoding: encoding, auth: auth, client: client, timeout: timeout); } @@ -154,7 +165,7 @@ class Network { return response; } - Future _post(url, { body, Encoding encoding, Map headers, NetworkAuth auth, int timeout}) async{ + Future _post(url, { body, Encoding encoding, Map headers, int auth, int timeout}) async{ if (Connectivity().isNotOffline) { try { Future response = (url != null) ? Http.post(url, headers: _prepareHeaders(headers, auth, url), body: body, encoding: encoding) : null; @@ -167,11 +178,11 @@ class Network { return null; } - Future post(url, { body, Encoding encoding, Map headers, NetworkAuth auth, int timeout = 60, bool sendAnalytics = true, String analyticsUrl, bool analyticsAnonymous }) async{ + Future post(url, { body, Encoding encoding, Map headers, int auth, int timeout = 60, bool sendAnalytics = true, String analyticsUrl, bool analyticsAnonymous }) async{ Http.Response response = await _post(url, body: body, encoding: encoding, headers: headers, auth: auth, timeout: timeout); if (_requiresTokenRefresh(response, auth)) { - await Auth().refreshUserSignToken(); + await _refreshToken(auth); response = await _post(url, body: body, encoding: encoding, headers: headers, auth: auth, timeout: timeout); } @@ -184,7 +195,7 @@ class Network { return response; } - Future _put(url, { body, Encoding encoding, Map headers, NetworkAuth auth, int timeout, Http.Client client }) async { + Future _put(url, { body, Encoding encoding, Map headers, int auth, int timeout, Http.Client client }) async { if (Connectivity().isNotOffline) { try { Future response = (url != null) ? @@ -203,11 +214,11 @@ class Network { return null; } - Future put(url, { body, Encoding encoding, Map headers, NetworkAuth auth, int timeout = 60, Http.Client client, bool sendAnalytics = true, String analyticsUrl, bool analyticsAnonymous }) async { + Future put(url, { body, Encoding encoding, Map headers, int auth, int timeout = 60, Http.Client client, bool sendAnalytics = true, String analyticsUrl, bool analyticsAnonymous }) async { Http.Response response = await _put(url, body: body, encoding: encoding, headers: headers, auth: auth, timeout: timeout, client: client); if (_requiresTokenRefresh(response, auth)) { - await Auth().refreshUserSignToken(); + await _refreshToken(auth); response = await _put(url, body: body, encoding: encoding, headers: headers, auth: auth, timeout: timeout, client: client); } @@ -220,7 +231,7 @@ class Network { return response; } - Future _patch(url, { body, Encoding encoding, Map headers, NetworkAuth auth, int timeout }) async { + Future _patch(url, { body, Encoding encoding, Map headers, int auth, int timeout }) async { if (Connectivity().isNotOffline) { try { Future response = (url != null) ? Http.patch(url, headers: _prepareHeaders(headers, auth, url), body: body, encoding: encoding) : null; @@ -233,11 +244,11 @@ class Network { return null; } - Future patch(url, { body, Encoding encoding, Map headers, NetworkAuth auth, int timeout = 60, bool sendAnalytics = true, String analyticsUrl, bool analyticsAnonymous }) async { + Future patch(url, { body, Encoding encoding, Map headers, int auth, int timeout = 60, bool sendAnalytics = true, String analyticsUrl, bool analyticsAnonymous }) async { Http.Response response = await _patch(url, body: body, encoding: encoding, headers: headers, auth: auth, timeout: timeout); if (_requiresTokenRefresh(response, auth)) { - await Auth().refreshUserSignToken(); + await _refreshToken(auth); response = await _patch(url, body: body, encoding: encoding, headers: headers, auth: auth, timeout: timeout); } @@ -250,7 +261,7 @@ class Network { return response; } - Future _delete(url, { Map headers, NetworkAuth auth, int timeout }) async { + Future _delete(url, { Map headers, int auth, int timeout }) async { if (Connectivity().isNotOffline) { try { Future response = (url != null) ? Http.delete(url, headers: _prepareHeaders(headers, auth, url)) : null; @@ -263,11 +274,11 @@ class Network { return null; } - Future delete(url, { Map headers, NetworkAuth auth, int timeout = 60, bool sendAnalytics = true, String analyticsUrl, bool analyticsAnonymous }) async { + Future delete(url, { Map headers, int auth, int timeout = 60, bool sendAnalytics = true, String analyticsUrl, bool analyticsAnonymous }) async { Http.Response response = await _delete(url, headers: headers, auth: auth, timeout: timeout); if (_requiresTokenRefresh(response, auth)) { - await Auth().refreshUserSignToken(); + await _refreshToken(auth); response = await _delete(url, headers: headers, auth: auth, timeout: timeout); } @@ -280,7 +291,7 @@ class Network { return response; } - Future _read(url, { Map headers, NetworkAuth auth, int timeout = 60 }) async { + Future _read(url, { Map headers, int auth, int timeout = 60 }) async { if (Connectivity().isNotOffline) { try { Future response = (url != null) ? Http.read(url, headers: _prepareHeaders(headers, auth, url)) : null; @@ -293,11 +304,11 @@ class Network { return null; } - Future read(url, { Map headers, NetworkAuth auth, int timeout = 60 }) async { + Future read(url, { Map headers, int auth, int timeout = 60 }) async { return _read(url, headers: headers, auth: auth, timeout: timeout); } - Future _readBytes(url, { Map headers, NetworkAuth auth, int timeout = 60 }) async{ + Future _readBytes(url, { Map headers, int auth, int timeout = 60 }) async{ if (Connectivity().isNotOffline) { try { Future response = (url != null) ? Http.readBytes(url, headers: _prepareHeaders(headers, auth, url)) : null; @@ -310,59 +321,89 @@ class Network { return null; } - Future readBytes(url, { Map headers, NetworkAuth auth, int timeout = 60 }) async { + Future readBytes(url, { Map headers, int auth, int timeout = 60 }) async { return _readBytes(url, headers: headers, auth: auth, timeout: timeout); } - Map _prepareHeaders(Map headers, NetworkAuth auth, String url) { + Map _prepareHeaders(Map headers, int auth, String url) { + + Map result; - if (auth == NetworkAuth.App) { - String rokwireApiKey = Config().rokwireApiKey; - if ((rokwireApiKey != null) && rokwireApiKey.isNotEmpty) { - if (headers == null) { - headers = new Map(); + if (auth != null) { + if ((auth & AppAuth) != 0) { + String rokwireApiKey = Config().rokwireApiKey; + if ((rokwireApiKey != null) && rokwireApiKey.isNotEmpty) { + if (result == null) { + result = (headers != null) ? Map.from(headers) : Map(); + } + result[RokwireApiKey] = rokwireApiKey; } - headers[RokwireApiKey] = rokwireApiKey; } - } - - if ((auth == NetworkAuth.User) || (auth == NetworkAuth.HealthUserAccount)) { - String idToken = Auth().userSignToken?.idToken; - String tokenType = Auth().userSignToken?.tokenType ?? 'Bearer'; - if ((idToken != null) && idToken.isNotEmpty) { - if (headers == null) { - headers = new Map(); + + if ((auth & ShibbolethUserAuth) != 0) { + String idToken = Auth().authToken?.idToken; + String tokenType = Auth().authToken?.tokenType ?? 'Bearer'; + if ((idToken != null) && idToken.isNotEmpty) { + if (result == null) { + result = (headers != null) ? Map.from(headers) : Map(); + } + result[HttpHeaders.authorizationHeader] = "$tokenType $idToken"; } - headers[HttpHeaders.authorizationHeader] = "$tokenType $idToken"; } - } - - if (auth == NetworkAuth.HealthUserAccount) { - String rokwireAccountId = Health().userAccountId; - if ((rokwireAccountId != null) && rokwireAccountId.isNotEmpty) { - if (headers == null) { - headers = new Map(); + else if ((auth & RokmetroUserAuth) != 0) { + String idToken = Auth().rokmetroToken?.idToken; + String tokenType = Auth().rokmetroToken?.tokenType ?? 'Bearer'; + if ((idToken != null) && idToken.isNotEmpty) { + if (result == null) { + result = (headers != null) ? Map.from(headers) : Map(); + } + result[HttpHeaders.authorizationHeader] = "$tokenType $idToken"; + } + } + + if ((auth & HealthAccountAuth) != 0) { + String rokwireAccountId = Health().userAccountId; + if ((rokwireAccountId != null) && rokwireAccountId.isNotEmpty) { + if (result == null) { + result = (headers != null) ? Map.from(headers) : Map(); + } + result[RokwireAccountId] = rokwireAccountId; } - headers[RokwireAccountId] = rokwireAccountId; } } + //cookies - String cookies = _loadCookiesForRequest(url); - if (AppString.isStringNotEmpty(cookies)) { - if (headers == null) { - headers = new Map(); + if (url != null) { + String cookies = _loadCookiesForRequest(url); + if (AppString.isStringNotEmpty(cookies)) { + if (result == null) { + result = (headers != null) ? Map.from(headers) : Map(); + } + result["Cookie"] = cookies; } - headers["Cookie"] = cookies; } - return headers; + return (result != null) ? result : headers; + } + + bool _requiresTokenRefresh(Http.Response response, int auth) { + return (response?.statusCode == 401) && (auth != null) && ((auth & (ShibbolethUserAuth | RokmetroUserAuth)) != 0) && (Auth().authToken != null); } - bool _requiresTokenRefresh(Http.Response response, NetworkAuth auth){ - return (response?.statusCode == 401) && (auth == NetworkAuth.User) && Auth().isLoggedIn; + Future _refreshToken(int auth) async { + if (auth != null) { + if ((auth & ShibbolethUserAuth) != 0) { + return await Auth().refreshAuthToken(); + } + else if ((auth & RokmetroUserAuth) != 0) { + return await Auth().refreshRokmetroToken(); + } + } + return null; } + void _saveCookiesFromResponse(String url, Http.Response response) { if (AppString.isStringEmpty(url) || response == null) return; diff --git a/lib/service/TransportationService.dart b/lib/service/TransportationService.dart index 0ab2bb70..20b76bf5 100644 --- a/lib/service/TransportationService.dart +++ b/lib/service/TransportationService.dart @@ -41,7 +41,7 @@ class TransportationService /* with Service */ { 'user_id': userId, 'device_id': deviceId, }); - final response = (url != null) ? await Network().get(url, auth: NetworkAuth.App, body:body) : null; + final response = (url != null) ? await Network().get(url, auth: Network.AppAuth, body:body) : null; String responseBody = response?.body; if ((response != null) && (response.statusCode == 200)) { @@ -64,7 +64,7 @@ class TransportationService /* with Service */ { 'device_id': deviceId, 'ibeacon_data': iBeaconData, }); - final response = (url != null) ? await Network().get(url, auth: NetworkAuth.App, body:body) : null; + final response = (url != null) ? await Network().get(url, auth: Network.AppAuth, body:body) : null; if (response != null) { if (response.statusCode == 200) { String responseBody = response.body; diff --git a/lib/service/UserProfile.dart b/lib/service/UserProfile.dart index 40dae3ac..dca712ea 100644 --- a/lib/service/UserProfile.dart +++ b/lib/service/UserProfile.dart @@ -144,7 +144,7 @@ class UserProfile with Service implements NotificationsListener { String profileUuid = _profileData.uuid; String url = (Config().userProfileUrl != null) ? "${Config().userProfileUrl}/$profileUuid" : null; Map headers = {"Accept": "application/json","content-type":"application/json"}; - final response = (url != null) ? await Network().put(url, body: json.encode(_profileData.toJson()), headers: headers, client: _client, auth: NetworkAuth.App) : null; + final response = (url != null) ? await Network().put(url, body: json.encode(_profileData.toJson()), headers: headers, client: _client, auth: Network.AppAuth) : null; String responseBody = response?.body; bool success = ((response != null) && (responseBody != null) && (response.statusCode == 200)); @@ -171,7 +171,7 @@ class UserProfile with Service implements NotificationsListener { Future requestProfile(String uuid) async { String url = ((Config().userProfileUrl != null) && (uuid != null) && (0 < uuid.length)) ? '${Config().userProfileUrl}/$uuid' : null; - final response = (url != null) ? await Network().get(url, auth: NetworkAuth.App) : null; + final response = (url != null) ? await Network().get(url, auth: Network.AppAuth) : null; if(response != null) { if (response?.statusCode == 404) { @@ -190,7 +190,7 @@ class UserProfile with Service implements NotificationsListener { Future _requestCreateProfile() async { try { - final response = (Config().userProfileUrl != null) ? await Network().post(Config().userProfileUrl, auth: NetworkAuth.App, timeout: 10) : null; + final response = (Config().userProfileUrl != null) ? await Network().post(Config().userProfileUrl, auth: Network.AppAuth, timeout: 10) : null; if ((response != null) && (response.statusCode == 200)) { String responseBody = response.body; Map jsonData = AppJson.decode(responseBody); @@ -208,7 +208,7 @@ class UserProfile with Service implements NotificationsListener { Future deleteProfile() async{ String profileUuid = _profileData?.uuid; if((Config().userProfileUrl != null) && (profileUuid != null)) { - await Network().delete("${Config().userProfileUrl}/$profileUuid", headers: {"Accept": "application/json", "content-type": "application/json"}, auth: NetworkAuth.App); + await Network().delete("${Config().userProfileUrl}/$profileUuid", headers: {"Accept": "application/json", "content-type": "application/json"}, auth: Network.AppAuth); _clearStoredProfile(); _notifyProfileDeleted(); @@ -239,7 +239,7 @@ class UserProfile with Service implements NotificationsListener { String url = "${Config().userProfileUrl}/${_profileData?.uuid}"; Map headers = {"Accept": "application/json","content-type":"application/json"}; String post = json.encode(_profileData.toJson()); - Network().put(url, body: post, headers: headers, auth: NetworkAuth.App); + Network().put(url, body: post, headers: headers, auth: Network.AppAuth); } // 2. We might need to add FCM token and user roles from Storage to new user diff --git a/lib/ui/debug/DebugExposureLogsPanel.dart b/lib/ui/debug/DebugExposureLogsPanel.dart index 9723e140..4ab3dbb3 100644 --- a/lib/ui/debug/DebugExposureLogsPanel.dart +++ b/lib/ui/debug/DebugExposureLogsPanel.dart @@ -1271,7 +1271,7 @@ class _DebugExposureLogsPanelState extends State Map headers = {"Content-type": "application/json"}; Response response = await Network().post(Url + "GetSessionID", - headers: headers, body: null, auth: NetworkAuth.App); + headers: headers, body: null, auth: Network.AppAuth); String body = response.body; if (response.statusCode == HttpStatus.ok) { // update text field @@ -1328,7 +1328,7 @@ class _DebugExposureLogsPanelState extends State '{"isAndroid": $_isAndroid, "deviceID": "$_deviceID", "sessionID":$_currentSession}'; print("" + req); Response response = await Network().post(Url + "CreateSession", - headers: headers, body: req, auth: NetworkAuth.App); + headers: headers, body: req, auth: Network.AppAuth); // statusCode: if (response.statusCode == HttpStatus.ok) { @@ -1365,7 +1365,7 @@ class _DebugExposureLogsPanelState extends State * */ void _onSessionJoin() async { // Response response = await Network() - // .post(Url + "getSessionID", body: post, auth: NetworkAuth.App); + // .post(Url + "getSessionID", body: post, auth: Network.AppAuth); if (_processingSessionID) { return; } @@ -1396,7 +1396,7 @@ class _DebugExposureLogsPanelState extends State '{"isAndroid": $_isAndroid, "deviceID": "$_deviceID", "sessionID":$_currentSession}'; print("" + req); Response response = await Network().post(Url + "JoinSession", - headers: headers, body: req, auth: NetworkAuth.App); + headers: headers, body: req, auth: Network.AppAuth); // statusCode: // if ok, parse js // if not, print the value @@ -1454,7 +1454,7 @@ class _DebugExposureLogsPanelState extends State ' "sessionID":$_currentSession, "additionalDetail":"${_additionalDetailTextController.text}" }'; print("" + req); Response response = await Network().post(Url + "EndSession", - headers: headers, body: req, auth: NetworkAuth.App); + headers: headers, body: req, auth: Network.AppAuth); // update text field setState(() { if (response.statusCode == HttpStatus.ok) {