-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
178 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import Foundation | ||
import Crypto | ||
import Vapor | ||
|
||
protocol CSRF: Service { | ||
func verifyCSRF(submittedToken: String?, key: String, request: Request) throws | ||
func setCSRF(key: String, request: Request) throws -> String | ||
func generateRandom() throws -> String | ||
} | ||
|
||
extension CSRF { | ||
func generateRandom() throws -> String { | ||
return try CryptoRandom().generateData(count: 4).hexEncodedString() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Foundation | ||
|
||
struct CSRFContext: CSRFViewContext, ViewContext { | ||
var common: CommonViewContext? | ||
var csrf: String | ||
|
||
init(csrf: String) { | ||
self.csrf = csrf | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import Foundation | ||
import Vapor | ||
|
||
struct CSRFVerifier: CSRF { | ||
func setCSRF(key: String, request: Request) throws -> String { | ||
let string = "\(try generateRandom())-\(try generateRandom())-\(try generateRandom())-\(try generateRandom())" | ||
try request.session()[key] = string | ||
|
||
return string | ||
} | ||
|
||
func verifyCSRF(submittedToken: String?, key: String, request: Request) throws { | ||
guard let requiredToken: String = try request.session()[key] else { throw Abort(.forbidden) } | ||
|
||
if let token = submittedToken { | ||
guard token == requiredToken else { throw Abort(.forbidden) } | ||
} else { | ||
let _ = try request.content.decode([String: String].self).map(to: Void.self) { form in | ||
guard let submittedToken: String = form[key] else { throw Abort(.forbidden) } | ||
guard requiredToken == submittedToken else { throw Abort(.forbidden) } | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import Foundation | ||
|
||
protocol CSRFViewContext { | ||
var csrf: String { get set } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import Foundation | ||
import Vapor | ||
|
||
struct EmptyCSRFVerifier: CSRF { | ||
func verifyCSRF(submittedToken: String?, key: String, request: Request) throws { | ||
|
||
} | ||
func setCSRF(key: String, request: Request) throws -> String { | ||
return "" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import Foundation | ||
import Vapor | ||
|
||
extension Request { | ||
static let csrfKey = "csrf" | ||
|
||
func verifyCSRF(submittedToken: String? = nil, key: String = Request.csrfKey) throws { | ||
let csrf = try make(CSRF.self) | ||
return try csrf.verifyCSRF(submittedToken: submittedToken, key: key, request: self) | ||
} | ||
|
||
func setCSRF(key: String = Request.csrfKey) throws -> String { | ||
let csrf = try make(CSRF.self) | ||
return try csrf.setCSRF(key: key, request: self) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import XCTest | ||
import Foundation | ||
import FluentMySQL | ||
import Crypto | ||
@testable import Vapor | ||
@testable import App | ||
|
||
|
@@ -22,10 +23,23 @@ class LoginTests: XCTestCase { | |
func testLinuxTestSuiteIncludesAllTests() { | ||
#if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) | ||
let thisClass = type(of: self) | ||
// let linuxCount = thisClass.__allTests.count | ||
let linuxCount = 0 | ||
let linuxCount = thisClass.__allTests.count | ||
let darwinCount = Int(thisClass.defaultTestSuite.testCaseCount) | ||
XCTAssertEqual(linuxCount, darwinCount, "\(darwinCount - linuxCount) tests are missing from allTests") | ||
#endif | ||
} | ||
|
||
/// Tests that a user with invalid credentials cannot login | ||
func testLoginInvalidCredentials() throws { | ||
let _ = try User(name: "name", email: "[email protected]", password: try BCrypt.hash("password")).save(on: conn).wait() | ||
let loginRequest = LoginRequest(email: "[email protected]", password: "wrong password", csrf: "n/a") | ||
|
||
let loginResponse = try app.sendRequest(to: "/login", method: .POST, data: loginRequest, contentType: .json) | ||
XCTAssertEqual(loginResponse.http.headers.firstValue(name: .location), "/login") | ||
} | ||
|
||
/// Tests that a user with valid credentials can login | ||
func testLoginSuccessful() throws { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import XCTest | ||
|
||
extension LoginTests { | ||
static let __allTests = [ | ||
("testLinuxTestSuiteIncludesAllTests", testLinuxTestSuiteIncludesAllTests), | ||
("testLoginInvalidCredentials", testLoginInvalidCredentials), | ||
("testLoginSuccessful", testLoginSuccessful), | ||
] | ||
} | ||
|
||
extension RegisterTests { | ||
static let __allTests = [ | ||
("testLinuxTestSuiteIncludesAllTests", testLinuxTestSuiteIncludesAllTests), | ||
("testInvalidEmailFails", testInvalidEmailFails), | ||
("testRegisterEmailAlreadyExists", testRegisterEmailAlreadyExists), | ||
("testRegisterPasswordsDontMatch", testRegisterPasswordsDontMatch), | ||
("testSuccessfulRegister", testSuccessfulRegister), | ||
] | ||
} | ||
|
||
#if !os(macOS) | ||
public func __allTests() -> [XCTestCaseEntry] { | ||
return [ | ||
testCase(LoginTests.__allTests), | ||
testCase(RegisterTests.__allTests), | ||
] | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import XCTest | ||
|
||
import AppTests | ||
|
||
var tests = [XCTestCaseEntry]() | ||
tests += AppTests.__allTests() | ||
|
||
XCTMain(tests) |