Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package co.nilin.opex.auth.proxy
import co.nilin.opex.auth.config.KeycloakConfig
import co.nilin.opex.auth.data.ActiveSession
import co.nilin.opex.auth.model.*
import co.nilin.opex.auth.utils.generateRandomID
import co.nilin.opex.common.OpexError
import co.nilin.opex.common.utils.LoggerDelegate
import kotlinx.coroutines.reactive.awaitFirstOrElse
Expand All @@ -15,7 +16,10 @@ import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.stereotype.Service
import org.springframework.web.reactive.function.client.*
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.web.reactive.function.client.awaitBodilessEntity
import org.springframework.web.reactive.function.client.awaitBody
import org.springframework.web.reactive.function.client.bodyToMono

@Service
class KeycloakProxy(
Expand Down Expand Up @@ -152,14 +156,14 @@ class KeycloakProxy(
) {
val keycloakUrl = "${keycloakConfig.url}/admin/realms/${keycloakConfig.realm}/users"
val token = getAdminAccessToken()

val internalID = generateRandomInternalID()
val response = keycloakClient.post()
.uri(keycloakUrl)
.header("Content-Type", "application/json")
.withAdminToken(token)
.bodyValue(
hashMapOf(
"username" to username.value,
"username" to internalID,
"emailVerified" to enabled,
"firstName" to firstName,
"lastName" to lastName,
Expand Down Expand Up @@ -358,4 +362,16 @@ class KeycloakProxy(
return this
}

private suspend fun generateRandomInternalID(): String {
var internalId: String;
var attempts = 0
do {
if (attempts >= 10) {
throw OpexError.InternalIdGenerateFailed.exception()
}
internalId = generateRandomID()
attempts++
} while (findUserByAttribute(Attribute("username", internalId)).isNotEmpty())
return internalId
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package co.nilin.opex.auth.utils

fun generateRandomID(length: Int = 8): String {
val charset = ('0'..'9') + ('a'..'z')
return (1..length)
.map { charset.random() }
.joinToString("")
}

2 changes: 2 additions & 0 deletions common/src/main/kotlin/co/nilin/opex/common/OpexError.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ enum class OpexError(val code: Int, val message: String?, val status: HttpStatus
InvalidRegisterToken(5016, "Invalid register token", HttpStatus.BAD_REQUEST),
ExpiredOTP(5017, "OTP is expired", HttpStatus.BAD_REQUEST),
InvalidToken(5018, "Invalid token", HttpStatus.BAD_REQUEST),
InternalIdGenerateFailed(5019, "Internal id generate failed", HttpStatus.INTERNAL_SERVER_ERROR),



// code 6000: wallet
Expand Down
Loading