Skip to content
This repository has been archived by the owner on Nov 16, 2020. It is now read-only.

Commit

Permalink
add session check during unauth (#60)
Browse files Browse the repository at this point in the history
* add session check during unauth

* update hasSession to method call
  • Loading branch information
tanner0101 authored Feb 12, 2019
1 parent 5aefb28 commit 9c71c8a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
9 changes: 4 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@ let package = Package(

// 💧 A server-side Swift web framework.
.package(url: "https://github.com/vapor/vapor.git", from: "3.0.0"),

// Fluent SQLite, only for testing.
.package(url: "https://github.com/vapor/fluent-sqlite.git", from: "3.0.0"),
],
targets: [
.target(name: "Authentication", dependencies: ["Async", "Bits", "Crypto", "Debugging", "Fluent", "HTTP", "Service", "Vapor"]),
.testTarget(name: "AuthenticationTests", dependencies: ["Authentication", "FluentSQLite", "Vapor"]),
]
)

if ProcessInfo.processInfo.environment["ENABLE_TESTS"]?.lowercased() == "true" {
package.dependencies.append(.package(url: "https://github.com/vapor/fluent-sqlite.git", .branch("master")))
package.targets.append(.testTarget(name: "AuthenticationTests", dependencies: ["Authentication", "FluentSQLite", "Vapor"]))
}
3 changes: 3 additions & 0 deletions Sources/Authentication/Persist/SessionAuthenticatable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ extension Request {

/// Un-authenticates the model from the session.
public func unauthenticateSession<A>(_ a: A.Type) throws where A: SessionAuthenticatable {
guard try self.hasSession() else {
return
}
try session()["_" + A.sessionName + "Session"] = nil
try unauthenticate(A.self)
}
Expand Down
21 changes: 18 additions & 3 deletions Tests/AuthenticationTests/AuthenticationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,20 @@ class AuthenticationTests: XCTestCase {

var migrations = MigrationConfig()
migrations.add(model: User.self, database: .test)
migrations.prepareCache(for: .test)
services.register(migrations)

var middleware = MiddlewareConfig.default()
middleware.use(SessionsMiddleware.self)
services.register(middleware)
services.register(MemoryKeyedCache(), as: KeyedCache.self)

services.register(KeyedCache.self) { container -> SQLiteCache in
let pool = try container.connectionPool(to: .test)
return .init(pool: pool)
}

var config = Config.default()
config.prefer(MemoryKeyedCache.self, for: KeyedCache.self)
config.prefer(SQLiteCache.self, for: KeyedCache.self)

let app = try Application(config: config, services: services)

Expand All @@ -104,7 +109,8 @@ class AuthenticationTests: XCTestCase {
}

group.get("logout") { req -> HTTPStatus in
try req.unauthenticateSession(User.self)
try req.destroySession()
try req.unauthenticate(User.self)
return .ok
}

Expand Down Expand Up @@ -167,6 +173,15 @@ class AuthenticationTests: XCTestCase {
let res = try responder.respond(to: req).wait()
XCTAssertEqual(res.http.status, .ok)
}

// ensure the session has been removed from storage
do {
let conn = try sqlite.newConnection(on: app.eventLoop).wait()
try conn.raw("SELECT COUNT(*) as count FROM fluentcache").run { row in
let count = row.firstValue(forColumn: "count")!.description
XCTAssertEqual(count, "0")
}.wait()
}

/// logged-out persisted req
do {
Expand Down

0 comments on commit 9c71c8a

Please sign in to comment.