Skip to content

Commit

Permalink
Format all files with ktlint 1.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
LudvigHz committed Oct 11, 2024
1 parent 66854f7 commit e491686
Show file tree
Hide file tree
Showing 74 changed files with 1,729 additions and 1,262 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[*.{kt,kts}]
ktlint_disabled_rules=no-wildcard-imports,trailing-comma-on-call-site,trailing-comma-on-declaration-site
ktlint_standard_no-wildcard-imports = disabled
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
cache: 'maven'
- uses: nbadal/action-ktlint-setup@v1
with:
ktlint_version: '0.47.1'
ktlint_version: '1.3.1'
- name: Ktlint
run: ktlint
- name: Build with Maven
Expand Down
40 changes: 24 additions & 16 deletions crypto/src/main/kotlin/no/nav/personoversikt/common/crypto/AES.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,46 @@ object AES {
private val algorithm = "AES/GCM/NoPadding"
private val GCM_IV_LENGTH = 16

fun generateKey(password: String, salt: String): SecretKey {
fun generateKey(
password: String,
salt: String,
): SecretKey {
val keyfactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256")
val keyspec = PBEKeySpec(password.toCharArray(), salt.toByteArray(), 65536, 256)
return SecretKeySpec(keyfactory.generateSecret(keyspec).encoded, "AES")
}

fun encrypt(plaintext: ByteArray, key: SecretKey): ByteArray {
fun encrypt(
plaintext: ByteArray,
key: SecretKey,
): ByteArray {
val iv = generateIv()
val ciphertext = Cipher
.getInstance(algorithm)
.apply {
init(Cipher.ENCRYPT_MODE, key, GCMParameterSpec(128, iv.iv))
}
.run {
doFinal(plaintext)
}
return ByteBuffer.allocate(iv.iv.size + ciphertext.size)
val ciphertext =
Cipher
.getInstance(algorithm)
.apply {
init(Cipher.ENCRYPT_MODE, key, GCMParameterSpec(128, iv.iv))
}.run {
doFinal(plaintext)
}
return ByteBuffer
.allocate(iv.iv.size + ciphertext.size)
.put(iv.iv)
.put(ciphertext)
.array()
}

fun decrypt(ciphertext: ByteArray, key: SecretKey): ByteArray {
return Cipher
fun decrypt(
ciphertext: ByteArray,
key: SecretKey,
): ByteArray =
Cipher
.getInstance(algorithm)
.apply {
init(Cipher.DECRYPT_MODE, key, GCMParameterSpec(128, ciphertext, 0, GCM_IV_LENGTH))
}
.run {
}.run {
doFinal(ciphertext, GCM_IV_LENGTH, ciphertext.size - GCM_IV_LENGTH)
}
}

private fun generateIv(): IvParameterSpec {
val bytes = ByteArray(GCM_IV_LENGTH)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package no.nav.personoversikt.common.crypto

class Crypter(secret: String) {
class Crypter(
secret: String,
) {
private val password = secret.substring(secret.length / 2)
private val salt = secret.removePrefix(password)
private val key = AES.generateKey(password, salt)

fun encryptOrThrow(plaintext: String): String = encrypt(plaintext).getOrThrow()

fun decryptOrThrow(ciphertext: String): String = decrypt(ciphertext).getOrThrow()

fun encrypt(plaintext: String): Result<String> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ object Encoding {
private val decoder = Base64.getUrlDecoder()

fun encode(bytes: ByteArray): String = encoder.encodeToString(bytes)

fun decode(string: String): ByteArray = decoder.decode(string)

fun String.isBase64Encoded(): Boolean {
return try {
fun String.isBase64Encoded(): Boolean =
try {
decoder.decode(this)
true
} catch (ex: Throwable) {
false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.MethodSource

internal class CrypterTest {

private val crypter = Crypter("dummy")

companion object {
@JvmStatic
fun plaintext(): List<Arguments> = listOf(
Arguments.of("💩"),
Arguments.of("plaintext"),
Arguments.of("https://github.com/navikt/modiapersonoversikt-api/blob/dev/web/src/test/java/no/nav/modiapersonoversikt/infrastructure/kabac/utils/CombiningAlgorithmTest.kt"),
)
fun plaintext(): List<Arguments> =
listOf(
Arguments.of("💩"),
Arguments.of("plaintext"),
Arguments.of(
"https://github.com/navikt/modiapersonoversikt-api/blob/dev/web/src/test/java/no/nav/modiapersonoversikt/infrastructure/kabac/utils/CombiningAlgorithmTest.kt",
),
)
}

@ParameterizedTest
Expand All @@ -37,11 +39,12 @@ internal class CrypterTest {

@Test
internal fun `should not throw exception on decrypt`() {
val result = assertDoesNotThrow(
ThrowingSupplier {
crypter.decrypt("")
}
)
val result =
assertDoesNotThrow(
ThrowingSupplier {
crypter.decrypt("")
},
)
assertTrue(result.isFailure)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import org.junit.jupiter.api.Test
import java.util.Base64

internal class EncodingTest {

@Test
internal fun `should use urlBase64Encoder`() {
val plaintext = "dummy"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import no.nav.personoversikt.common.kabac.utils.Key

data class AttributeValue<TValue>(
override val key: Key<TValue>,
private val value: TValue
private val value: TValue,
) : Kabac.PolicyInformationPoint<TValue> {
override fun provide(ctx: Kabac.EvaluationContext): TValue = value

companion object {
operator fun <TValue : Any> invoke(
provider: Kabac.PolicyInformationPoint<TValue>,
value: TValue
value: TValue,
) = AttributeValue(provider.key, value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,55 +12,62 @@ interface CombiningAlgorithm {
}
}

private class DecisionOverride(val overrideValue: Decision.Type) : CombiningAlgorithm {
override fun combine(policies: List<Kabac.Policy>) = object : Kabac.Policy {
override val key = Key<Kabac.Policy>("Combinging policies by ${overrideValue.name.lowercase()}-override rule")
private class DecisionOverride(
val overrideValue: Decision.Type,
) : CombiningAlgorithm {
override fun combine(policies: List<Kabac.Policy>) =
object : Kabac.Policy {
override val key = Key<Kabac.Policy>("Combinging policies by ${overrideValue.name.lowercase()}-override rule")

override fun evaluate(ctx: Kabac.EvaluationContext): Decision {
var decision: Decision = Decision.NotApplicable("No applicable policy found")
for (policy in policies) {
ctx.report(policy.key.name).indent()
val policyDecision = policy
.evaluate(ctx)
.also { ctx.report("Decision: $it") }
override fun evaluate(ctx: Kabac.EvaluationContext): Decision {
var decision: Decision = Decision.NotApplicable("No applicable policy found")
for (policy in policies) {
ctx.report(policy.key.name).indent()
val policyDecision =
policy
.evaluate(ctx)
.also { ctx.report("Decision: $it") }

decision = when (policyDecision.type) {
overrideValue -> {
ctx
.unindent()
.report("Last decision matches override value. Stopping policy evaluation.")
return policyDecision
}
Decision.Type.NOT_APPLICABLE -> decision
else -> policyDecision
decision =
when (policyDecision.type) {
overrideValue -> {
ctx
.unindent()
.report("Last decision matches override value. Stopping policy evaluation.")
return policyDecision
}
Decision.Type.NOT_APPLICABLE -> decision
else -> policyDecision
}
ctx.unindent()
}
ctx.unindent()
return decision
}
return decision
}
}
}

private class FirstApplicable : CombiningAlgorithm {
override fun combine(policies: List<Kabac.Policy>) = object : Kabac.Policy {
override val key = Key<Kabac.Policy>("Combinging policies by first-applicable rule")
override fun combine(policies: List<Kabac.Policy>) =
object : Kabac.Policy {
override val key = Key<Kabac.Policy>("Combinging policies by first-applicable rule")

override fun evaluate(ctx: Kabac.EvaluationContext): Decision {
for (policy in policies) {
ctx.report(policy.key.name).indent()
val decision = policy
.evaluate(ctx)
.also { ctx.report("Decision: $it") }
override fun evaluate(ctx: Kabac.EvaluationContext): Decision {
for (policy in policies) {
ctx.report(policy.key.name).indent()
val decision =
policy
.evaluate(ctx)
.also { ctx.report("Decision: $it") }

if (decision.isApplicable()) {
ctx
.unindent()
.report("Last decision was applicable. Stopping policy evaluation.")
return decision
if (decision.isApplicable()) {
ctx
.unindent()
.report("Last decision was applicable. Stopping policy evaluation.")
return decision
}
ctx.unindent()
}
ctx.unindent()
return Decision.NotApplicable("No applicable policy found")
}
return Decision.NotApplicable("No applicable policy found")
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package no.nav.personoversikt.common.kabac

sealed class Decision(var type: Type) {
sealed class Decision(
var type: Type,
) {
interface DenyCause

enum class Type { PERMIT, DENY, NOT_APPLICABLE }

fun isApplicable(): Boolean = when (this) {
is Permit, is Deny -> true
is NotApplicable -> false
}
fun isApplicable(): Boolean =
when (this) {
is Permit, is Deny -> true
is NotApplicable -> false
}

fun withBias(bias: Type): Decision {
if (isApplicable()) {
Expand All @@ -22,6 +25,7 @@ sealed class Decision(var type: Type) {
}

override fun hashCode(): Int = type.hashCode()

override fun equals(other: Any?): Boolean {
if (other is Decision) {
return type == other.type
Expand All @@ -32,10 +36,17 @@ sealed class Decision(var type: Type) {
class Permit : Decision(Type.PERMIT) {
override fun toString() = "Permit"
}
class Deny(val message: String, val cause: DenyCause) : Decision(Type.DENY) {

class Deny(
val message: String,
val cause: DenyCause,
) : Decision(Type.DENY) {
override fun toString() = "Deny($message)"
}
class NotApplicable(val message: String? = null) : Decision(Type.NOT_APPLICABLE) {

class NotApplicable(
val message: String? = null,
) : Decision(Type.NOT_APPLICABLE) {
override fun toString(): String {
val msg = message?.let { "($it)" } ?: ""
return "NotApplicable$msg"
Expand Down
Loading

0 comments on commit e491686

Please sign in to comment.