Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fix: generate token for the user whose password is created not api caller #186

Merged
merged 5 commits into from
Mar 2, 2024
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
2 changes: 1 addition & 1 deletion src/main/kotlin/com/hypto/iam/server/apis/TokenApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ fun Route.tokenApi() {
}
}

authenticate("sub-org-basic-auth") {
authenticate("sub-org-basic-auth", "bearer-auth") {
post("/organizations/{organization_id}/sub_organizations/token") {
generateToken(call, context)
}
Expand Down
22 changes: 15 additions & 7 deletions src/main/kotlin/com/hypto/iam/server/apis/UsersApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,10 @@ fun Route.resetPasswordApi() {
}

fun Route.createUserPasswordApi() {
val gson: Gson by inject()
val usersService: UsersService by inject()
val passcodeRepo: PasscodeRepo by inject()
val tokenService: TokenService by inject()

postWithPermission(
listOf(
Expand Down Expand Up @@ -466,13 +468,19 @@ fun Route.createUserPasswordApi() {
require(passcode.email == inviteeUser.email) { "Email in passcode does not match email in request" }
}

usersService.createUserPassword(
organizationId,
subOrganizationName,
userId,
request.password,
)
val user =
usersService.createUserPassword(
organizationId,
subOrganizationName,
userId,
request.password,
)

generateToken(call, context)
val tokenResponse = tokenService.generateJwtToken(ResourceHrn(user.hrn))
call.respondText(
text = gson.toJson(tokenResponse),
contentType = ContentType.Application.Json,
status = HttpStatusCode.Created,
)
}
}
5 changes: 3 additions & 2 deletions src/main/kotlin/com/hypto/iam/server/service/UsersService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ class UsersServiceImpl : KoinComponent, UsersService {
subOrganizationName: String?,
userId: String,
password: String,
) {
): User {
val user = getUser(organizationId, subOrganizationName, userId)
organizationRepo.findById(organizationId)
?: throw EntityNotFoundException("Invalid organization id")
Expand Down Expand Up @@ -357,6 +357,7 @@ class UsersServiceImpl : KoinComponent, UsersService {
verified = true,
loginAccess = true,
)
return getUser(organizationId, subOrganizationName, userId)
}

override suspend fun listUsers(
Expand Down Expand Up @@ -614,7 +615,7 @@ interface UsersService {
subOrganizationName: String?,
userId: String,
password: String,
)
): User

suspend fun changeUserPassword(
organizationId: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.hypto.iam.server.models.CreateUserRequest
import com.hypto.iam.server.models.CreateUserResponse
import com.hypto.iam.server.models.PolicyAssociationRequest
import com.hypto.iam.server.models.ResetPasswordRequest
import com.hypto.iam.server.models.TokenResponse
import com.hypto.iam.server.models.UpdateUserRequest
import com.hypto.iam.server.models.User
import com.hypto.iam.server.models.UserPaginatedResponse
Expand Down Expand Up @@ -1188,19 +1189,38 @@ class SubOrganizationUserApiTest : AbstractContainerBaseTest() {
header(HttpHeaders.ContentType, ContentType.Application.Json.toString())
setBody(gson.toJson(CreateUserPasswordRequest("testPassword@Hash1")))
}.apply {
assertEquals(HttpStatusCode.OK, status)
assertEquals(HttpStatusCode.Created, status)
assertEquals(
ContentType.Application.Json.withCharset(Charsets.UTF_8),
contentType(),
)
}

assertEquals(HttpStatusCode.OK, response.status)
assertEquals(HttpStatusCode.Created, response.status)
assertEquals(
ContentType.Application.Json.withCharset(Charsets.UTF_8),
response.contentType(),
)

val createPasswordResponse = gson.fromJson(response.bodyAsText(), TokenResponse::class.java)

client.post(
"/organizations/${organization.id}/sub_organizations/token",
) {
header(HttpHeaders.ContentType, ContentType.Application.Json.toString())
header(HttpHeaders.Authorization, "Bearer " + createPasswordResponse.token)
}.apply {
assertEquals(HttpStatusCode.OK, status)
assertEquals(
ContentType.Application.Json.withCharset(Charsets.UTF_8),
contentType(),
)
assertEquals(
organization.id,
headers[Constants.X_ORGANIZATION_HEADER],
)
}

// Check if password is created by logging in
coEvery {
cognitoClient.adminGetUser(any<AdminGetUserRequest>())
Expand Down
Loading