Skip to content

Commit d4429ca

Browse files
committed
Fix: Django session handling
1 parent 46a03cc commit d4429ca

5 files changed

Lines changed: 110 additions & 3 deletions

File tree

app/src/main/java/org/openimis/imispolicies/Global.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.json.JSONArray;
4747
import org.json.JSONException;
4848
import org.json.JSONObject;
49+
import org.openimis.imispolicies.network.util.PersistentCookieJar;
4950
import org.openimis.imispolicies.repository.LoginRepository;
5051
import org.openimis.imispolicies.tools.Log;
5152
import org.openimis.imispolicies.util.StreamUtils;
@@ -99,6 +100,7 @@ public class Global extends Application {
99100
private String AppDirectory;
100101
private Map<String,
101102
String> SubDirectories;
103+
private PersistentCookieJar cookieJar;
102104
private volatile LoginRepository loginRepository;
103105
public static Global getGlobal() {
104106
return GlobalContext;
@@ -116,6 +118,13 @@ public void onCreate() {
116118
initSharedPrefsInts();
117119
}
118120

121+
public void setCookieJar(PersistentCookieJar jar) {
122+
this.cookieJar = jar;
123+
}
124+
125+
public PersistentCookieJar getCookieJar() {
126+
return cookieJar;
127+
}
119128
protected boolean isRunningTest() {
120129
try {
121130
Class.forName("org.robolectric.RobolectricTestRunner");

app/src/main/java/org/openimis/imispolicies/network/okhttp/AuthorizationInterceptor.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import androidx.annotation.NonNull;
44

55
import org.apache.commons.lang3.StringUtils;
6+
import org.openimis.imispolicies.Global;
67
import org.openimis.imispolicies.MainActivity;
78
import org.openimis.imispolicies.repository.LoginRepository;
89

@@ -12,6 +13,7 @@
1213
import okhttp3.Interceptor;
1314
import okhttp3.Request;
1415
import okhttp3.Response;
16+
import okhttp3.ResponseBody;
1517

1618
public class AuthorizationInterceptor implements Interceptor {
1719
private static final String USER_AGENT = "mobile_app";
@@ -38,8 +40,14 @@ public Response intercept(@NonNull Chain chain) throws IOException {
3840
builder.addHeader("X-Csrftoken", csrfToken);
3941
}
4042
Response response = chain.proceed(builder.build());
41-
if (response.code() == HttpURLConnection.HTTP_UNAUTHORIZED) {
43+
ResponseBody body = response.peekBody(Long.MAX_VALUE);
44+
String bodyString = body.string();
45+
if (bodyString.contains("'csrftoken'") || response.code() == HttpURLConnection.HTTP_UNAUTHORIZED) {
4246
repository.saveFhirToken(null, null, null);
47+
repository.saveCsrfToken(null);
48+
if (Global.getGlobal().getCookieJar() != null) {
49+
Global.getGlobal().getCookieJar().clear();
50+
}
4351
MainActivity.SetLoggedIn();
4452
}
4553
return response;

app/src/main/java/org/openimis/imispolicies/network/util/OkHttpUtils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package org.openimis.imispolicies.network.util;
22

3+
import static android.content.Context.MODE_PRIVATE;
4+
5+
import static org.openimis.imispolicies.Global.PREF_NAME;
6+
37
import android.annotation.SuppressLint;
8+
import android.content.SharedPreferences;
49

510
import androidx.annotation.NonNull;
611

@@ -30,6 +35,10 @@ public static OkHttpClient getDefaultOkHttpClient() {
3035
synchronized (OkHttpUtils.class) {
3136
if (client == null) {
3237
OkHttpClient.Builder builder = new OkHttpClient.Builder();
38+
PersistentCookieJar cookieJar =
39+
new PersistentCookieJar(Global.getGlobal().getSharedPreferences(PREF_NAME, MODE_PRIVATE));
40+
Global.getGlobal().setCookieJar(cookieJar);
41+
builder.cookieJar(cookieJar);
3342
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
3443
interceptor.setLevel(BuildConfig.DEBUG ? HttpLoggingInterceptor.Level.BODY : HttpLoggingInterceptor.Level.BASIC);
3544
builder.addInterceptor(interceptor);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.openimis.imispolicies.network.util;
2+
3+
import android.content.SharedPreferences;
4+
5+
import androidx.annotation.NonNull;
6+
7+
import okhttp3.Cookie;
8+
import okhttp3.CookieJar;
9+
import okhttp3.HttpUrl;
10+
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
14+
public class PersistentCookieJar implements CookieJar {
15+
16+
private final SharedPreferences prefs;
17+
18+
public PersistentCookieJar(SharedPreferences prefs) {
19+
this.prefs = prefs;
20+
}
21+
22+
@Override
23+
public void saveFromResponse(@NonNull HttpUrl url, List<Cookie> cookies) {
24+
25+
for (Cookie cookie : cookies) {
26+
if ("openimis_session".equals(cookie.name())) {
27+
28+
prefs.edit()
29+
.putString("session_value", cookie.value())
30+
.putLong("session_expiry", cookie.expiresAt())
31+
.apply();
32+
}
33+
}
34+
}
35+
36+
@NonNull
37+
@Override
38+
public List<Cookie> loadForRequest(HttpUrl url) {
39+
40+
String value = prefs.getString("session_value", null);
41+
long expiry = prefs.getLong("session_expiry", -1);
42+
43+
if (value == null || expiry == -1) {
44+
return new ArrayList<>();
45+
}
46+
47+
Cookie cookie = new Cookie.Builder()
48+
.name("openimis_session")
49+
.value(value)
50+
.domain(url.host())
51+
.path("/")
52+
.expiresAt(expiry)
53+
.build();
54+
55+
List<Cookie> cookies = new ArrayList<>();
56+
cookies.add(cookie);
57+
return cookies;
58+
}
59+
60+
public void clear() {
61+
prefs.edit().clear().apply();
62+
}
63+
}

app/src/main/java/org/openimis/imispolicies/repository/LoginRepository.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.openimis.imispolicies.repository;
22

3+
import static android.content.Context.MODE_PRIVATE;
4+
import static org.openimis.imispolicies.Global.PREF_NAME;
5+
36
import android.content.Context;
47
import android.content.SharedPreferences;
58
import android.util.Base64;
@@ -13,6 +16,7 @@
1316
import org.openimis.imispolicies.BuildConfig;
1417
import org.openimis.imispolicies.Global;
1518
import org.openimis.imispolicies.Token;
19+
import org.openimis.imispolicies.network.util.PersistentCookieJar;
1620
import org.openimis.imispolicies.tools.Log;
1721

1822
import java.util.Date;
@@ -37,7 +41,7 @@ public LoginRepository(@NonNull Context context) {
3741

3842
public LoginRepository(@NonNull Context context, boolean isPaymentEnabled) {
3943
this.isPaymentEnabled = isPaymentEnabled;
40-
prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
44+
prefs = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
4145
if (!prefs.getBoolean(HAS_MIGRATED, false)) {
4246
migrateOldTokens();
4347
}
@@ -167,7 +171,21 @@ public boolean isLoggedIn() {
167171
if (isPaymentEnabled && getRestToken() == null) {
168172
return false;
169173
}
170-
return getFhirToken() != null;
174+
175+
PersistentCookieJar cookieJar = Global.getGlobal().getCookieJar();
176+
long expiry = Global.getGlobal().getSharedPreferences(PREF_NAME, MODE_PRIVATE)
177+
.getLong("session_expiry", 0);
178+
179+
boolean isLoggedIn = getFhirToken() != null
180+
&& expiry > System.currentTimeMillis();
181+
182+
if (!isLoggedIn) {
183+
if (cookieJar != null) {
184+
cookieJar.clear();
185+
}
186+
}
187+
188+
return isLoggedIn;
171189
}
172190

173191
public void logout() {

0 commit comments

Comments
 (0)