-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* M8l2 auth * M8l2 auth
- Loading branch information
Showing
66 changed files
with
7,101 additions
and
71 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
84 changes: 84 additions & 0 deletions
84
ok-marketplace-be/ok-marketplace-app-common/src/commonMain/kotlin/JwtHelper.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,84 @@ | ||
package ru.otus.otuskotlin.marketplace.app.common | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.Json | ||
import ru.otus.otuskotlin.marketplace.common.models.MkplUserId | ||
import ru.otus.otuskotlin.marketplace.common.permissions.MkplPrincipalModel | ||
import ru.otus.otuskotlin.marketplace.common.permissions.MkplUserGroups | ||
import kotlin.io.encoding.Base64 | ||
import kotlin.io.encoding.ExperimentalEncodingApi | ||
|
||
const val AUTH_HEADER: String = "x-jwt-payload" | ||
|
||
@OptIn(ExperimentalEncodingApi::class) | ||
fun String?.jwt2principal(): MkplPrincipalModel = this?.let { jwtHeader -> | ||
val jwtJson = Base64.decode(jwtHeader).decodeToString() | ||
println("JWT JSON PAYLOAD: $jwtJson") | ||
val jwtObj = jsMapper.decodeFromString(JwtPayload.serializer(), jwtJson) | ||
jwtObj.toPrincipal() | ||
} | ||
?: run { | ||
println("No jwt found in headers") | ||
MkplPrincipalModel.NONE | ||
} | ||
|
||
@OptIn(ExperimentalEncodingApi::class) | ||
fun MkplPrincipalModel.createJwtTestHeader(): String { | ||
val jwtObj = fromPrincipal() | ||
val jwtJson = jsMapper.encodeToString(JwtPayload.serializer(), jwtObj) | ||
return Base64.encode(jwtJson.encodeToByteArray()) | ||
} | ||
|
||
private val jsMapper = Json { | ||
ignoreUnknownKeys = true | ||
} | ||
|
||
@Serializable | ||
private data class JwtPayload( | ||
val aud: List<String>? = null, | ||
val sub: String? = null, | ||
@SerialName("family_name") | ||
val familyName: String? = null, | ||
@SerialName("given_name") | ||
val givenName: String? = null, | ||
@SerialName("middle_name") | ||
val middleName: String? = null, | ||
val groups: List<String>? = null, | ||
) | ||
|
||
private fun JwtPayload.toPrincipal(): MkplPrincipalModel = MkplPrincipalModel( | ||
id = sub?.let { MkplUserId(it) } ?: MkplUserId.NONE, | ||
fname = givenName ?: "", | ||
mname = middleName ?: "", | ||
lname = familyName ?: "", | ||
groups = groups?.mapNotNull { it.toPrincipalGroup() }?.toSet() ?: emptySet(), | ||
) | ||
|
||
private fun MkplPrincipalModel.fromPrincipal(): JwtPayload = JwtPayload( | ||
sub = id.takeIf { it != MkplUserId.NONE }?.asString(), | ||
givenName = fname.takeIf { it.isNotBlank() }, | ||
middleName = mname.takeIf { it.isNotBlank() }, | ||
familyName = lname.takeIf { it.isNotBlank() }, | ||
groups = groups.mapNotNull { it.fromPrincipalGroup() }.toList().takeIf { it.isNotEmpty() } ?: emptyList(), | ||
) | ||
|
||
private fun String?.toPrincipalGroup(): MkplUserGroups? = when (this?.uppercase()) { | ||
"USER" -> MkplUserGroups.USER | ||
"ADMIN_AD" -> MkplUserGroups.ADMIN_AD | ||
"MODERATOR_MP" -> MkplUserGroups.MODERATOR_MP | ||
"TEST" -> MkplUserGroups.TEST | ||
"BAN_AD" -> MkplUserGroups.BAN_AD | ||
// TODO сделать обработку ошибок | ||
else -> null | ||
} | ||
|
||
private fun MkplUserGroups?.fromPrincipalGroup(): String? = when (this) { | ||
MkplUserGroups.USER -> "USER" | ||
MkplUserGroups.ADMIN_AD -> "ADMIN_AD" | ||
MkplUserGroups.MODERATOR_MP -> "MODERATOR_MP" | ||
MkplUserGroups.TEST -> "TEST" | ||
MkplUserGroups.BAN_AD -> "BAN_AD" | ||
// TODO сделать обработку ошибок | ||
else -> null | ||
} |
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
47 changes: 47 additions & 0 deletions
47
ok-marketplace-be/ok-marketplace-app-ktor/src/commonTest/kotlin/auth/AuthTest.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,47 @@ | ||
package ru.otus.otuskotlin.marketplace.app.ktor.auth | ||
|
||
import io.ktor.client.call.* | ||
import io.ktor.client.plugins.contentnegotiation.* | ||
import io.ktor.client.request.* | ||
import io.ktor.http.* | ||
import io.ktor.serialization.kotlinx.json.* | ||
import io.ktor.server.testing.* | ||
import ru.otus.otuskotlin.marketplace.api.v2.apiV2Mapper | ||
import ru.otus.otuskotlin.marketplace.api.v2.models.* | ||
import ru.otus.otuskotlin.marketplace.app.ktor.MkplAppSettings | ||
import ru.otus.otuskotlin.marketplace.app.ktor.module | ||
import ru.otus.otuskotlin.marketplace.common.MkplCorSettings | ||
import ru.otus.otuskotlin.marketplace.repo.inmemory.AdRepoInMemory | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class AuthTest { | ||
@Test | ||
fun invalidAudience() = testApplication { | ||
val client = createClient { | ||
install(ContentNegotiation) { | ||
json(apiV2Mapper) | ||
} | ||
} | ||
application { module(MkplAppSettings(corSettings = MkplCorSettings(repoTest = AdRepoInMemory()))) } | ||
val response = client.post("/v2/ad/create") { | ||
addAuth(groups = emptyList()) | ||
contentType(ContentType.Application.Json) | ||
setBody( | ||
AdCreateRequest( | ||
ad = AdCreateObject( | ||
title = "xxsdgff", | ||
description = "dfgdfg", | ||
adType = DealSide.SUPPLY, | ||
visibility = AdVisibility.PUBLIC, | ||
), | ||
debug = AdDebug(mode = AdRequestDebugMode.TEST) | ||
) | ||
) | ||
} | ||
val adObj = response.body<AdCreateResponse>() | ||
assertEquals(200, response.status.value) | ||
assertEquals(ResponseResult.ERROR, adObj.result) | ||
assertEquals("access-create", adObj.errors?.first()?.code) | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
ok-marketplace-be/ok-marketplace-app-ktor/src/commonTest/kotlin/auth/addAuth.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,20 @@ | ||
package ru.otus.otuskotlin.marketplace.app.ktor.auth | ||
|
||
import io.ktor.client.request.* | ||
import ru.otus.otuskotlin.marketplace.app.common.AUTH_HEADER | ||
import ru.otus.otuskotlin.marketplace.app.common.createJwtTestHeader | ||
import ru.otus.otuskotlin.marketplace.common.models.MkplUserId | ||
import ru.otus.otuskotlin.marketplace.common.permissions.MkplPrincipalModel | ||
import ru.otus.otuskotlin.marketplace.common.permissions.MkplUserGroups | ||
import ru.otus.otuskotlin.marketplace.stubs.MkplAdStubBolts.AD_DEMAND_BOLT1 | ||
|
||
fun HttpRequestBuilder.addAuth(principal: MkplPrincipalModel) { | ||
header(AUTH_HEADER, principal.createJwtTestHeader()) | ||
} | ||
|
||
fun HttpRequestBuilder.addAuth( | ||
id: MkplUserId = AD_DEMAND_BOLT1.ownerId, | ||
groups: Collection<MkplUserGroups> = listOf(MkplUserGroups.TEST, MkplUserGroups.USER), | ||
) { | ||
addAuth(MkplPrincipalModel(id, groups = groups.toSet())) | ||
} |
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
Oops, something went wrong.