mirror of
https://github.com/etesync/android
synced 2024-11-25 17:38:13 +00:00
JournalAuthenticator: add a way to invalidate tokens.
This commit is contained in:
parent
5f30014d7a
commit
a04a6ee284
@ -1,29 +1,21 @@
|
||||
package com.etesync.syncadapter.journalmanager
|
||||
|
||||
import com.etesync.syncadapter.GsonHelper
|
||||
import okhttp3.FormBody
|
||||
import okhttp3.HttpUrl
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.*
|
||||
import java.io.IOException
|
||||
import java.net.HttpURLConnection
|
||||
|
||||
class JournalAuthenticator(private val client: OkHttpClient, remote: HttpUrl) {
|
||||
private val remote: HttpUrl
|
||||
|
||||
init {
|
||||
this.remote = remote.newBuilder()
|
||||
.addPathSegments("api-token-auth")
|
||||
.addPathSegment("")
|
||||
.build()
|
||||
}
|
||||
|
||||
class JournalAuthenticator(private val client: OkHttpClient, private val remote: HttpUrl) {
|
||||
private inner class AuthResponse private constructor() {
|
||||
val token: String? = null
|
||||
}
|
||||
|
||||
@Throws(Exceptions.HttpException::class, IOException::class)
|
||||
fun getAuthToken(username: String, password: String): String? {
|
||||
val remote = remote.newBuilder()
|
||||
.addPathSegments("api-token-auth")
|
||||
.addPathSegment("")
|
||||
.build()
|
||||
val formBuilder = FormBody.Builder()
|
||||
.add("username", username)
|
||||
.add("password", password)
|
||||
@ -42,4 +34,28 @@ class JournalAuthenticator(private val client: OkHttpClient, remote: HttpUrl) {
|
||||
throw Exceptions.HttpException(response)
|
||||
}
|
||||
}
|
||||
|
||||
fun invalidateAuthToken(authToken: String) {
|
||||
val remote = remote.newBuilder()
|
||||
.addPathSegments("api/logout")
|
||||
.addPathSegment("")
|
||||
.build()
|
||||
|
||||
val body = RequestBody.create(null, byteArrayOf())
|
||||
val request = Request.Builder()
|
||||
.post(body)
|
||||
.url(remote)
|
||||
.build()
|
||||
|
||||
val response = client.newCall(request).execute()
|
||||
if (response.isSuccessful) {
|
||||
return
|
||||
} else {
|
||||
when (response.code()) {
|
||||
HttpURLConnection.HTTP_BAD_GATEWAY -> throw Exceptions.BadGatewayException(response, "Bad gateway: most likely a server restart")
|
||||
HttpURLConnection.HTTP_UNAVAILABLE -> throw Exceptions.ServiceUnavailableException(response, "Service unavailable")
|
||||
HttpURLConnection.HTTP_UNAUTHORIZED -> throw Exceptions.UnauthorizedException(response, "Unauthorized auth token")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -39,6 +39,10 @@ class AuthenticatorTest {
|
||||
val journalAuthenticator = JournalAuthenticator(httpClient!!, remote!!)
|
||||
val authToken = journalAuthenticator.getAuthToken(Helpers.USER, Helpers.PASSWORD)
|
||||
assertNotEquals(authToken!!.length.toLong(), 0)
|
||||
|
||||
val httpClient2 = HttpClient.Builder(null, null, authToken).build().okHttpClient
|
||||
val journalAuthenticator2 = JournalAuthenticator(httpClient2!!, remote!!)
|
||||
journalAuthenticator2.invalidateAuthToken(authToken)
|
||||
}
|
||||
|
||||
@Test(expected = Exceptions.UnauthorizedException::class)
|
||||
|
Loading…
Reference in New Issue
Block a user