Skip to content

Commit

Permalink
Formatting + m in OAuth-related code
Browse files Browse the repository at this point in the history
  • Loading branch information
gagarski committed Dec 7, 2020
1 parent 4462f3d commit 8a9a831
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class OAuthException(message: String) : Exception(message)
abstract class AbstractOAuthProvider(
val name: String,
private val vertx: Vertx,
val callbackBaseUri: String) : Loggable {
private val callbackBaseUri: String) : Loggable {
abstract val providerBaseUri: String

open val authorizePath: String = "/authorize"
Expand All @@ -43,15 +43,14 @@ abstract class AbstractOAuthProvider(
private var providerDbRecord: OauthProviderRecord? = null

private suspend fun getProviderDbRecord() = providerDbRecord ?: run {
val records: JsonArray = vertx.eventBus().
sendJsonableAsync(Address.DB.find(
Tables.OAUTH_PROVIDER.name), OauthProviderRecord().apply {
name = this@AbstractOAuthProvider.name
})
val records: JsonArray = vertx.eventBus().sendJsonableAsync(Address.DB.find(
Tables.OAUTH_PROVIDER.name), OauthProviderRecord().apply {
name = this@AbstractOAuthProvider.name
})

val record = records[0]?.uncheckedCastOrNull<JsonObject>()
return record?.toRecord<OauthProviderRecord>()?.also { providerDbRecord = it } ?:
throw OAuthException("OAuth provider is not present in DB")
return record?.toRecord<OauthProviderRecord>()?.also { providerDbRecord = it }
?: throw OAuthException("OAuth provider is not present in DB")
}

suspend fun getClientId(): String = getProviderDbRecord().clientId
Expand All @@ -70,13 +69,13 @@ abstract class AbstractOAuthProvider(
doGetAuthorizeUriWithQuery().also { codeUri = it }
}

open protected suspend fun doGetAuthorizeUriWithQuery(): String {
protected open suspend fun doGetAuthorizeUriWithQuery(): String {
val query = mapOf(
ClientId to getClientId(),
RedirectUri to redirectUri,
ResponseType to Code,
Scope to scope
).makeUriQuery()
).makeUriQuery()
return authorizeUri + query
}

Expand All @@ -96,24 +95,26 @@ abstract class AbstractOAuthProvider(
.putHeader("${HttpHeaderNames.ACCEPT}", "${HttpHeaderValues.APPLICATION_JSON}")
.basicAuthentication(getClientId(), getClientSecret())
.sendFormAsync(formData)
return resp.bodyAsJsonObject()?.also { accessTokenResponseBody = it } ?: throw OAuthException("Empty access token response")
return resp.bodyAsJsonObject()?.also { accessTokenResponseBody = it }
?: throw OAuthException("Empty access token response")
}

suspend fun getAccessToken(): String = accessToken ?: run {
doGetAccessToken().also { accessToken = it }
}

open protected suspend fun doGetAccessToken(): String {
protected open suspend fun doGetAccessToken(): String {
val json = getAccessTokenResponseBody()

return json.getString(AccessToken) ?: throw OAuthException(json.getString("remoteError") ?: "Unknown OAuth remoteError")
return json.getString(AccessToken) ?: throw OAuthException(json.getString("remoteError")
?: "Unknown OAuth remoteError")
}

suspend fun getUserId() = userId ?: run {
doGetUserId().also { userId = it }
}

abstract protected suspend fun doGetUserId(): String
protected abstract suspend fun doGetUserId(): String


companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Bitbucket(vertx: Vertx, callbackBaseUri: String) : AbstractOAuthProvider(N
private val apiUri = "https://api.bitbucket.org/2.0/"
private val userEndpoint = "/user"

suspend override fun doGetUserId(): String {
override suspend fun doGetUserId(): String {
val resp = webClient.getAbs("$apiUri/$userEndpoint".normalizeUri())
.putHeader("${HttpHeaderNames.AUTHORIZATION}", "Bearer ${getAccessToken()}")
.putHeader("${HttpHeaderNames.ACCEPT}", "${HttpHeaderValues.APPLICATION_JSON}")
Expand All @@ -23,6 +23,6 @@ class Bitbucket(vertx: Vertx, callbackBaseUri: String) : AbstractOAuthProvider(N
}

companion object {
val Name = "Bitbucket"
const val Name = "Bitbucket"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Facebook(vertx: Vertx, callbackBaseUri: String) : AbstractOAuthProvider(Na
}


suspend override fun doGetUserId(): String {
override suspend fun doGetUserId(): String {
val query = mapOf(
"fields" to "id",
AccessToken to getAccessToken()
Expand All @@ -31,6 +31,6 @@ class Facebook(vertx: Vertx, callbackBaseUri: String) : AbstractOAuthProvider(Na
}

companion object {
val Name = "Facebook"
const val Name = "Facebook"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package org.jetbrains.research.kotoed.oauth
import io.netty.handler.codec.http.HttpHeaderNames
import io.netty.handler.codec.http.HttpHeaderValues
import io.vertx.core.Vertx
import org.jetbrains.research.kotoed.util.get
import org.jetbrains.research.kotoed.util.makeUriQuery
import org.jetbrains.research.kotoed.util.normalizeUri
import org.jetbrains.research.kotoed.util.sendAsync

Expand All @@ -24,6 +22,6 @@ class GitHub(vertx: Vertx, callbackBaseUri: String) : AbstractOAuthProvider(Name
}

companion object {
val Name = "GitHub"
const val Name = "GitHub"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.jetbrains.research.kotoed.oauth
import io.netty.handler.codec.http.HttpHeaderNames
import io.netty.handler.codec.http.HttpHeaderValues
import io.vertx.core.Vertx
import org.jetbrains.research.kotoed.util.asMultiMap
import org.jetbrains.research.kotoed.util.makeUriQuery
import org.jetbrains.research.kotoed.util.normalizeUri
import org.jetbrains.research.kotoed.util.sendAsync
Expand All @@ -21,7 +20,7 @@ class Google(vertx: Vertx, callbackBaseUri: String) : AbstractOAuthProvider(Name
"https://www.googleapis.com/auth/userinfo.profile"
}

suspend override fun doGetUserId(): String {
override suspend fun doGetUserId(): String {
val query = mapOf(
AccessToken to getAccessToken(),
"alt" to "json"
Expand All @@ -34,6 +33,6 @@ class Google(vertx: Vertx, callbackBaseUri: String) : AbstractOAuthProvider(Name
}

companion object {
val Name = "Google"
const val Name = "Google"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ class Polytech(vertx: Vertx, callbackBaseUri: String) : AbstractOAuthProvider(Na


companion object {
val Name = "Polytech"
const val Name = "Polytech"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class Vk(vertx: Vertx, callbackBaseUri: String) : AbstractOAuthProvider(Name, ve
override val providerBaseUri: String = "https://oauth.vk.com"

override suspend fun doGetUserId(): String =
getAccessTokenResponseBody().getInteger("user_id")?.toString() ?:
throw OAuthException("Cannot get VK user Id")
getAccessTokenResponseBody().getInteger("user_id")?.toString()
?: throw OAuthException("Cannot get VK user Id")

override suspend fun getAccessTokenResponseBody(): JsonObject = accessTokenResponseBody ?: run {
val formData = mapOf(
Expand All @@ -27,10 +27,11 @@ class Vk(vertx: Vertx, callbackBaseUri: String) : AbstractOAuthProvider(Name, ve
.putHeader("${HttpHeaderNames.CONTENT_TYPE}", "${HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED}")
.putHeader("${HttpHeaderNames.ACCEPT}", "${HttpHeaderValues.APPLICATION_JSON}")
.sendFormAsync(formData)
return resp.bodyAsJsonObject()?.also { accessTokenResponseBody = it } ?: throw OAuthException("Empty access token response")
return resp.bodyAsJsonObject()?.also { accessTokenResponseBody = it }
?: throw OAuthException("Empty access token response")
}

companion object {
val Name = "Vk"
const val Name = "Vk"
}
}

0 comments on commit 8a9a831

Please sign in to comment.