Skip to content

Commit

Permalink
improve find one mfa account method query
Browse files Browse the repository at this point in the history
  • Loading branch information
mrFlick72 committed Jul 27, 2024
1 parent ad0e766 commit 43488f4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import com.vauthenticator.server.oauth2.clientapp.ClientAppId
import org.springframework.http.ResponseEntity
import org.springframework.http.ResponseEntity.ok
import org.springframework.security.core.Authentication
import org.springframework.web.bind.annotation.*
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RestController

@RestController
class MfaEnrolmentAssociationEndPoint(
Expand Down Expand Up @@ -47,8 +50,6 @@ class MfaEnrolmentAssociationEndPoint(
authentication: Authentication,
@RequestBody enrolling: MfaEnrollmentRequest
): ResponseEntity<String> {
// todo introduce validation on the expected fields 400 in case of error

val ticketId = accountRepository.accountFor(authentication.name)
.map { account ->
mfaMethodsEnrollment.enroll(
Expand All @@ -71,13 +72,6 @@ class MfaEnrolmentAssociationEndPoint(
mfaMethodsEnrolmentAssociation.associate(associationRequest.ticket, associationRequest.code)
}

@DeleteMapping("/api/mfa/enrollment/{enrollmentId}")
fun deleteMfaAssociation(
@PathVariable("enrollmentId") enrollmentId: String,
authentication: Authentication
) {

}
}

data class MfaEnrollmentRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.vauthenticator.server.mfa.domain.MfaAccountMethod
import com.vauthenticator.server.mfa.domain.MfaMethod
import com.vauthenticator.server.mfa.domain.MfaMethod.valueOf
import software.amazon.awssdk.services.dynamodb.DynamoDbClient
import software.amazon.awssdk.services.dynamodb.model.AttributeValue
import software.amazon.awssdk.services.dynamodb.model.PutItemRequest
import software.amazon.awssdk.services.dynamodb.model.QueryRequest
import java.util.*
Expand All @@ -23,27 +24,27 @@ class DynamoMfaAccountMethodsRepository(
userName: String,
mfaMfaMethod: MfaMethod,
mfaChannel: String
): Optional<MfaAccountMethod> =
Optional.ofNullable(findAll(userName).find { it.method == mfaMfaMethod && it.mfaChannel == mfaChannel})
): Optional<MfaAccountMethod> {
return Optional.ofNullable(
getFromDynamo(userName, mfaChannel)
.map { MfaAccountMethodMapper.fromDynamoToDomain(userName, it) }
.find { it.method == mfaMfaMethod }
)
}


override fun findAll(userName: String): List<MfaAccountMethod> =
getFromDynamo(userName).map {
MfaAccountMethod(
userName,
Kid(it.valueAsStringFor("key_id")),
valueOf(it.valueAsStringFor("mfa_method")),
it.valueAsStringFor("mfa_channel"),
it.valueAsBoolFor("associated")
)
}
getFromDynamo(userName)
.map { MfaAccountMethodMapper.fromDynamoToDomain(userName, it) }

private fun getFromDynamo(email: String) = dynamoDbClient.query(
QueryRequest.builder().tableName(tableName).keyConditionExpression("user_name=:email")
.expressionAttributeValues(mapOf(":email" to email.asDynamoAttribute())).build()
).items()
private fun getFromDynamo(email: String, mfaChannel : String) = dynamoDbClient.query(
QueryRequest.builder().tableName(tableName).keyConditionExpression("user_name=:email AND mfa_channel=:mfaChannel")

private fun getFromDynamo(email: String, mfaChannel: String) = dynamoDbClient.query(
QueryRequest.builder().tableName(tableName)
.keyConditionExpression("user_name=:email AND mfa_channel=:mfaChannel")
.expressionAttributeValues(
mapOf(
":email" to email.asDynamoAttribute(),
Expand All @@ -65,7 +66,7 @@ class DynamoMfaAccountMethodsRepository(
}

private fun storeOnDynamo(
userName: String, mfaMfaMethod: MfaMethod, mfaChannel: String, kid: Kid, associated : Boolean
userName: String, mfaMfaMethod: MfaMethod, mfaChannel: String, kid: Kid, associated: Boolean
) {
dynamoDbClient.putItem(
PutItemRequest.builder().tableName(tableName).item(
Expand All @@ -81,4 +82,19 @@ class DynamoMfaAccountMethodsRepository(
)
}

}


object MfaAccountMethodMapper {
fun fromDynamoToDomain(
userName: String,
item: MutableMap<String, AttributeValue>
): MfaAccountMethod =
MfaAccountMethod(
userName,
Kid(item.valueAsStringFor("key_id")),
valueOf(item.valueAsStringFor("mfa_method")),
item.valueAsStringFor("mfa_channel"),
item.valueAsBoolFor("associated")
)
}

0 comments on commit 43488f4

Please sign in to comment.