Skip to content

Commit

Permalink
Add OAuth support, continued
Browse files Browse the repository at this point in the history
  • Loading branch information
BoD committed Mar 14, 2021
1 parent a91d718 commit 23be25e
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ import org.jraf.klibqonto.model.attachments.Attachment
import org.jraf.klibqonto.model.dates.DateRange
import org.jraf.klibqonto.model.labels.Label
import org.jraf.klibqonto.model.memberships.Membership
import org.jraf.klibqonto.model.oauth.OAuthCodeAndUniqueState
import org.jraf.klibqonto.model.oauth.OAuthCredentials
import org.jraf.klibqonto.model.oauth.OAuthScope
import org.jraf.klibqonto.model.oauth.OAuthTokens
import org.jraf.klibqonto.model.organizations.Organization
import org.jraf.klibqonto.model.pagination.Page
import org.jraf.klibqonto.model.pagination.Pagination
Expand All @@ -48,6 +52,39 @@ import kotlin.jvm.JvmName
*/
interface BlockingQontoClient {

/**
* See [QontoClient.OAuth].
*/
interface OAuth {
/**
* Get the URI used to login to the Qonto service with your application.
*/
fun getLoginUri(
oAuthCredentials: OAuthCredentials,
scopes: List<OAuthScope> = listOf(
OAuthScope.OFFLINE_ACCESS,
OAuthScope.ORGANIZATION_READ,
OAuthScope.OPENID,
),
uniqueState: String,
): String

/**
* See [QontoClient.OAuth.extractCodeAndUniqueStateFromRedirectUri].
*/
fun extractCodeAndUniqueStateFromRedirectUri(redirectUri: String): OAuthCodeAndUniqueState?

/**
* See [QontoClient.OAuth.getTokens].
*/
fun getTokens(oAuthCredentials: OAuthCredentials, code: String): OAuthTokens

/**
* See [QontoClient.OAuth.refreshTokens].
*/
fun refreshTokens(oAuthCredentials: OAuthCredentials, oAuthTokens: OAuthTokens): OAuthTokens
}

/**
* See [QontoClient.Organizations].
*/
Expand Down Expand Up @@ -118,6 +155,12 @@ interface BlockingQontoClient {
fun getAttachment(id: String): Attachment
}


/**
* See [QontoClient.OAuth].
*/
val oAuth: OAuth

/**
* See [QontoClient.organizations].
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ import org.jraf.klibqonto.model.attachments.Attachment
import org.jraf.klibqonto.model.dates.DateRange
import org.jraf.klibqonto.model.labels.Label
import org.jraf.klibqonto.model.memberships.Membership
import org.jraf.klibqonto.model.oauth.OAuthCodeAndUniqueState
import org.jraf.klibqonto.model.oauth.OAuthCredentials
import org.jraf.klibqonto.model.oauth.OAuthScope
import org.jraf.klibqonto.model.oauth.OAuthTokens
import org.jraf.klibqonto.model.organizations.Organization
import org.jraf.klibqonto.model.pagination.Page
import org.jraf.klibqonto.model.pagination.Pagination
Expand All @@ -49,6 +53,43 @@ import kotlin.jvm.JvmName
*/
interface CallbackQontoClient {

/**
* See [QontoClient.OAuth].
*/
interface OAuth {
/**
* Get the URI used to login to the Qonto service with your application.
*/
fun getLoginUri(
oAuthCredentials: OAuthCredentials,
scopes: List<OAuthScope> = listOf(
OAuthScope.OFFLINE_ACCESS,
OAuthScope.ORGANIZATION_READ,
OAuthScope.OPENID,
),
uniqueState: String,
): String

/**
* See [QontoClient.OAuth.extractCodeAndUniqueStateFromRedirectUri].
*/
fun extractCodeAndUniqueStateFromRedirectUri(redirectUri: String): OAuthCodeAndUniqueState?

/**
* See [QontoClient.OAuth.getTokens].
*/
fun getTokens(oAuthCredentials: OAuthCredentials, code: String, onResult: (Result<OAuthTokens>) -> Unit)

/**
* See [QontoClient.OAuth.refreshTokens].
*/
fun refreshTokens(
oAuthCredentials: OAuthCredentials,
oAuthTokens: OAuthTokens,
onResult: (Result<OAuthTokens>) -> Unit,
)
}

/**
* See [QontoClient.Organizations].
*/
Expand Down Expand Up @@ -125,6 +166,12 @@ interface CallbackQontoClient {
)
}


/**
* See [QontoClient.OAuth].
*/
val oAuth: OAuth

/**
* See [QontoClient.organizations].
*/
Expand All @@ -151,10 +198,7 @@ interface CallbackQontoClient {
val attachments: Attachments

/**
* Dispose of this client instance.
* This will release some resources so it is recommended to call it after use.
*
* **Note: this client will no longer be usable after this is called.**
* See [QontoClient.close].
*/
fun close()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ import org.jraf.klibqonto.model.attachments.Attachment
import org.jraf.klibqonto.model.dates.DateRange
import org.jraf.klibqonto.model.labels.Label
import org.jraf.klibqonto.model.memberships.Membership
import org.jraf.klibqonto.model.oauth.OAuthCodeAndUniqueState
import org.jraf.klibqonto.model.oauth.OAuthCredentials
import org.jraf.klibqonto.model.oauth.OAuthScope
import org.jraf.klibqonto.model.oauth.OAuthTokens
import org.jraf.klibqonto.model.organizations.Organization
import org.jraf.klibqonto.model.pagination.Page
import org.jraf.klibqonto.model.pagination.Pagination
Expand All @@ -39,18 +43,48 @@ import org.jraf.klibqonto.model.transactions.Transaction
internal class BlockingQontoClientImpl(
private val qontoClient: QontoClient,
) : BlockingQontoClient,
BlockingQontoClient.OAuth,
BlockingQontoClient.Organizations,
BlockingQontoClient.Transactions,
BlockingQontoClient.Memberships,
BlockingQontoClient.Labels,
BlockingQontoClient.Attachments {

override val oAuth = this
override val organizations = this
override val transactions = this
override val memberships = this
override val labels = this
override val attachments = this

override fun getLoginUri(
oAuthCredentials: OAuthCredentials,
scopes: List<OAuthScope>,
uniqueState: String,
): String = qontoClient.oAuth.getLoginUri(
oAuthCredentials = oAuthCredentials,
scopes = scopes,
uniqueState = uniqueState,
)

override fun extractCodeAndUniqueStateFromRedirectUri(redirectUri: String): OAuthCodeAndUniqueState? =
qontoClient.oAuth.extractCodeAndUniqueStateFromRedirectUri(redirectUri)

override fun getTokens(oAuthCredentials: OAuthCredentials, code: String): OAuthTokens = runBlocking {
qontoClient.oAuth.getTokens(
oAuthCredentials = oAuthCredentials,
code = code,
)
}

override fun refreshTokens(oAuthCredentials: OAuthCredentials, oAuthTokens: OAuthTokens): OAuthTokens =
runBlocking {
qontoClient.oAuth.refreshTokens(
oAuthCredentials = oAuthCredentials,
oAuthTokens = oAuthTokens,
)
}

override fun getOrganization(): Organization = runBlocking {
qontoClient.organizations.getOrganization()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ import org.jraf.klibqonto.model.attachments.Attachment
import org.jraf.klibqonto.model.dates.DateRange
import org.jraf.klibqonto.model.labels.Label
import org.jraf.klibqonto.model.memberships.Membership
import org.jraf.klibqonto.model.oauth.OAuthCodeAndUniqueState
import org.jraf.klibqonto.model.oauth.OAuthCredentials
import org.jraf.klibqonto.model.oauth.OAuthScope
import org.jraf.klibqonto.model.oauth.OAuthTokens
import org.jraf.klibqonto.model.organizations.Organization
import org.jraf.klibqonto.model.pagination.Page
import org.jraf.klibqonto.model.pagination.Pagination
Expand All @@ -42,18 +46,55 @@ import org.jraf.klibqonto.model.transactions.Transaction
internal class CallbackQontoClientImpl(
private val qontoClient: QontoClient,
) : CallbackQontoClient,
CallbackQontoClient.OAuth,
CallbackQontoClient.Organizations,
CallbackQontoClient.Transactions,
CallbackQontoClient.Memberships,
CallbackQontoClient.Labels,
CallbackQontoClient.Attachments {

override val oAuth = this
override val organizations = this
override val transactions = this
override val memberships = this
override val labels = this
override val attachments = this

override fun getLoginUri(
oAuthCredentials: OAuthCredentials,
scopes: List<OAuthScope>,
uniqueState: String,
): String = qontoClient.oAuth.getLoginUri(
oAuthCredentials = oAuthCredentials,
scopes = scopes,
uniqueState = uniqueState,
)

override fun extractCodeAndUniqueStateFromRedirectUri(redirectUri: String): OAuthCodeAndUniqueState? =
qontoClient.oAuth.extractCodeAndUniqueStateFromRedirectUri(redirectUri)

override fun getTokens(
oAuthCredentials: OAuthCredentials,
code: String,
onResult: (Result<OAuthTokens>) -> Unit,
) = launchAndCallback(onResult) {
qontoClient.oAuth.getTokens(
oAuthCredentials = oAuthCredentials,
code = code,
)
}

override fun refreshTokens(
oAuthCredentials: OAuthCredentials,
oAuthTokens: OAuthTokens,
onResult: (Result<OAuthTokens>) -> Unit,
) = launchAndCallback(onResult) {
qontoClient.oAuth.refreshTokens(
oAuthCredentials = oAuthCredentials,
oAuthTokens = oAuthTokens,
)
}

override fun getOrganization(onResult: (Result<Organization>) -> Unit) = launchAndCallback(onResult) {
qontoClient.organizations.getOrganization()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@
package org.jraf.klibqonto.client.future

import org.jraf.klibqonto.client.QontoClient
import org.jraf.klibqonto.client.blocking.BlockingQontoClient
import org.jraf.klibqonto.client.callback.Result
import org.jraf.klibqonto.internal.client.future.FutureQontoClientImpl
import org.jraf.klibqonto.model.attachments.Attachment
import org.jraf.klibqonto.model.dates.DateRange
import org.jraf.klibqonto.model.labels.Label
import org.jraf.klibqonto.model.memberships.Membership
import org.jraf.klibqonto.model.oauth.OAuthCodeAndUniqueState
import org.jraf.klibqonto.model.oauth.OAuthCredentials
import org.jraf.klibqonto.model.oauth.OAuthScope
import org.jraf.klibqonto.model.oauth.OAuthTokens
import org.jraf.klibqonto.model.organizations.Organization
import org.jraf.klibqonto.model.pagination.Page
import org.jraf.klibqonto.model.pagination.Pagination
Expand All @@ -47,6 +53,39 @@ import java.util.concurrent.Future
*/
interface FutureQontoClient {

/**
* See [QontoClient.OAuth].
*/
interface OAuth {
/**
* Get the URI used to login to the Qonto service with your application.
*/
fun getLoginUri(
oAuthCredentials: OAuthCredentials,
scopes: List<OAuthScope> = listOf(
OAuthScope.OFFLINE_ACCESS,
OAuthScope.ORGANIZATION_READ,
OAuthScope.OPENID,
),
uniqueState: String,
): String

/**
* See [QontoClient.OAuth.extractCodeAndUniqueStateFromRedirectUri].
*/
fun extractCodeAndUniqueStateFromRedirectUri(redirectUri: String): OAuthCodeAndUniqueState?

/**
* See [QontoClient.OAuth.getTokens].
*/
fun getTokens(oAuthCredentials: OAuthCredentials, code: String): Future<OAuthTokens>

/**
* See [QontoClient.OAuth.refreshTokens].
*/
fun refreshTokens(oAuthCredentials: OAuthCredentials, oAuthTokens: OAuthTokens): Future<OAuthTokens>
}

/**
* See [QontoClient.Organizations].
*/
Expand Down Expand Up @@ -116,6 +155,12 @@ interface FutureQontoClient {
fun getAttachment(id: String): Future<Attachment>
}


/**
* See [QontoClient.OAuth].
*/
val oAuth: OAuth

/**
* See [QontoClient.organizations].
*/
Expand Down
Loading

0 comments on commit 23be25e

Please sign in to comment.