Skip to content

Commit

Permalink
Fix getAccessToken() race condition.
Browse files Browse the repository at this point in the history
  • Loading branch information
meiron03 committed Oct 14, 2023
1 parent e685436 commit 0a110a5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import eightbitlab.com.blurview.RenderScriptBlur
import kotlinx.android.synthetic.main.custom_sneaker_view.view.*
import kotlinx.android.synthetic.main.fragment_dining_holder.*
import kotlinx.android.synthetic.main.include_main.*
import kotlinx.coroutines.sync.Mutex
import retrofit.RestAdapter
import retrofit.android.AndroidLog
import retrofit.client.OkClient
Expand All @@ -60,6 +61,8 @@ class MainActivity : AppCompatActivity() {
private lateinit var fragmentManager: FragmentManager
private lateinit var mSharedPrefs: SharedPreferences

val tokenMutex = Mutex()

override fun onCreate(savedInstanceState: Bundle?) {
setTheme(R.style.AppTheme)
if (Build.VERSION.SDK_INT > 28) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ package com.pennapps.labs.pennmobile.api
import android.preference.PreferenceManager
import android.provider.Settings
import android.util.Log
import androidx.lifecycle.lifecycleScope
import com.pennapps.labs.pennmobile.BuildConfig
import com.pennapps.labs.pennmobile.MainActivity
import com.pennapps.labs.pennmobile.R
import com.pennapps.labs.pennmobile.classes.AccessTokenResponse
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.withLock
import retrofit.Callback
import retrofit.RetrofitError
import retrofit.client.Response
Expand All @@ -25,24 +31,33 @@ class OAuth2NetworkManager(private var mActivity: MainActivity) {

@Synchronized
fun getAccessToken(function: () -> Unit) {
val expiresIn = sp.getString(mActivity.getString(R.string.expires_in), "")
if (expiresIn != "") {
val expiresAt = sp.getLong(mActivity.getString(R.string.token_expires_at), 0)
val currentTime = Calendar.getInstance().timeInMillis
if (currentTime >= expiresAt) { // if it has expired, refresh access token
Log.i("Accounts", "Expired")
refreshAccessToken(function)
mActivity.lifecycleScope.launch {
val tokenMutex = mActivity.tokenMutex
tokenMutex.lock()
val expiresIn = sp.getString(mActivity.getString(R.string.expires_in), "")
if (expiresIn != "") {
val expiresAt = sp.getLong(mActivity.getString(R.string.token_expires_at), 0)
val currentTime = Calendar.getInstance().timeInMillis
if (currentTime >= expiresAt) { // if it has expired, refresh access token
Log.i("Accounts", "Expired")
refreshAccessToken (function) {
tokenMutex.unlock()
}
} else {
Log.i("Accounts", "Not Expired")
tokenMutex.unlock()
function.invoke()
}
} else {
Log.i("Accounts", "Not Expired")
function.invoke()
refreshAccessToken (function) {
tokenMutex.unlock()
}
}
} else {
refreshAccessToken(function)
}
}

@Synchronized
private fun refreshAccessToken(function: () -> Unit) {
private fun refreshAccessToken(function: () -> Unit, unlockMutex: () -> Unit) {
val refreshToken = sp.getString(mActivity.getString(R.string.refresh_token), "")
val clientID = BuildConfig.PLATFORM_CLIENT_ID

Expand All @@ -61,6 +76,7 @@ class OAuth2NetworkManager(private var mActivity: MainActivity) {
val currentTime = Calendar.getInstance().timeInMillis
editor.putLong(mActivity.getString(R.string.token_expires_at), currentTime + expiresInInt)
editor.apply()
unlockMutex.invoke()
function.invoke()
Log.i("Accounts", "Reloaded Homepage")
}
Expand All @@ -72,9 +88,8 @@ class OAuth2NetworkManager(private var mActivity: MainActivity) {

if (error.response != null && error.response.status == 400) {
mActivity.startLoginFragment()
unlockMutex.invoke()
}

function.invoke()
}
})
}
Expand Down

0 comments on commit 0a110a5

Please sign in to comment.