Skip to content

Commit d4fcd27

Browse files
oheger-boschmnonnenmacher
authored andcommitted
fix(git): Pass the full URL to the Authenticator
When querying the credentials for the current repository provide the complete URL, not only host, port, and scheme. This gives the `Authenticator` more context to select the best-fitting credentials. Signed-off-by: Oliver Heger <oliver.heger@bosch.com>
1 parent a7f6289 commit d4fcd27

2 files changed

Lines changed: 37 additions & 7 deletions

File tree

plugins/version-control-systems/git/src/main/kotlin/Git.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import org.ossreviewtoolkit.utils.common.CommandLineTool
5454
import org.ossreviewtoolkit.utils.common.Os
5555
import org.ossreviewtoolkit.utils.common.collectMessages
5656
import org.ossreviewtoolkit.utils.common.safeMkdirs
57+
import org.ossreviewtoolkit.utils.common.toUri
5758
import org.ossreviewtoolkit.utils.common.withoutPrefix
5859
import org.ossreviewtoolkit.utils.ort.requestPasswordAuthentication
5960
import org.ossreviewtoolkit.utils.ort.showStackTrace
@@ -352,9 +353,11 @@ internal object AuthenticatorCredentialsProvider : CredentialsProvider() {
352353
}
353354

354355
override fun get(uri: URIish, vararg items: CredentialItem): Boolean {
355-
logger.debug { "JGit queries credentials ${items.map { it.javaClass.simpleName }} for '${uri.host}'." }
356+
logger.debug { "JGit queries credentials ${items.map { it.javaClass.simpleName }} for '$uri'." }
356357

357-
val auth = requestPasswordAuthentication(uri.host, uri.port, uri.scheme) ?: return false
358+
val url = uri.toString().toUri { it.toURL() }.getOrNull()
359+
val auth = requestPasswordAuthentication(uri.host.orEmpty(), uri.port, uri.scheme.orEmpty(), url)
360+
?: return false
358361

359362
logger.debug { "Passing credentials for '${uri.host}' to JGit." }
360363

plugins/version-control-systems/git/src/test/kotlin/GitTest.kt

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,31 @@ class GitTest : WordSpec({
115115

116116
val credentialProvider = CredentialsProvider.getDefault()
117117

118-
credentialProvider.get(TestUri, userCredential, passwordCredential) shouldBe true
118+
credentialProvider.get(testUri, userCredential, passwordCredential) shouldBe true
119+
120+
verify {
121+
userCredential.value = user
122+
passwordCredential.value = password
123+
}
124+
}
125+
126+
"handle an invalid URL" {
127+
val user = "scott"
128+
val password = "tiger".toCharArray()
129+
val userCredential = mockk<CredentialItem.Username>()
130+
val passwordCredential = mockk<CredentialItem.Password>()
131+
val invalidUri = URIish("foo")
132+
133+
every { userCredential.value = any() } just runs
134+
every { passwordCredential.value = any() } just runs
135+
mockAuthentication(null)
136+
every {
137+
requestPasswordAuthentication("", invalidUri.port, "", null)
138+
} returns PasswordAuthentication(user, password)
139+
140+
val credentialProvider = CredentialsProvider.getDefault()
141+
142+
credentialProvider.get(invalidUri, userCredential, passwordCredential) shouldBe true
119143

120144
verify {
121145
userCredential.value = user
@@ -131,7 +155,7 @@ class GitTest : WordSpec({
131155

132156
val credentialProvider = CredentialsProvider.getDefault()
133157

134-
credentialProvider.get(TestUri, userCredential, passwordCredential) shouldBe false
158+
credentialProvider.get(testUri, userCredential, passwordCredential) shouldBe false
135159
}
136160

137161
"throw for unsupported credential types" {
@@ -142,7 +166,7 @@ class GitTest : WordSpec({
142166
val credentialProvider = CredentialsProvider.getDefault()
143167

144168
shouldThrow<UnsupportedCredentialItem> {
145-
credentialProvider.get(TestUri, otherCredential)
169+
credentialProvider.get(testUri, otherCredential)
146170
}
147171
}
148172

@@ -154,13 +178,16 @@ class GitTest : WordSpec({
154178
}
155179
})
156180

157-
private val TestUri = URIish(URI("https://www.example.org:8080/foo").toURL())
181+
private val testUri = URIish(URI("https://www.example.org:8080/foo").toURL())
182+
private val testUriAsUrl = URI.create(testUri.toString()).toURL()
158183

159184
/**
160185
* Mocks the utility function to query password authentication for the test URI. Return the [result] provided.
161186
*/
162187
private fun mockAuthentication(result: PasswordAuthentication?) {
163188
mockkStatic("org.ossreviewtoolkit.utils.ort.UtilsKt")
164189

165-
every { requestPasswordAuthentication(TestUri.host, TestUri.port, TestUri.scheme) } returns result
190+
every {
191+
requestPasswordAuthentication(testUri.host, testUri.port, testUri.scheme, testUriAsUrl)
192+
} returns result
166193
}

0 commit comments

Comments
 (0)