Skip to content

Commit

Permalink
GH-2026 Display unauthorized host in authentication error log entry (R…
Browse files Browse the repository at this point in the history
…esolve #2026)
  • Loading branch information
dzikoysk committed Jan 29, 2024
1 parent 830af84 commit 2edf061
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class AuthenticationFacade(
.map { authenticator ->
authenticator
.authenticate(credentials)
.onError { logger.debug("${credentials.name} failed to authenticate with ${authenticator.realm()} realm due to $it") }
.onError { logger.debug("${credentials.name}@${credentials.host} failed to authenticate with ${authenticator.realm()} realm due to $it") }
}
.firstOrNull { it.isOk }
?.peek { authenticationCache.put(credentials, it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.reposilite.auth.api

data class Credentials(
val host: String,
val name: String,
val secret: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,26 @@ internal class CliEndpoint(
val authMessage = connection.message()

if (!authMessage.startsWith(AUTHORIZATION_PREFIX)) {
return unauthorizedError("Unauthorized CLI access request from ${address(connection)} (missing credentials)")
return unauthorizedError("Unauthorized CLI access request from ${connection.getHost()} (missing credentials)")
}

return extractFromString(authMessage.replaceFirst(AUTHORIZATION_PREFIX, ""))
.flatMap { (name, secret) -> authenticationFacade.authenticateByCredentials(Credentials(name, secret)) }
.map { (name, secret) ->
Credentials(
host = connection.getHost(),
name = name,
secret = secret
)
}
.flatMap { authenticationFacade.authenticateByCredentials(it) }
.filter(
{ accessTokenFacade.hasPermission(it.identifier, MANAGER) },
{ unauthorized("Unauthorized CLI access request from ${address(connection)}") }
{ unauthorized("Unauthorized CLI access request from ${connection.getHost()}") }
)
.map { "${it.name}@${address(connection)}" }
.map { "${it.name}@${connection.getHost()}" }
}

private fun address(context: WsContext): String =
context.header(forwardedIp.get()) ?: context.session.remoteAddress.toString()
private fun WsContext.getHost(): String =
header(forwardedIp.get()) ?: session.remoteAddress.toString()

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ internal class RepositoryLoopbackClient(
private fun RemoteCredentials?.toAccessToken(): AccessTokenIdentifier? =
Option.of(this)
.toResult(UNAUTHORIZED.toErrorResponse("Missing credentials"))
.filter({ it.method == LOOPBACK_LINK }, { UNAUTHORIZED.toErrorResponse("") })
.flatMap { authenticationFacade.authenticateByCredentials(Credentials(it.login, it.password)) }
.filter({ it.method == LOOPBACK_LINK }, { UNAUTHORIZED.toErrorResponse() })
.flatMap { authenticationFacade.authenticateByCredentials(Credentials(host = "loopback", name = it.login, secret = it.password)) }
.fold(
{ it.identifier },
{ null }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ import io.javalin.http.Context
import io.javalin.http.ExceptionHandler
import io.javalin.http.Handler
import io.javalin.http.Header
import io.javalin.http.Header.AUTHORIZATION
import io.javalin.http.HttpStatus
import io.javalin.json.JavalinJackson
import io.javalin.openapi.plugin.OpenApiPlugin
import io.javalin.openapi.plugin.OpenApiPluginConfiguration
import io.javalin.plugin.bundled.SslRedirectPlugin
import io.javalin.util.ConcurrencyUtil
import io.javalin.util.javalinLazy
import kotlin.time.Duration.Companion.minutes
import org.eclipse.jetty.server.Server
import org.eclipse.jetty.server.ServerConnector
Expand Down Expand Up @@ -140,9 +142,9 @@ internal object JavalinConfiguration {
logger = reposilite.logger,
ctx = this,
accessTokenFacade = accessTokenFacade,
authenticationResult = lazy {
extractFromHeader(header(Header.AUTHORIZATION))
.map { (name, secret) -> Credentials(name, secret) }
authenticationResult = javalinLazy {
extractFromHeader(header(AUTHORIZATION))
.map { (name, secret) -> Credentials(host = host() ?: req().remoteAddr, name = name, secret = secret) }
.flatMap { authenticationFacade.authenticateByCredentials(it) }
}
)
Expand Down

0 comments on commit 2edf061

Please sign in to comment.