1
0
mirror of https://github.com/etesync/android synced 2024-11-29 11:28:19 +00:00

JournalAuthenticator: add a way to invalidate tokens.

This commit is contained in:
Tom Hacohen 2020-01-23 19:31:18 +02:00
parent 5f30014d7a
commit a04a6ee284
2 changed files with 34 additions and 14 deletions

View File

@ -1,29 +1,21 @@
package com.etesync.syncadapter.journalmanager package com.etesync.syncadapter.journalmanager
import com.etesync.syncadapter.GsonHelper import com.etesync.syncadapter.GsonHelper
import okhttp3.FormBody import okhttp3.*
import okhttp3.HttpUrl
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.IOException import java.io.IOException
import java.net.HttpURLConnection import java.net.HttpURLConnection
class JournalAuthenticator(private val client: OkHttpClient, remote: HttpUrl) { class JournalAuthenticator(private val client: OkHttpClient, private val remote: HttpUrl) {
private val remote: HttpUrl
init {
this.remote = remote.newBuilder()
.addPathSegments("api-token-auth")
.addPathSegment("")
.build()
}
private inner class AuthResponse private constructor() { private inner class AuthResponse private constructor() {
val token: String? = null val token: String? = null
} }
@Throws(Exceptions.HttpException::class, IOException::class) @Throws(Exceptions.HttpException::class, IOException::class)
fun getAuthToken(username: String, password: String): String? { fun getAuthToken(username: String, password: String): String? {
val remote = remote.newBuilder()
.addPathSegments("api-token-auth")
.addPathSegment("")
.build()
val formBuilder = FormBody.Builder() val formBuilder = FormBody.Builder()
.add("username", username) .add("username", username)
.add("password", password) .add("password", password)
@ -42,4 +34,28 @@ class JournalAuthenticator(private val client: OkHttpClient, remote: HttpUrl) {
throw Exceptions.HttpException(response) 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")
}
}
}
} }

View File

@ -39,6 +39,10 @@ class AuthenticatorTest {
val journalAuthenticator = JournalAuthenticator(httpClient!!, remote!!) val journalAuthenticator = JournalAuthenticator(httpClient!!, remote!!)
val authToken = journalAuthenticator.getAuthToken(Helpers.USER, Helpers.PASSWORD) val authToken = journalAuthenticator.getAuthToken(Helpers.USER, Helpers.PASSWORD)
assertNotEquals(authToken!!.length.toLong(), 0) 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) @Test(expected = Exceptions.UnauthorizedException::class)