From f8511e29ef96567c340f456ee8a39702ace4b9cb Mon Sep 17 00:00:00 2001 From: private-yusuke <30387586+private-yusuke@users.noreply.github.com> Date: Thu, 18 Apr 2024 11:02:11 +0900 Subject: [PATCH 1/3] Bump up `com.squareup.okhttp3:okhttp` to `5.0.0-alpha.13` From `5.0.0-alpha.12`, the support for SameSite attribute of cookies are added to OkHttp. This is mandatory for the feature that imports schedules from TWINS, as it requires the cookie `twinte_session` with the attribute "SameSite=None". see also: https://github.com/square/okhttp/commit/85286c3f8ca110cf53a70a7a8e30d396757b11f7 --- gradle/libs.versions.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 7396302..fccae3f 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -22,7 +22,7 @@ androidx-navigation-ui-ktx = { group = "androidx.navigation", name = "navigation androidx-preference-ktx = { group = "androidx.preference", name = "preference-ktx", version = "1.2.0" } androidx-webkit = { group = "androidx.webkit", name = "webkit", version = "1.7.0" } gms-play-services-auth = { group = "com.google.android.gms", name = "play-services-auth", version = "20.5.0" } -squareup-okhttp3 = { group = "com.squareup.okhttp3", name = "okhttp", version = "4.11.0" } +squareup-okhttp3 = { group = "com.squareup.okhttp3", name = "okhttp", version = "5.0.0-alpha.13" } gms-play-services-oss-licenses = { group = "com.google.android.gms", name = "play-services-oss-licenses", version = "17.0.1" } gson = { group = "com.google.code.gson", name = "gson", version = "2.10.1" } From 66cb051a1c749ea2858a79a5aee337172eed9412 Mon Sep 17 00:00:00 2001 From: private-yusuke <30387586+private-yusuke@users.noreply.github.com> Date: Thu, 18 Apr 2024 11:04:30 +0900 Subject: [PATCH 2/3] Reset cookies obtained before the support of SameSite attribute Previously, the cookies are saved with no respect to the SameSite attribute, resulting in all cookies treated as if they didn't have that attribute at all, which prevented us from sending schedules to Twin:te server at TWINS page. --- .../java/net/twinte/android/MainActivity.kt | 11 ++++++++ .../twinte/android/MainApplicationModule.kt | 7 ++++++ .../ResetCookiesForSameSiteDataStore.kt | 11 ++++++++ .../ResetCookiesForSameSiteDataStoreImpl.kt | 25 +++++++++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 app/src/main/java/net/twinte/android/datastore/resetcookiesforsamesite/ResetCookiesForSameSiteDataStore.kt create mode 100644 app/src/main/java/net/twinte/android/datastore/resetcookiesforsamesite/ResetCookiesForSameSiteDataStoreImpl.kt diff --git a/app/src/main/java/net/twinte/android/MainActivity.kt b/app/src/main/java/net/twinte/android/MainActivity.kt index b08bace..2e92c12 100644 --- a/app/src/main/java/net/twinte/android/MainActivity.kt +++ b/app/src/main/java/net/twinte/android/MainActivity.kt @@ -25,6 +25,7 @@ import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import net.twinte.android.databinding.ActivityMainBinding +import net.twinte.android.datastore.resetcookiesforsamesite.ResetCookiesForSameSiteDataStore import net.twinte.android.datastore.schedule.ScheduleDataStore import net.twinte.android.datastore.schedulenotification.ScheduleNotificationDataStore import net.twinte.android.datastore.user.UserDataStore @@ -48,6 +49,9 @@ class MainActivity : AppCompatActivity(), SubWebViewFragment.Callback { @Inject lateinit var scheduleNotificationDataStore: ScheduleNotificationDataStore + @Inject + lateinit var resetCookiesForSameSiteDataStore: ResetCookiesForSameSiteDataStore + @Inject lateinit var cookieManager: CookieManager @@ -83,6 +87,13 @@ class MainActivity : AppCompatActivity(), SubWebViewFragment.Callback { WebView.setWebContentsDebuggingEnabled(true) } + // ref: https://github.com/twin-te/twinte-android/issues/44 + // TODO: リリースしてから一年経過したら削除する(twinte_session は元々得られてから 1 年後に expire する設定になっている) + if (resetCookiesForSameSiteDataStore.shouldResetCookiesForSameSite) { + cookieManager.removeAllCookies {} + resetCookiesForSameSiteDataStore.shouldResetCookiesForSameSite = false + } + binding.mainWebview.setup() val url = intent.getStringExtra("REGISTERED_COURSE_ID") diff --git a/app/src/main/java/net/twinte/android/MainApplicationModule.kt b/app/src/main/java/net/twinte/android/MainApplicationModule.kt index 2e6bc82..00d5ba6 100644 --- a/app/src/main/java/net/twinte/android/MainApplicationModule.kt +++ b/app/src/main/java/net/twinte/android/MainApplicationModule.kt @@ -9,6 +9,8 @@ import dagger.Provides import dagger.hilt.InstallIn import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.components.SingletonComponent +import net.twinte.android.datastore.resetcookiesforsamesite.ResetCookiesForSameSiteDataStore +import net.twinte.android.datastore.resetcookiesforsamesite.ResetCookiesForSameSiteDataStoreImpl import net.twinte.android.datastore.schedule.ScheduleDataStore import net.twinte.android.datastore.schedule.SharedPreferencesScheduleDataStore import net.twinte.android.datastore.schedulenotification.ScheduleNotificationDataStore @@ -38,6 +40,11 @@ interface MainApplicationModule { scheduleNotificationDataStore: SharedPreferencesScheduleNotificationDataStore, ): ScheduleNotificationDataStore + @Binds + fun bindResetCookiesForSameSiteDataStore( + resetCookiesForSameSiteDataStoreImpl: ResetCookiesForSameSiteDataStoreImpl, + ): ResetCookiesForSameSiteDataStore + companion object { // TODO: リリース版でのみ ProductionServerSettings を inject するように変更する(マルチモジュール化が必要) @Provides diff --git a/app/src/main/java/net/twinte/android/datastore/resetcookiesforsamesite/ResetCookiesForSameSiteDataStore.kt b/app/src/main/java/net/twinte/android/datastore/resetcookiesforsamesite/ResetCookiesForSameSiteDataStore.kt new file mode 100644 index 0000000..23bb43d --- /dev/null +++ b/app/src/main/java/net/twinte/android/datastore/resetcookiesforsamesite/ResetCookiesForSameSiteDataStore.kt @@ -0,0 +1,11 @@ +package net.twinte.android.datastore.resetcookiesforsamesite + +/** + * ref: https://github.com/twin-te/twinte-android/issues/44 + * SameSite 属性が付与されていない twinte_session Cookie があるとき、 + * lTWINS からの時間割インポートに失敗するため、一度 WebView が利用できる Cookie を + * すべて削除し、再度 Twin:te の認証を行わせるために利用される DataStore + */ +interface ResetCookiesForSameSiteDataStore { + var shouldResetCookiesForSameSite: Boolean +} diff --git a/app/src/main/java/net/twinte/android/datastore/resetcookiesforsamesite/ResetCookiesForSameSiteDataStoreImpl.kt b/app/src/main/java/net/twinte/android/datastore/resetcookiesforsamesite/ResetCookiesForSameSiteDataStoreImpl.kt new file mode 100644 index 0000000..ef61fef --- /dev/null +++ b/app/src/main/java/net/twinte/android/datastore/resetcookiesforsamesite/ResetCookiesForSameSiteDataStoreImpl.kt @@ -0,0 +1,25 @@ +package net.twinte.android.datastore.resetcookiesforsamesite + +import android.content.Context +import androidx.core.content.edit +import dagger.hilt.android.qualifiers.ApplicationContext +import javax.inject.Inject + +class ResetCookiesForSameSiteDataStoreImpl @Inject constructor( + @ApplicationContext private val context: Context, +) : ResetCookiesForSameSiteDataStore { + private val preferences = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE) + + override var shouldResetCookiesForSameSite: Boolean + get() = preferences.getBoolean(KEY_RESET_COOKIES_FOR_SAMESITE, true) + set(value) { + preferences.edit { + putBoolean(KEY_RESET_COOKIES_FOR_SAMESITE, value) + } + } + + companion object { + const val FILE_NAME = "reset_cookies_For_samesite" + private const val KEY_RESET_COOKIES_FOR_SAMESITE = "resetCookiesForSameSite" + } +} From 09b00e767f6158249dea9af83bb4e7bdd6ea12fd Mon Sep 17 00:00:00 2001 From: private-yusuke <30387586+private-yusuke@users.noreply.github.com> Date: Thu, 18 Apr 2024 13:06:58 +0900 Subject: [PATCH 3/3] Set `minSdk` to 24 --- app/build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 589f465..5a8058c 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -11,7 +11,7 @@ android { defaultConfig { applicationId = "net.twinte.android" - minSdkVersion(23) + minSdk = 24 targetSdkVersion (31) versionCode = 19 versionName = "2.0.3"