diff --git a/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebClient.kt b/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebClient.kt index f60f87294a93003504e805332313834c32aa81d4..a3453e667b9ba9d78f30b041bbf7ff6149574534 100644 --- a/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebClient.kt +++ b/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebClient.kt @@ -5,6 +5,7 @@ import android.net.Uri import android.util.Log import android.webkit.* import jp.atled.agileworks.model.CertRepository +import jp.atled.agileworks.model.LoginRepository import jp.atled.agileworks.model.api.ApiClient import kotlinx.coroutines.* @@ -63,7 +64,8 @@ class DocumentWebViewClient(private val webShare: DocumentWebShare): WebViewClie val uri = Uri.parse(url) response.body()!!.session_id?.apply { val sessionId = response.body()!!.session_id - setCookie("https://${uri.host}", "JSESSIONID=${sessionId}; Path=/AgileWorks; HttpOnly;Secure;SameSite=None") + setCookie("https://${uri.host}", "JSESSIONID=${sessionId}; Path=/${LoginRepository().loadServerContext()}; HttpOnly;Secure;SameSite=None") + flush() } } } catch (e: Exception) { diff --git a/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebFragment.kt b/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebFragment.kt index b651560200ce422f16f0e6e1da87daafa3a933aa..20fdc4488da2f5ad51e342346a8b803790bdf682 100644 --- a/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebFragment.kt +++ b/app/src/main/java/jp/atled/agileworks/view/ui/documentweb/DocumentWebFragment.kt @@ -20,6 +20,7 @@ import androidx.lifecycle.lifecycleScope import jp.atled.agileworks.R import jp.atled.agileworks.AwApp import jp.atled.agileworks.databinding.FragmentWebviewDetailBinding +import jp.atled.agileworks.model.LoginRepository import jp.atled.agileworks.model.SessionRepository import jp.atled.agileworks.view.base.BaseViewModel import kotlinx.android.synthetic.main.fragment_webview_detail.* @@ -316,7 +317,8 @@ class DocumentWebFragment : Fragment(), DocumentWebPresenter { val uri = Uri.parse(url) response!!.session_id?.apply { sessionId = response!!.session_id - setCookie("https://${uri.host}", "JSESSIONID=${sessionId}; Path=/AgileWorks; HttpOnly;Secure;SameSite=None") + setCookie("https://${uri.host}", "JSESSIONID=${sessionId}; Path=/${LoginRepository().loadServerContext()}; HttpOnly;Secure;SameSite=None") + flush() // セッションID保存 AwApp.instance.applicationContext.getSharedPreferences("cookie", Context.MODE_PRIVATE) .edit() diff --git a/app/src/main/java/jp/atled/agileworks/view/ui/login/LoginFragment.kt b/app/src/main/java/jp/atled/agileworks/view/ui/login/LoginFragment.kt index d77fce4bc1f2a874fb9cc266530c7ca7258ef3d3..9fd004f53c3dc898a3b3b2dbd203ff12db289dd9 100644 --- a/app/src/main/java/jp/atled/agileworks/view/ui/login/LoginFragment.kt +++ b/app/src/main/java/jp/atled/agileworks/view/ui/login/LoginFragment.kt @@ -1,6 +1,7 @@ package jp.atled.agileworks.view.ui.login import android.content.ActivityNotFoundException +import android.content.Context import android.content.Intent import android.os.Bundle import android.os.Handler @@ -8,6 +9,7 @@ import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.webkit.CookieManager import android.widget.* import androidx.annotation.StringRes import androidx.appcompat.app.AlertDialog @@ -35,6 +37,10 @@ import com.journeyapps.barcodescanner.ScanOptions import com.journeyapps.barcodescanner.ScanIntentResult import jp.atled.agileworks.model.* +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.launch class LoginFragment: Fragment() { @@ -79,6 +85,7 @@ class LoginFragment: Fragment() { Log.d(TAG, "authState.isAuthorized == true") Log.d(TAG,"appBackGroundStatus == true") disableLogin() + checkCookie() onSuccessAuth() } else { // 認証済みの場合、生体/デバイス認証を実行 @@ -443,6 +450,60 @@ class LoginFragment: Fragment() { } } + // Cookieにセッション情報がセットされているかを判定 + private fun checkCookie() { + val url = AwApp.Baseurl() + CookieManager.getInstance().apply { + // Cookieの情報を取得 + val getCookieValue = getCookie(url) + // 取得したCookieがnullでなければ;で分割 + val cookieValue = getCookieValue?.split(';') + var jSessionId: String? = null + cookieValue?.forEach { p -> + // 文字列の先頭スペース削除 + val cookieValueTrim = p.trimStart() + // = で文字列分割 + val keyValue = cookieValueTrim.split('=') + // 分割した値がJSESSIONIDなら + if(keyValue[0] == "JSESSIONID") { + // 配列の要素数がが2以上なら + if (keyValue.size >= 2){ + jSessionId = keyValue[1] + } + // ループから抜ける + return@forEach + } + } + // JSESSIONIDがNullもしくは空ならセッションIDをセット + if (jSessionId.isNullOrEmpty()) { + // メインスレッドでHTTP通信をするとエラーになるためスレッドを分けて処理 + val job = SupervisorJob() + val scope = CoroutineScope(Dispatchers.Default + job) + scope.launch { + // セッションID取得、Cookieへセット、内部に保持 + try { + // HTTP通信 + val response = ApiClient.instance.getSession().execute() + if (response.isSuccessful) { + response.body()!!.session_id?.apply { + val sessionId = response.body()!!.session_id + setCookie("https://${LoginRepository().loadServerUrl()}", "JSESSIONID=${sessionId}; Path=/${LoginRepository().loadServerContext()}; HttpOnly;Secure;SameSite=None") + flush() + // セッションID保存 + AwApp.instance.applicationContext.getSharedPreferences("cookie", Context.MODE_PRIVATE) + .edit() + .putString("sessionId", sessionId) + .apply() + } + } + } catch (e: Exception) { + Log.e(TAG, e.toString(), e) + } + } + } + } + } + // 文字列から"https://"を取り除く private fun String.stripScheme(): String = SCHEME_PATTERN.replaceFirst(this, "")