Skip to content

Commit 40acf21

Browse files
committed
Extract variable algOid in WebAuthnTestCodecs.eddsaPublicKeyToCose
1 parent 3f62966 commit 40acf21

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

webauthn-server-core/src/test/scala/com/yubico/webauthn/WebAuthnTestCodecs.scala

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import java.security.PublicKey
1414
import java.security.interfaces.ECPublicKey
1515
import java.security.interfaces.RSAPublicKey
1616
import java.security.spec.PKCS8EncodedKeySpec
17+
import scala.util.Try
1718

1819
/** Re-exports from [[WebAuthnCodecs]] and [[Crypto]] so tests can use it
1920
*/
@@ -73,18 +74,15 @@ object WebAuthnTestCodecs {
7374

7475
def eddsaPublicKeyToCose(key: BCEdDSAPublicKey): ByteArray = {
7576
val encoded = key.getEncoded
76-
val (alg, crv, keyBytesLength) =
77-
if (
78-
encoded.length > 9 && encoded
79-
.slice(2, 9)
80-
.sameElements(WebAuthnCodecs.ED25519_ALG_ID.getBytes)
77+
val algOid: Array[Byte] =
78+
Try(encoded.slice(2, 9)).getOrElse(
79+
throw new IllegalArgumentException("Unknown EdDSA ASN.1 OID prefix")
8180
)
81+
82+
val (alg, crv, keyBytesLength) =
83+
if (algOid.sameElements(WebAuthnCodecs.ED25519_ALG_ID.getBytes))
8284
(COSEAlgorithmIdentifier.EdDSA, COSE_CRV_ED25519, 32)
83-
else if (
84-
encoded.length > 9 && encoded
85-
.slice(2, 9)
86-
.sameElements(WebAuthnCodecs.ED448_ALG_ID.getBytes)
87-
)
85+
else if (algOid.sameElements(WebAuthnCodecs.ED448_ALG_ID.getBytes))
8886
(COSEAlgorithmIdentifier.Ed448, COSE_CRV_ED448, 57)
8987
else throw new IllegalArgumentException("Unknown EdDSA ASN.1 OID prefix")
9088

0 commit comments

Comments
 (0)