Не подтверждена Коммит 5cdf819c создал по автору Sergey Mashkov's avatar Sergey Mashkov
Просмотр файлов

Make authentication feature's phases public (#1160)

владелец f471bc33
......@@ -32,7 +32,7 @@ class Authentication(config: Configuration) {
* Authentication configuration
*/
class Configuration(providers: List<AuthenticationProvider> = emptyList()) {
internal val providers = ArrayList<AuthenticationProvider>(providers)
internal val providers = ArrayList(providers)
/**
* Register a provider with the specified [name] and [configure] it
......@@ -112,10 +112,10 @@ class Authentication(config: Configuration) {
}
}
pipeline.insertPhaseAfter(ApplicationCallPipeline.Features, authenticationPhase)
pipeline.insertPhaseAfter(authenticationPhase, challengePhase)
pipeline.insertPhaseAfter(ApplicationCallPipeline.Features, AuthenticatePhase)
pipeline.insertPhaseAfter(AuthenticatePhase, ChallengePhase)
pipeline.intercept(authenticationPhase) {
pipeline.intercept(AuthenticatePhase) {
val call = call
val authenticationContext = AuthenticationContext.from(call)
if (authenticationContext.principal != null) return@intercept
......@@ -123,7 +123,7 @@ class Authentication(config: Configuration) {
processAuthentication(call, authenticationContext, configurations, authenticationPipeline)
}
pipeline.intercept(challengePhase) {
pipeline.intercept(ChallengePhase) {
val context = AuthenticationContext.from(call)
when {
......@@ -143,10 +143,21 @@ class Authentication(config: Configuration) {
* Installable feature for [Authentication].
*/
companion object Feature : ApplicationFeature<Application, Configuration, Authentication> {
private val authenticationPhase = PipelinePhase("Authenticate")
private val challengePhase = PipelinePhase("Challenge")
/**
* Authenticate phase in that authentication procedures are executed.
* Please note that referring to the phase is only possible *after* feature installation.
*/
@KtorExperimentalAPI
val AuthenticatePhase: PipelinePhase = PipelinePhase("Authenticate")
/**
* Authenticate phase in that auth provider's challenges performing.
* Please note that referring to the phase is only possible *after* feature installation.
*/
@KtorExperimentalAPI
val ChallengePhase: PipelinePhase = PipelinePhase("Challenge")
override val key = AttributeKey<Authentication>("Authentication")
override val key: AttributeKey<Authentication> = AttributeKey("Authentication")
override fun install(pipeline: Application, configure: Configuration.() -> Unit): Authentication {
return Authentication().apply {
......@@ -177,11 +188,11 @@ class Authentication(config: Configuration) {
context: AuthenticationContext,
handleErrors: Boolean
) {
val challengePipeline = Pipeline<AuthenticationProcedureChallenge, ApplicationCall>(challengePhase)
val challengePipeline = Pipeline<AuthenticationProcedureChallenge, ApplicationCall>(ChallengePhase)
val challenges = context.challenge.challenges
for (challenge in challenges) {
challengePipeline.intercept(challengePhase) {
challengePipeline.intercept(ChallengePhase) {
challenge(it)
if (it.completed)
finish() // finish challenge pipeline if it has been completed
......@@ -190,7 +201,7 @@ class Authentication(config: Configuration) {
if (handleErrors) {
for (challenge in context.challenge.errorChallenges) {
challengePipeline.intercept(challengePhase) {
challengePipeline.intercept(ChallengePhase) {
challenge(it)
if (it.completed)
finish() // finish challenge pipeline if it has been completed
......@@ -198,7 +209,7 @@ class Authentication(config: Configuration) {
}
for (error in context.errors.values.filterIsInstance<AuthenticationFailedCause.Error>()) {
challengePipeline.intercept(challengePhase) {
challengePipeline.intercept(ChallengePhase) {
if (!it.completed) {
logger.trace("Responding unauthorized because of error ${error.cause}")
call.respond(UnauthorizedResponse())
......@@ -267,7 +278,7 @@ val ApplicationCall.authentication: AuthenticationContext
/**
* Retrieves authenticated [Principal] for `this` call
*/
inline fun <reified P : Principal> ApplicationCall.principal() = authentication.principal<P>()
inline fun <reified P : Principal> ApplicationCall.principal(): P? = authentication.principal<P>()
/**
* Creates an authentication route that does handle authentication by the specified providers referred by
......
Поддерживает Markdown
0% или .
You are about to add 0 people to the discussion. Proceed with caution.
Сначала завершите редактирование этого сообщения!
Пожалуйста, зарегистрируйтесь или чтобы прокомментировать