Skip to content

Commit 30e700c

Browse files
oheger-boschsschuberth
authored andcommitted
feat(utils): Pass a URL to the Authenticator if available
That way, the `Authenticator` can evaluate all information available in the URL. Signed-off-by: Oliver Heger <[email protected]>
1 parent ce9aff7 commit 30e700c

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

utils/ort/src/main/kotlin/Utils.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import java.io.File
2323
import java.net.Authenticator
2424
import java.net.PasswordAuthentication
2525
import java.net.URI
26+
import java.net.URL
2627

2728
import kotlin.coroutines.CoroutineContext
2829
import kotlin.coroutines.EmptyCoroutineContext
@@ -120,10 +121,10 @@ fun filterVersionNames(version: String, names: List<String>, project: String? =
120121
}
121122

122123
/**
123-
* Request a [PasswordAuthentication] object for the given [host], [port], and [scheme]. Install the [OrtAuthenticator]
124-
* and the [OrtProxySelector] beforehand to ensure they are active.
124+
* Request a [PasswordAuthentication] object for the given [host], [port], [scheme], and optional [url]. Install the
125+
* [OrtAuthenticator] and the [OrtProxySelector] beforehand to ensure they are active.
125126
*/
126-
fun requestPasswordAuthentication(host: String, port: Int, scheme: String): PasswordAuthentication? {
127+
fun requestPasswordAuthentication(host: String, port: Int, scheme: String, url: URL? = null): PasswordAuthentication? {
127128
OrtAuthenticator.install()
128129
OrtProxySelector.install()
129130

@@ -133,7 +134,9 @@ fun requestPasswordAuthentication(host: String, port: Int, scheme: String): Pass
133134
/* port = */ port,
134135
/* protocol = */ scheme,
135136
/* prompt = */ null,
136-
/* scheme = */ null
137+
/* scheme = */ null,
138+
/* url = */ url,
139+
/* reqType = */ Authenticator.RequestorType.SERVER
137140
)
138141
}
139142

@@ -142,7 +145,7 @@ fun requestPasswordAuthentication(host: String, port: Int, scheme: String): Pass
142145
* [OrtProxySelector] beforehand to ensure they are active.
143146
*/
144147
fun requestPasswordAuthentication(uri: URI): PasswordAuthentication? =
145-
requestPasswordAuthentication(uri.host, uri.port, uri.scheme)
148+
requestPasswordAuthentication(uri.host, uri.port, uri.scheme, uri.toURL())
146149

147150
/**
148151
* Normalize a string representing a [VCS URL][vcsUrl] to a common string form.

utils/ort/src/test/kotlin/UtilsTest.kt

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,16 @@ class UtilsTest : WordSpec({
444444
val passwordAuth = mockk<PasswordAuthentication>()
445445

446446
every {
447-
Authenticator.requestPasswordAuthentication(host, null, port, scheme, null, null)
447+
Authenticator.requestPasswordAuthentication(
448+
host,
449+
null,
450+
port,
451+
scheme,
452+
null,
453+
null,
454+
null,
455+
Authenticator.RequestorType.SERVER
456+
)
448457
} returns passwordAuth
449458

450459
requestPasswordAuthentication(host, port, scheme) shouldBe passwordAuth
@@ -455,6 +464,36 @@ class UtilsTest : WordSpec({
455464
}
456465
}
457466

467+
"return a correct authentication object for a URL" {
468+
val url = URI.create("https://www.example.org:442/auth/test")
469+
470+
mockkObject(OrtAuthenticator)
471+
mockkObject(OrtProxySelector)
472+
mockkStatic(Authenticator::class)
473+
474+
val passwordAuth = mockk<PasswordAuthentication>()
475+
476+
every {
477+
Authenticator.requestPasswordAuthentication(
478+
"www.example.org",
479+
null,
480+
442,
481+
"https",
482+
null,
483+
null,
484+
url.toURL(),
485+
Authenticator.RequestorType.SERVER
486+
)
487+
} returns passwordAuth
488+
489+
requestPasswordAuthentication(url) shouldBe passwordAuth
490+
491+
verify {
492+
OrtAuthenticator.install()
493+
OrtProxySelector.install()
494+
}
495+
}
496+
458497
"return the credentials present in the URL if any" {
459498
val host = "www.example.org"
460499
val port = 442

0 commit comments

Comments
 (0)