Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic authentication does not work on first attempt #37

Closed
arturbosch opened this issue Aug 22, 2016 · 1 comment
Closed

Basic authentication does not work on first attempt #37

arturbosch opened this issue Aug 22, 2016 · 1 comment
Labels

Comments

@arturbosch
Copy link

arturbosch commented Aug 22, 2016

When I have following code fragments:

fun main(args: Array<String>) {
    embeddedNettyServer(8080) {
        get("/test") {
            authentication {
                println("authentication...")
                basicAuthentication("ktor") {
                    println("within basic ...")
                    authenticate(it)
                }
            }
            call.respond("Hello")
        }
    }.start(wait = true)
}

fun authenticate(user: UserPasswordCredential): Principal? {
    println(user)
    return users[user.name]?.let {
        if (it == user.password)
            UserIdPrincipal(user.name)
        else null
    }
}

I can call curl abosch@localhost:8080/test and get "Hello" prompt on first call. Following calls show me correctly that no password is specified and "Hello" is not returned.

Is it a bug or do I not understand how to use the authentication :) ?

@cy6erGn0m cy6erGn0m self-assigned this Aug 24, 2016
@cy6erGn0m cy6erGn0m added the bug label Aug 24, 2016
@cy6erGn0m
Copy link
Contributor

It's a bug in the example, will update

You should do like this

route("/test") {
    authentication {
         basicAuthentication("ktor", ::authenticate)
    }

    handle {
        call.respond("Hello")
    }
}

or

route("/") {
    authentication {
        basicAuthentication("ktor", ::authenticate)
    }

    get("/") {
        call.respond("Hello, /")
    }
    get("/other") {
        call.respond("Hello, /other")
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants