Double basic authentication

Report a typo

The Ktor authentication plugin allows you to create multiple authorizations and give them different names. Let's create two basic authentications as follows:

install(Authentication) {
    basic("myAuth") {
        realm = "Access to the '/page' path"
        validate { credentials ->
            if (credentials.name == "Admin" && credentials.password == "2425") {
                UserIdPrincipal(credentials.name)
            } else {
                null
            }
        }
    }
    basic("myAuth2") {
        realm = "Access to the '/page' path"
        validate { credentials ->
            if (credentials.name == "Admin2" && credentials.password == "1213") {
                UserIdPrincipal(credentials.name)
            } else {
                null
            }
        }
    }
}

What happens if we apply these authorizations to one page?

authenticate("myAuth") {
    authenticate("myAuth2") {
        get("/page") {
            call.respondText("Name: ${call.principal<UserIdPrincipal>()?.name}")
        }
    }
}

Open the IDE and check the result yourself.

Select one or more options from the list
___

Create a free account to access the full topic