Skip to content

Commit 9c71c8a

Browse files
authored
add session check during unauth (#60)
* add session check during unauth * update hasSession to method call
1 parent 5aefb28 commit 9c71c8a

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

Package.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@ let package = Package(
2525

2626
// 💧 A server-side Swift web framework.
2727
.package(url: "https://github.com/vapor/vapor.git", from: "3.0.0"),
28+
29+
// Fluent SQLite, only for testing.
30+
.package(url: "https://github.com/vapor/fluent-sqlite.git", from: "3.0.0"),
2831
],
2932
targets: [
3033
.target(name: "Authentication", dependencies: ["Async", "Bits", "Crypto", "Debugging", "Fluent", "HTTP", "Service", "Vapor"]),
34+
.testTarget(name: "AuthenticationTests", dependencies: ["Authentication", "FluentSQLite", "Vapor"]),
3135
]
3236
)
33-
34-
if ProcessInfo.processInfo.environment["ENABLE_TESTS"]?.lowercased() == "true" {
35-
package.dependencies.append(.package(url: "https://github.com/vapor/fluent-sqlite.git", .branch("master")))
36-
package.targets.append(.testTarget(name: "AuthenticationTests", dependencies: ["Authentication", "FluentSQLite", "Vapor"]))
37-
}

Sources/Authentication/Persist/SessionAuthenticatable.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ extension Request {
4141

4242
/// Un-authenticates the model from the session.
4343
public func unauthenticateSession<A>(_ a: A.Type) throws where A: SessionAuthenticatable {
44+
guard try self.hasSession() else {
45+
return
46+
}
4447
try session()["_" + A.sessionName + "Session"] = nil
4548
try unauthenticate(A.self)
4649
}

Tests/AuthenticationTests/AuthenticationTests.swift

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,20 @@ class AuthenticationTests: XCTestCase {
7373

7474
var migrations = MigrationConfig()
7575
migrations.add(model: User.self, database: .test)
76+
migrations.prepareCache(for: .test)
7677
services.register(migrations)
7778

7879
var middleware = MiddlewareConfig.default()
7980
middleware.use(SessionsMiddleware.self)
8081
services.register(middleware)
81-
services.register(MemoryKeyedCache(), as: KeyedCache.self)
82+
83+
services.register(KeyedCache.self) { container -> SQLiteCache in
84+
let pool = try container.connectionPool(to: .test)
85+
return .init(pool: pool)
86+
}
8287

8388
var config = Config.default()
84-
config.prefer(MemoryKeyedCache.self, for: KeyedCache.self)
89+
config.prefer(SQLiteCache.self, for: KeyedCache.self)
8590

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

@@ -104,7 +109,8 @@ class AuthenticationTests: XCTestCase {
104109
}
105110

106111
group.get("logout") { req -> HTTPStatus in
107-
try req.unauthenticateSession(User.self)
112+
try req.destroySession()
113+
try req.unauthenticate(User.self)
108114
return .ok
109115
}
110116

@@ -167,6 +173,15 @@ class AuthenticationTests: XCTestCase {
167173
let res = try responder.respond(to: req).wait()
168174
XCTAssertEqual(res.http.status, .ok)
169175
}
176+
177+
// ensure the session has been removed from storage
178+
do {
179+
let conn = try sqlite.newConnection(on: app.eventLoop).wait()
180+
try conn.raw("SELECT COUNT(*) as count FROM fluentcache").run { row in
181+
let count = row.firstValue(forColumn: "count")!.description
182+
XCTAssertEqual(count, "0")
183+
}.wait()
184+
}
170185

171186
/// logged-out persisted req
172187
do {

0 commit comments

Comments
 (0)