-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from stslex/dev
init core auth
- Loading branch information
Showing
35 changed files
with
529 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
core/network/src/commonMain/kotlin/com/stslex/core/network/api/base/BaseNetworkClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 0 additions & 7 deletions
7
...etwork/src/commonMain/kotlin/com/stslex/core/network/api/base/DefaultNetworkClientImpl.kt
This file was deleted.
Oops, something went wrong.
21 changes: 0 additions & 21 deletions
21
core/network/src/commonMain/kotlin/com/stslex/core/network/api/server/ServerApi.kt
This file was deleted.
Oops, something went wrong.
96 changes: 96 additions & 0 deletions
96
core/network/src/commonMain/kotlin/com/stslex/core/network/api/server/ServerApiClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package com.stslex.core.network.api.server | ||
|
||
import Wizard.core.network.BuildConfig | ||
import com.stslex.core.core.AppDispatcher | ||
import com.stslex.core.network.api.base.NetworkClient | ||
import com.stslex.core.network.api.base.NetworkClientBuilder.setupLogging | ||
import com.stslex.core.network.api.base.NetworkClientBuilder.setupNegotiation | ||
import com.stslex.core.network.utils.token.AuthController | ||
import com.stslex.core.network.utils.token.toModel | ||
import io.ktor.client.HttpClient | ||
import io.ktor.client.call.body | ||
import io.ktor.client.engine.cio.CIO | ||
import io.ktor.client.plugins.HttpResponseValidator | ||
import io.ktor.client.plugins.ResponseException | ||
import io.ktor.client.plugins.auth.Auth | ||
import io.ktor.client.plugins.auth.providers.BearerTokens | ||
import io.ktor.client.plugins.auth.providers.bearer | ||
import io.ktor.client.plugins.defaultRequest | ||
import io.ktor.client.request.HttpRequest | ||
import io.ktor.client.request.get | ||
import io.ktor.client.request.headers | ||
import io.ktor.http.ContentType | ||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.http.URLProtocol | ||
import io.ktor.http.contentType | ||
import kotlinx.coroutines.withContext | ||
|
||
interface ServerApiClient : NetworkClient | ||
|
||
class ServerApiClientImpl( | ||
private val tokenProvider: AuthController, | ||
private val appDispatcher: AppDispatcher | ||
) : ServerApiClient { | ||
|
||
private val client = HttpClient(CIO) { | ||
setupNegotiation() | ||
|
||
setupLogging() | ||
expectSuccess = true | ||
|
||
install(Auth) { | ||
bearer { | ||
loadTokens { | ||
BearerTokens( | ||
accessToken = tokenProvider.accessToken, | ||
refreshToken = tokenProvider.refreshToken | ||
) | ||
} | ||
} | ||
} | ||
HttpResponseValidator { | ||
handleResponseExceptionWithRequest(errorParser) | ||
} | ||
defaultRequest { | ||
url( | ||
scheme = URLProtocol.HTTP.name, | ||
host = BuildConfig.SERVER_HOST, | ||
port = BuildConfig.SERVER_PORT.toInt(), | ||
path = BuildConfig.SERVER_API_VERSION, | ||
block = { | ||
contentType(ContentType.Application.Json) | ||
} | ||
) | ||
headers { | ||
append(API_KEY_NAME, "API_KEY") // TODO move to buildConfig | ||
} | ||
} | ||
} | ||
|
||
override suspend fun <T> request( | ||
request: suspend HttpClient.() -> T | ||
): T = withContext(appDispatcher.io) { | ||
request(client) | ||
} | ||
|
||
private val errorParser: suspend (Throwable, HttpRequest) -> Unit | ||
get() = { exception, _ -> | ||
val clientException = exception as? ResponseException ?: throw exception | ||
if (HttpStatusCode.Unauthorized.value == clientException.response.status.value) { | ||
refreshToken() | ||
} else { | ||
throw clientException | ||
} | ||
} | ||
|
||
private suspend fun refreshToken() { | ||
val tokenResponse = client | ||
.get("passport/refresh") | ||
.body<TokenResponseModel>() | ||
tokenProvider.update(tokenResponse.toModel()) | ||
} | ||
|
||
companion object { | ||
private const val API_KEY_NAME = "x-api-key" | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
core/network/src/commonMain/kotlin/com/stslex/core/network/api/server/TokenResponseModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.stslex.core.network.api.server | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class TokenResponseModel( | ||
@SerialName("uuid") | ||
val uuid: String, | ||
@SerialName("username") | ||
val username: String, | ||
@SerialName("access_token") | ||
val accessToken: String, | ||
@SerialName("refresh_token") | ||
val refreshToken: String | ||
) |
11 changes: 11 additions & 0 deletions
11
core/network/src/commonMain/kotlin/com/stslex/core/network/clients/auth/client/AuthClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.stslex.core.network.clients.auth.client | ||
|
||
import com.stslex.core.network.clients.auth.response.LoginOkResponse | ||
|
||
interface AuthClient { | ||
|
||
suspend fun authUser(login: String, password: String): LoginOkResponse | ||
|
||
suspend fun registerUser(login: String, username: String, password: String): LoginOkResponse | ||
} | ||
|
55 changes: 55 additions & 0 deletions
55
...twork/src/commonMain/kotlin/com/stslex/core/network/clients/auth/client/AuthClientImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.stslex.core.network.clients.auth.client | ||
|
||
import com.stslex.core.network.api.server.ServerApiClient | ||
import com.stslex.core.network.clients.auth.request.LoginRequest | ||
import com.stslex.core.network.clients.auth.request.RegisterRequest | ||
import com.stslex.core.network.clients.auth.response.LoginOkResponse | ||
import com.stslex.core.network.utils.token.AuthController | ||
import com.stslex.core.network.utils.token.toModel | ||
import io.ktor.client.call.body | ||
import io.ktor.client.request.post | ||
import io.ktor.client.request.setBody | ||
|
||
class AuthClientImpl( | ||
private val networkClient: ServerApiClient, | ||
private val tokenController: AuthController | ||
) : AuthClient { | ||
|
||
override suspend fun authUser( | ||
login: String, | ||
password: String | ||
): LoginOkResponse = networkClient.request { | ||
post("$AUTH_URL/login") { | ||
setBody( | ||
LoginRequest( | ||
login = login, | ||
password = password | ||
) | ||
) | ||
}.body<LoginOkResponse>().also { | ||
tokenController.update(it.toModel()) | ||
} | ||
} | ||
|
||
override suspend fun registerUser( | ||
login: String, | ||
username: String, | ||
password: String | ||
): LoginOkResponse = networkClient.request { | ||
post("$AUTH_URL/register") { | ||
setBody( | ||
RegisterRequest( | ||
login = login, | ||
password = password, | ||
username = username | ||
) | ||
) | ||
}.body<LoginOkResponse>().also { | ||
tokenController.update(it.toModel()) | ||
} | ||
} | ||
|
||
companion object { | ||
private const val AUTH_URL = "passport" | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...etwork/src/commonMain/kotlin/com/stslex/core/network/clients/auth/request/LoginRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.stslex.core.network.clients.auth.request | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class LoginRequest( | ||
@SerialName("login") | ||
val login: String, | ||
@SerialName("password") | ||
val password: String | ||
) |
14 changes: 14 additions & 0 deletions
14
...ork/src/commonMain/kotlin/com/stslex/core/network/clients/auth/request/RegisterRequest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.stslex.core.network.clients.auth.request | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class RegisterRequest( | ||
@SerialName("login") | ||
val login: String, | ||
@SerialName("username") | ||
val username: String, | ||
@SerialName("password") | ||
val password: String | ||
) |
Oops, something went wrong.