Skip to content

Commit 6bb82b7

Browse files
committed
Use RichOption/Optional for Scala/Java conversion
1 parent 68a1186 commit 6bb82b7

File tree

12 files changed

+146
-199
lines changed

12 files changed

+146
-199
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import com.fasterxml.jackson.databind.node.JsonNodeFactory
2929
import com.fasterxml.jackson.databind.node.ObjectNode
3030
import com.yubico.internal.util.CertificateParser
3131
import com.yubico.internal.util.JacksonCodecs
32-
import com.yubico.internal.util.scala.JavaConverters._
3332
import com.yubico.webauthn.TestAuthenticator.AttestationCert
3433
import com.yubico.webauthn.TestAuthenticator.AttestationMaker
3534
import com.yubico.webauthn.TestAuthenticator.AttestationSigner
@@ -58,6 +57,7 @@ import java.security.PrivateKey
5857
import java.security.cert.X509Certificate
5958
import java.security.spec.PKCS8EncodedKeySpec
6059
import scala.jdk.CollectionConverters._
60+
import scala.jdk.OptionConverters.RichOption
6161
import scala.util.Failure
6262
import scala.util.Success
6363
import scala.util.Try
@@ -752,7 +752,7 @@ case class RegistrationTestData(
752752
).asJava
753753
)
754754
.extensions(requestedExtensions)
755-
.authenticatorSelection(authenticatorSelection.asJava)
755+
.authenticatorSelection(authenticatorSelection.toJava)
756756
.build()
757757

758758
def response: PublicKeyCredential[

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

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import com.fasterxml.jackson.databind.node.JsonNodeFactory
2929
import com.fasterxml.jackson.databind.node.ObjectNode
3030
import com.upokecenter.cbor.CBORObject
3131
import com.yubico.internal.util.JacksonCodecs
32-
import com.yubico.internal.util.scala.JavaConverters._
3332
import com.yubico.webauthn.data.AssertionExtensionInputs
3433
import com.yubico.webauthn.data.AuthenticatorAssertionResponse
3534
import com.yubico.webauthn.data.AuthenticatorTransport
@@ -71,6 +70,8 @@ import java.security.MessageDigest
7170
import java.security.interfaces.ECPublicKey
7271
import java.util.Optional
7372
import scala.jdk.CollectionConverters._
73+
import scala.jdk.OptionConverters.RichOption
74+
import scala.jdk.OptionConverters.RichOptional
7475
import scala.util.Failure
7576
import scala.util.Success
7677
import scala.util.Try
@@ -137,7 +138,7 @@ class RelyingPartyAssertionSpec
137138
userHandle: ByteArray,
138139
): Optional[ByteArray] =
139140
if (username == Defaults.username)
140-
Some(userHandle).asJava
141+
Some(userHandle).toJava
141142
else
142143
???
143144

@@ -146,7 +147,7 @@ class RelyingPartyAssertionSpec
146147
username: String,
147148
): Optional[String] =
148149
if (userHandle == Defaults.userHandle)
149-
Some(username).asJava
150+
Some(username).toJava
150151
else
151152
???
152153

@@ -201,12 +202,12 @@ class RelyingPartyAssertionSpec
201202
.builder()
202203
.challenge(challenge)
203204
.rpId(rpId.getId)
204-
.allowCredentials(allowCredentials.asJava)
205+
.allowCredentials(allowCredentials.toJava)
205206
.userVerification(userVerificationRequirement)
206207
.extensions(requestedExtensions)
207208
.build()
208209
)
209-
.username(usernameForRequest.asJava)
210+
.username(usernameForRequest.toJava)
210211
.build()
211212

212213
val response = PublicKeyCredential
@@ -246,9 +247,9 @@ class RelyingPartyAssertionSpec
246247
.build()
247248
)
248249
else None
249-
).asJava
250+
).toJava
250251
override def lookupAll(credId: ByteArray) =
251-
lookup(credId, null).asScala.toSet.asJava
252+
lookup(credId, null).toScala.toSet.asJava
252253
override def getCredentialIdsForUsername(username: String) = ???
253254
override def getUserHandleForUsername(username: String)
254255
: Optional[ByteArray] =
@@ -274,7 +275,7 @@ class RelyingPartyAssertionSpec
274275

275276
builder
276277
.build()
277-
._finishAssertion(request, response, callerTokenBindingId.asJava)
278+
._finishAssertion(request, response, callerTokenBindingId.toJava)
278279
}
279280

280281
testWithEachProvider { it =>
@@ -298,10 +299,10 @@ class RelyingPartyAssertionSpec
298299
.build()
299300
)
300301

301-
request1.getPublicKeyCredentialRequestOptions.getUserVerification.asScala should be(
302+
request1.getPublicKeyCredentialRequestOptions.getUserVerification.toScala should be(
302303
None
303304
)
304-
request2.getPublicKeyCredentialRequestOptions.getUserVerification.asScala should be(
305+
request2.getPublicKeyCredentialRequestOptions.getUserVerification.toScala should be(
305306
None
306307
)
307308
}
@@ -317,11 +318,11 @@ class RelyingPartyAssertionSpec
317318
val request = rp.startAssertion(
318319
StartAssertionOptions
319320
.builder()
320-
.userVerification(uv.asJava)
321+
.userVerification(uv.toJava)
321322
.build()
322323
)
323324

324-
request.getPublicKeyCredentialRequestOptions.getUserVerification.asScala should equal(
325+
request.getPublicKeyCredentialRequestOptions.getUserVerification.toScala should equal(
325326
uv
326327
)
327328
}
@@ -368,15 +369,15 @@ class RelyingPartyAssertionSpec
368369
username: String
369370
): Optional[ByteArray] =
370371
if (username == user.getName)
371-
Some(user.getId).asJava
372-
else None.asJava
372+
Some(user.getId).toJava
373+
else None.toJava
373374

374375
override def getUsernameForUserHandle(
375376
userHandle: ByteArray
376377
): Optional[String] =
377378
if (userHandle == user.getId)
378-
Some(user.getName).asJava
379-
else None.asJava
379+
Some(user.getName).toJava
380+
else None.toJava
380381

381382
override def lookup(
382383
credentialId: ByteArray,
@@ -386,8 +387,8 @@ class RelyingPartyAssertionSpec
386387
if (
387388
credentialId == credential.getCredentialId && userHandle == user.getId
388389
)
389-
Some(credential).asJava
390-
else None.asJava
390+
Some(credential).toJava
391+
else None.toJava
391392
}
392393

393394
override def lookupAll(
@@ -492,13 +493,13 @@ class RelyingPartyAssertionSpec
492493

493494
val requestCreds =
494495
result.getPublicKeyCredentialRequestOptions.getAllowCredentials.get.asScala
495-
requestCreds.head.getTransports.asScala should equal(
496+
requestCreds.head.getTransports.toScala should equal(
496497
Some(cred1Transports.asJava)
497498
)
498-
requestCreds(1).getTransports.asScala should equal(
499+
requestCreds(1).getTransports.toScala should equal(
499500
Some(Set.empty.asJava)
500501
)
501-
requestCreds(2).getTransports.asScala should equal(None)
502+
requestCreds(2).getTransports.toScala should equal(None)
502503
}
503504
}
504505
}
@@ -614,7 +615,7 @@ class RelyingPartyAssertionSpec
614615
.publicKeyCose(getPublicKeyBytes(Defaults.credentialKey))
615616
.signatureCount(0)
616617
.build()
617-
).asJava
618+
).toJava
618619
override def lookupAll(id: ByteArray) = ???
619620
override def getCredentialIdsForUsername(username: String) = ???
620621
override def getUserHandleForUsername(
@@ -623,14 +624,14 @@ class RelyingPartyAssertionSpec
623624
Some(
624625
if (username == owner.username) owner.userHandle
625626
else nonOwner.userHandle
626-
).asJava
627+
).toJava
627628
override def getUsernameForUserHandle(
628629
userHandle: ByteArray
629630
): Optional[String] =
630631
Some(
631632
if (userHandle == owner.userHandle) owner.username
632633
else nonOwner.username
633-
).asJava
634+
).toJava
634635
})
635636

636637
describe("If the user was identified before the authentication ceremony was initiated, e.g., via a username or cookie, verify that the identified user is the owner of credentialSource. If response.userHandle is present, let userHandle be its value. Verify that userHandle also maps to the same user.") {
@@ -749,7 +750,7 @@ class RelyingPartyAssertionSpec
749750
val step: steps.Step7 = new steps.Step7(
750751
Defaults.username,
751752
Defaults.userHandle,
752-
None.asJava,
753+
None.toJava,
753754
)
754755

755756
step.validations shouldBe a[Failure[_]]
@@ -1302,7 +1303,7 @@ class RelyingPartyAssertionSpec
13021303
val appid = new AppId("https://test.example.org/foo")
13031304
val extensions = AssertionExtensionInputs
13041305
.builder()
1305-
.appid(Some(appid).asJava)
1306+
.appid(Some(appid).toJava)
13061307
.build()
13071308

13081309
it("fails if RP ID is different.") {
@@ -2238,10 +2239,10 @@ class RelyingPartyAssertionSpec
22382239
.build()
22392240
)
22402241

2241-
result.getClientExtensionOutputs.get.getLargeBlob.get.getWritten.asScala should be(
2242+
result.getClientExtensionOutputs.get.getLargeBlob.get.getWritten.toScala should be(
22422243
Some(true)
22432244
)
2244-
result.getClientExtensionOutputs.get.getLargeBlob.get.getBlob.asScala should be(
2245+
result.getClientExtensionOutputs.get.getLargeBlob.get.getBlob.toScala should be(
22452246
None
22462247
)
22472248
}
@@ -2282,10 +2283,10 @@ class RelyingPartyAssertionSpec
22822283
.build()
22832284
)
22842285

2285-
result.getClientExtensionOutputs.get.getLargeBlob.get.getBlob.asScala should be(
2286+
result.getClientExtensionOutputs.get.getLargeBlob.get.getBlob.toScala should be(
22862287
Some(ByteArray.fromHex("00010203"))
22872288
)
2288-
result.getClientExtensionOutputs.get.getLargeBlob.get.getWritten.asScala should be(
2289+
result.getClientExtensionOutputs.get.getLargeBlob.get.getWritten.toScala should be(
22892290
None
22902291
)
22912292
}
@@ -2337,7 +2338,7 @@ class RelyingPartyAssertionSpec
23372338
.build()
23382339
)
23392340

2340-
result.getAuthenticatorExtensionOutputs.get.getUvm.asScala should equal(
2341+
result.getAuthenticatorExtensionOutputs.get.getUvm.toScala should equal(
23412342
Some(
23422343
List(
23432344
new UvmEntry(

0 commit comments

Comments
 (0)