Fix SQWOTFormatter

This commit is contained in:
Paul Schaub 2023-07-17 18:07:30 +02:00
parent dd1bbcd47f
commit edca8574af
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
2 changed files with 42 additions and 19 deletions

View File

@ -4,6 +4,7 @@
package org.pgpainless.wot.cli.format package org.pgpainless.wot.cli.format
import org.pgpainless.wot.api.AuthenticationLevel
import org.pgpainless.wot.api.Binding import org.pgpainless.wot.api.Binding
import org.pgpainless.wot.network.EdgeComponent import org.pgpainless.wot.network.EdgeComponent
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
@ -17,20 +18,22 @@ class SQWOTFormatter: Formatter {
*/ */
override fun format(binding: Binding, amountMin: Int, amountReference: Int): String { override fun format(binding: Binding, amountMin: Int, amountReference: Int): String {
val percentage = binding.percentage(amountReference) val percentage = binding.percentage(amountReference)
val authLevel = when(binding.paths.amount) { val bAmount = binding.paths.amount
in 0..39 -> "not authenticated" val authLevel = if (bAmount >= AuthenticationLevel.Doubly.amount) "doubly"
in 40..119 -> "partially authenticated" else if (bAmount >= AuthenticationLevel.Fully.amount) "fully"
in 120 .. 239 -> "fully authenticated" else if (bAmount >= AuthenticationLevel.Partially.amount) "partially"
else -> {if (percentage < 0) "not authenticated" else "doubly authenticated"} else if (bAmount > 0) "marginally"
} else "not"
val checkmark = if(binding.paths.amount >= amountMin) "[✓] " else "[ ] " val checkmark = if(binding.paths.amount >= amountMin) "[✓] " else "[ ] "
val pathList = binding.paths.paths val pathList = binding.paths.paths
val singlePath = pathList.size == 1 val singlePath = pathList.size == 1
val indent = " ".repeat(if (singlePath) 2 else 4) val indent = " ".repeat(if (singlePath) 2 else 4)
return buildString { return buildString {
// [✓] 7F9116FEA90A5983936C7CFAA027DB2F3E1E118A Paul Schaub <vanitasvitae@fsfe.org>: fully authenticated (100%) // [✓] 7F9116FEA90A5983936C7CFAA027DB2F3E1E118A Paul Schaub <vanitasvitae@fsfe.org>: fully authenticated (100%)
append(checkmark); appendLine("${binding.fingerprint} ${binding.userId}: $authLevel (${percentage}%)") append(checkmark); appendLine("${binding.fingerprint} ${binding.userId}: $authLevel authenticated (${percentage}%)")
for ((pIndex, path) in pathList.withIndex()) { for ((pIndex, path) in pathList.withIndex()) {
if (!singlePath) { if (!singlePath) {
appendLine(" Path #${pIndex + 1} of ${pathList.size}, trust amount ${path.amount}:") appendLine(" Path #${pIndex + 1} of ${pathList.size}, trust amount ${path.amount}:")
@ -41,11 +44,18 @@ class SQWOTFormatter: Formatter {
" \"${path.root.userIds.keys.first()}\"" " \"${path.root.userIds.keys.first()}\""
else else
" (\"${path.root.userIds.keys.first()}\")" " (\"${path.root.userIds.keys.first()}\")"
// ◯ AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA ("Alice")
append(indent); appendLine("${path.root.fingerprint}$originUserId") append(indent); appendLine("${path.root.fingerprint}$originUserId")
for ((eIndex, edge) in path.certifications.withIndex()) { for ((eIndex, edge) in path.certifications.withIndex()) {
val targetUserId = if (edge.userId == null) "" else " \"${edge.userId}\"" val targetUserId = if (edge.userId == null)
append(indent); appendLine("${certDegree(edge.trustAmount)}the following " + ""
(if (edge.userId != null) "binding" else "certificate") + else if (eIndex == path.certifications.lastIndex)
" \"${edge.userId}\""
else
" (\"${edge.userId}\")"
append(indent); appendLine("${certDegree(edge.trustAmount)} the following " +
(if (eIndex == path.certifications.lastIndex) "binding" else "certificate") +
" on ${dateFormat.format(edge.creationTime)}" + " on ${dateFormat.format(edge.creationTime)}" +
(if (edge.expirationTime == null) "" else " (expiry: ${dateFormat.format(edge.expirationTime)})") + (if (edge.expirationTime == null) "" else " (expiry: ${dateFormat.format(edge.expirationTime)})") +
introducerType(edge) introducerType(edge)
@ -69,18 +79,31 @@ class SQWOTFormatter: Formatter {
} }
private fun introducerType(edge: EdgeComponent): String { private fun introducerType(edge: EdgeComponent): String {
return when (edge.trustDepth.value()) { if (edge.trustDepth.value() <= 0) {
0 -> "" return ""
1 -> " as a ${introducerDegree(edge.trustAmount)} trusted introducer (depth: ${edge.trustDepth.value()})"
else -> " as a ${introducerDegree(edge.trustAmount)} trusted meta-introducer (depth: ${edge.trustDepth.value()})"
} }
return buildString {
append(" as a ")
if (edge.trustAmount < AuthenticationLevel.Fully.amount) {
append("partially trusted (${edge.trustAmount} of 120) ")
} else {
append("fully trusted ")
}
if (edge.trustDepth.value() == 1) {
append("introducer (depth: ${edge.trustDepth.value()})")
} else {
append("meta-introducer (depth: ${edge.trustDepth.value()})")
}
}
} }
private fun certDegree(amount: Int): String { private fun certDegree(amount: Int): String {
return when (amount) { return if (amount >= AuthenticationLevel.Fully.amount) {
in 1 .. 119 -> "partially certified (amount: $amount of 120) " "certified"
else -> if (amount <= 0) "did not certify (amount: $amount of 120) " else "certified " } else {
"partially certified (amount: $amount of 120)"
} }
} }
} }

View File

@ -35,10 +35,10 @@ class SQWOTFormatterTest {
null, null,
true, true,
120, 120,
Depth.auto(10), Depth.auto(0),
RegexSet.wildcard()) RegexSet.wildcard())
)), )),
Depth.auto(9)), Depth.auto(0)),
120) 120)
} }
) )