-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathOpenSignInAuthenticator.kt
More file actions
61 lines (53 loc) · 2.52 KB
/
OpenSignInAuthenticator.kt
File metadata and controls
61 lines (53 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.commit451.gitlab.api
import android.content.Intent
import android.os.Build
import android.provider.DocumentsContract
import android.widget.Toast
import com.commit451.gitlab.App
import com.commit451.gitlab.R
import com.commit451.gitlab.activity.LoginActivity
import com.commit451.gitlab.data.Prefs
import com.commit451.gitlab.model.Account
import com.commit451.gitlab.providers.FileProvider
import com.commit451.gitlab.util.ThreadUtil
import okhttp3.Authenticator
import okhttp3.Request
import okhttp3.Response
import okhttp3.Route
import timber.log.Timber
import java.io.IOException
/**
* If it detects a 401, redirect the user to the login screen, clearing activity the stack.
* Kinda a weird global way of forcing the user to the login screen if their auth has expired
*/
class OpenSignInAuthenticator(private val account: Account) : Authenticator {
@Throws(IOException::class)
override fun authenticate(route: Route?, response: Response): Request? {
val url = response.request().url()
var cleanUrl = url.toString().toLowerCase()
cleanUrl = cleanUrl.substring(cleanUrl.indexOf(':'))
var cleanServerUrl = account.serverUrl.toString().toLowerCase()
cleanServerUrl = cleanServerUrl.substring(cleanServerUrl.indexOf(':'))
//Ensure that we only check urls of the gitlab instance
if (cleanUrl.startsWith(cleanServerUrl)) {
//Special case for if someone just put in their username or password wrong
if ("session" != url.pathSegments()[url.pathSegments().size - 1]) {
//Off the background thread
Timber.wtf(RuntimeException("Got a 401 and showing sign in for url: " + response.request().url()))
ThreadUtil.postOnMainThread(Runnable {
//Remove the account, so that the user can sign in again
Prefs.removeAccount(account)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
App.get().applicationContext.contentResolver.notifyChange(DocumentsContract.buildRootsUri(FileProvider.getAuthority()), null)
}
Toast.makeText(App.get(), R.string.error_401, Toast.LENGTH_LONG)
.show()
val intent = LoginActivity.newIntent(App.get())
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
App.get().startActivity(intent)
})
}
}
return null
}
}