|
| 1 | +package me.snoty.backend.test |
| 2 | + |
| 3 | +import io.ktor.client.* |
| 4 | +import io.ktor.client.call.* |
| 5 | +import io.ktor.client.plugins.contentnegotiation.* |
| 6 | +import io.ktor.client.request.forms.* |
| 7 | +import io.ktor.http.* |
| 8 | +import io.ktor.serialization.kotlinx.json.* |
| 9 | +import kotlinx.serialization.SerialName |
| 10 | +import kotlinx.serialization.Serializable |
| 11 | +import kotlinx.serialization.json.Json |
| 12 | +import me.snoty.backend.config.OidcConfig |
| 13 | +import org.keycloak.admin.client.CreatedResponseUtil |
| 14 | +import org.keycloak.admin.client.resource.RealmResource |
| 15 | +import org.keycloak.admin.client.resource.UsersResource |
| 16 | +import org.keycloak.representations.idm.CredentialRepresentation |
| 17 | +import org.keycloak.representations.idm.UserRepresentation |
| 18 | + |
| 19 | + |
| 20 | +private val httpClient = HttpClient { |
| 21 | + install(ContentNegotiation) { |
| 22 | + json(Json { |
| 23 | + ignoreUnknownKeys = true |
| 24 | + }) |
| 25 | + } |
| 26 | +} |
| 27 | +suspend fun getAccessToken(oidcConfig: OidcConfig, username: String, password: String): String { |
| 28 | + @Serializable |
| 29 | + data class TokenResponse(@SerialName("access_token") val accessToken: String) |
| 30 | + |
| 31 | + return httpClient.submitForm( |
| 32 | + url = "${oidcConfig.oidcUrl}/token", |
| 33 | + formParameters = parameters { |
| 34 | + append("username", username) |
| 35 | + append("password", password) |
| 36 | + append("grant_type", "password") |
| 37 | + append("client_id", oidcConfig.clientId) |
| 38 | + append("client_secret", oidcConfig.clientSecret) |
| 39 | + } |
| 40 | + ).body<TokenResponse>().accessToken |
| 41 | +} |
| 42 | + |
| 43 | +data class UserLogin(val properties: UserRepresentation, val password: String, val accessToken: String) |
| 44 | + |
| 45 | +suspend fun RealmResource.createAndLoginUser( |
| 46 | + oidcConfig: OidcConfig, |
| 47 | + firstName: String = "First", |
| 48 | + lastName: String = "Last", |
| 49 | + username: String = "${firstName.lowercase()}.${lastName.lowercase()}", |
| 50 | + password: String = "12345", |
| 51 | + email: String = " [email protected]" |
| 52 | +) = createAndLoginUser(oidcConfig) { |
| 53 | + this.username = username |
| 54 | + this.firstName = firstName |
| 55 | + this.lastName = lastName |
| 56 | + this.email = email |
| 57 | + CredentialRepresentation().apply { |
| 58 | + isTemporary = false |
| 59 | + type = CredentialRepresentation.PASSWORD |
| 60 | + value = password |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +suspend fun RealmResource.createAndLoginUser( |
| 65 | + oidcConfig: OidcConfig, |
| 66 | + userCustomizer: UserRepresentation.() -> CredentialRepresentation |
| 67 | +): UserLogin { |
| 68 | + val user = UserRepresentation() |
| 69 | + user.isEnabled = true |
| 70 | + val password = user.userCustomizer() |
| 71 | + val usersRessource: UsersResource = users() |
| 72 | + // Create user (requires manage-users role) |
| 73 | + val response = usersRessource.create(user) |
| 74 | + val userId = CreatedResponseUtil.getCreatedId(response) |
| 75 | + |
| 76 | + // Define password credential |
| 77 | + val userResource = usersRessource[userId] |
| 78 | + // Set password credential |
| 79 | + userResource.resetPassword(password) |
| 80 | + |
| 81 | + val accessToken = getAccessToken(oidcConfig, user.username, password.value) |
| 82 | + |
| 83 | + return UserLogin(user, password.value, accessToken) |
| 84 | +} |
0 commit comments