Skip to content

Commit

Permalink
Added support for both authentication models. Use the new Rokmetro au…
Browse files Browse the repository at this point in the history
…thentication only in Health API calls (#396).
  • Loading branch information
Mihail Varbanov committed Dec 23, 2020
1 parent 352e022 commit cea490c
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 128 deletions.
2 changes: 1 addition & 1 deletion lib/service/Analytics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ class Analytics with Service implements NotificationsListener {
Future<bool>_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) {
Expand Down
32 changes: 14 additions & 18 deletions lib/service/Auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<String, dynamic> jsonData = AppJson.decode(response.body);
Expand Down Expand Up @@ -482,10 +482,6 @@ class Auth with Service implements NotificationsListener {
return _rokmetroUser;
}

AuthToken get userSignToken {
return _authToken; //RokmetroAuth: _rokmetroToken
}

UserPiiData get userPiiData {
return _userPiiData;
}
Expand Down Expand Up @@ -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<String, dynamic> responseJson = AppJson.decodeMap(userDataResp.body);
//TMP: return AuthUser(uin: '000000000');
Expand Down Expand Up @@ -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;
Expand All @@ -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);
});
Expand Down Expand Up @@ -972,20 +968,17 @@ class Auth with Service implements NotificationsListener {

// Refresh Auth Token

Future<void> refreshUserSignToken() {
return _refreshAuthToken(); //RokmetroAuth: _refreshRokmetroToken();
}

Future<void> _refreshAuthToken() async {
Future<AuthToken> refreshAuthToken() async {
if (isShibbolethLoggedIn) {
await _refreshShibbolethAuthToken();
return await _refreshShibbolethAuthToken();
}
else {
// We do not support this currently
return null;
}
}

Future<void> _refreshShibbolethAuthToken() async {
Future<AuthToken> _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");
Expand Down Expand Up @@ -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) {
Expand All @@ -1024,14 +1018,16 @@ class Auth with Service implements NotificationsListener {
}
}
}
return null;
}

/*Future<void> _refreshRokmetroToken() async {
Future<AuthToken> refreshRokmetroToken() async {
RokmetroToken newRokmetroToken = await _loadRokmetroToken(optAuthToken: _authToken);
if (newRokmetroToken?.idToken != null) {
Storage().rokmetroToken = _rokmetroToken = newRokmetroToken;
}
}*/
return newRokmetroToken;
}

// Deep Links

Expand Down
6 changes: 3 additions & 3 deletions lib/service/Exposure.dart
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ class Exposure with Service implements NotificationsListener {
Future<bool> reportTEKs(List<ExposureTEK> 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);
}

Expand All @@ -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<dynamic> responseJson = (responseString != null) ? AppJson.decodeList(responseString) : null;
return (responseJson != null) ? ExposureTEK.listFromJson(responseJson) : null;
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions lib/service/FirebaseMessaging.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/service/FlexUI.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
28 changes: 14 additions & 14 deletions lib/service/Groups.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class Groups /* with Service */ {
Future<List<String>> 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<dynamic> categoriesJson = ((response != null) && (responseCode == 200)) ? jsonDecode(responseBody) : null;
Expand All @@ -90,7 +90,7 @@ class Groups /* with Service */ {
Future<List<Group>> 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<dynamic> groupsJson = ((response != null) && (responseCode == 200)) ? jsonDecode(responseBody) : null;
Expand All @@ -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<String, dynamic> groupsJson = ((response != null) && (responseCode == 200)) ? jsonDecode(responseBody) : null;
Expand All @@ -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<String, dynamic> jsonData = ((response != null) && (responseCode == 200)) ? jsonDecode(responseBody) : null;
Expand All @@ -150,7 +150,7 @@ class Groups /* with Service */ {
try {
Map<String, dynamic> 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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -308,7 +308,7 @@ class Groups /* with Service */ {
try {
Map<String, dynamic> 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;
Expand All @@ -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;
Expand Down
Loading

0 comments on commit cea490c

Please sign in to comment.