-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace homeserver authorization approach with an
Authorization
hea…
…der instead of `access_token` when talking to the application service, as per [MSC2832](matrix-org/matrix-spec-proposals#2832).
- Loading branch information
Showing
4 changed files
with
108 additions
and
56 deletions.
There are no files selected for viewing
50 changes: 0 additions & 50 deletions
50
...nMain/kotlin/net/folivo/trixnity/applicationserviceapi/server/MatrixQueryParameterAuth.kt
This file was deleted.
Oops, something went wrong.
69 changes: 69 additions & 0 deletions
69
...tlin/net/folivo/trixnity/applicationserviceapi/server/MatrixQueryParameterOrBearerAuth.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package net.folivo.trixnity.applicationserviceapi.server | ||
|
||
import io.ktor.http.* | ||
import io.ktor.http.auth.* | ||
import io.ktor.server.auth.* | ||
import io.ktor.server.request.* | ||
import io.ktor.server.response.* | ||
import net.folivo.trixnity.core.ErrorResponse | ||
|
||
class MatrixQueryParameterOrBearerAuthenticationProvider internal constructor( | ||
configuration: Configuration, | ||
private val field: String, | ||
private val token: String | ||
) : AuthenticationProvider(configuration) { | ||
class Configuration internal constructor(name: String? = null) : Config(name) | ||
|
||
override suspend fun onAuthenticate(context: AuthenticationContext) { | ||
val queryCredentials = context.call.request.queryParameters[field] | ||
val accessTokenCredentials = context.call.request.getAccessTokenFromHeader() | ||
val cause = when { | ||
queryCredentials == null && accessTokenCredentials == null -> AuthenticationFailedCause.NoCredentials | ||
accessTokenCredentials != null && queryCredentials != null && accessTokenCredentials != queryCredentials | ||
|| (accessTokenCredentials == null && queryCredentials != token) | ||
|| (queryCredentials == null && accessTokenCredentials != token) | ||
|| (accessTokenCredentials == queryCredentials && queryCredentials != token) -> AuthenticationFailedCause.InvalidCredentials | ||
|
||
else -> null | ||
} | ||
|
||
if (cause != null) { | ||
context.challenge("MatrixQueryParameterAuth", cause) { challenge, call -> | ||
when (cause) { | ||
AuthenticationFailedCause.NoCredentials -> | ||
call.respond<ErrorResponse>(HttpStatusCode.Unauthorized, ErrorResponse.Unauthorized()) | ||
|
||
else -> call.respond<ErrorResponse>(HttpStatusCode.Forbidden, ErrorResponse.Forbidden()) | ||
} | ||
challenge.complete() | ||
} | ||
} else { | ||
context.principal(UserIdPrincipal("homeserver")) | ||
} | ||
} | ||
} | ||
|
||
private fun ApplicationRequest.getAccessTokenFromHeader(): String? { | ||
return when (val authHeader = parseAuthorizationHeader()) { | ||
is HttpAuthHeader.Single -> { | ||
if (!authHeader.authScheme.equals("Bearer", ignoreCase = true)) null | ||
else authHeader.blob | ||
} | ||
|
||
else -> null | ||
} | ||
} | ||
|
||
fun AuthenticationConfig.matrixQueryParameterOrBearer( | ||
name: String? = null, | ||
field: String, | ||
token: String, | ||
) { | ||
val provider = | ||
MatrixQueryParameterOrBearerAuthenticationProvider( | ||
MatrixQueryParameterOrBearerAuthenticationProvider.Configuration(name), | ||
field, | ||
token | ||
) | ||
register(provider) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters