vapor / auth

👤 Authentication and Authorization framework for Fluent.
53 stars 34 forks source link

[Beta] Crash in middleware: DB connection #23

Closed bensyverson closed 6 years ago

bensyverson commented 6 years ago

Summary

I'm getting a crash when I use the middleware in the beta branch of Auth. Specifically when requesting a route which has been protected with basicAuthMiddleware or tokenAuthMiddleware.

Detail

Given a User model which is BasicAuthenticatable:

extension User: BasicAuthenticatable {
    static let usernameKey = \User.email
    static let passwordKey = \User.password
}

…And a route which uses the middleware:

let authMiddleware = try User.basicAuthMiddleware(using: PlaintextVerifier(), database: .psql)
let group = router.grouped(authMiddleware)
group.get("test"){ req -> String in
    return "OK"
}

…Requesting /test results in a crash when DatabaseKit tries to use a nil connection.

Results

DatabaseKit throws an exception in line 21 of /Connection/DatabaseConnection.swift:

extension DatabaseConnection {
    /// See `DatabaseConnectable.connect(to:)`
    public func connect<D>(to database: DatabaseIdentifier<D>?) -> Future<D.Connection> {
        assert(database == nil, "Unexpected \(#function): nil database identifier") // 🚨🚨🚨
        assert(self is D.Connection, "Unexpected \(#function): \(self) not \(D.Connection.self)")
        return Future(self as! D.Connection)
    }
}

🚨: codes.vapor.application (3): Assertion failed: Unexpected connect(to:): nil database identifier

Steps to reproduce

bensyverson commented 6 years ago

The underlying error here is in DatabaseKit, and I have PR on vapor/database-kit which should take care of it.