在本篇中棚愤,我們將專注于構(gòu)建基于session的 Web 身份驗證層。 用戶將能夠使用表單登錄,并且已經(jīng)登錄的用戶將在session cookie 和使用 Fluent 的持久session存儲的幫助下被檢測到。 我們會使用自定義身份驗證器中間件茎杂,通過session或credentials對用戶進行身份驗證珍德。
User module
用戶模塊將負(fù)責(zé)用戶管理和認(rèn)證摔竿。 請創(chuàng)建一個新的用戶模塊目錄結(jié)構(gòu)真慢,就像我們?yōu)椴┛湍K所做的那樣。 我們將需要一個 User
文件夾榕吼,一個包含 Migrations
和 Models
目錄的 Database
文件夾饿序。
首先我們需要一個模型來存儲用戶帳號數(shù)據(jù),用戶可以通過郵箱和密碼進行登錄羹蚣。所以我們需要新建一個UserAccountModel
/// FILE: Sources/App/Modules/User/Database/Models/UserAccountModel.swift
import Vapor
import Fluent
final class UserAccountModel: DatabaseModelInterface {
typealias Module = UserModule
struct FieldKeys {
struct v1 {
static var email: FieldKey { "email" }
static var password: FieldKey { "password" }
}
}
@ID() var id: UUID?
@Field(key: FieldKeys.v1.email) var email: String
@Field(key: FieldKeys.v1.password) var password: String
init() { }
init(id: UUID? = nil, email: String, password: String) {
self.id = id
self.email = email
self.password = password
}
}
跟上一篇文章一樣原探,我們還需要實現(xiàn)數(shù)據(jù)庫遷移來初始化用戶表和做數(shù)據(jù)填充。
/// FILE: Sources/App/Modules/User/Database/Migrations/UserMigrations.swift
import Vapor
import Fluent
enum UserMigrations {
struct v1: AsyncMigration {
func prepare(on database: Database) async throws {
try await database.schema(UserAccountModel.schema)
.id()
.field(UserAccountModel.FieldKeys.v1.email, .string, .required)
.field(UserAccountModel.FieldKeys.v1.password, .string, .required)
.unique(on: UserAccountModel.FieldKeys.v1.email)
.create()
}
func revert(on database: Database) async throws {
try await database.schema(UserAccountModel.schema).delete()
}
}
struct seed: AsyncMigration {
func prepare(on database: Database) async throws {
let email = "root@loacalhost.com"
let password = "changeMe1"
let user = UserAccountModel(email: email, password: try Bcrypt.hash(password))
try await user.create(on: database)
}
func revert(on database: Database) async throws {
try await UserAccountModel.query(on: database).delete()
}
}
}
與之前不同的是顽素,我們使用了unique
去約束了 email字段的唯一性咽弦。同時數(shù)據(jù)填充時,我們將密碼加密了胁出,敏感信息不應(yīng)該明文存儲型型,我們需要時刻保持警覺。
最后創(chuàng)建我們的UserModule
去使用數(shù)據(jù)遷移吧全蝶。
/// FILE: Sources/App/Modules/User/UserModule.swift
import Vapor
struct UserModule: ModuleInterface {
func boot(_ app: Application) throws {
app.migrations.add(UserMigrations.v1())
app.migrations.add(UserMigrations.seed())
}
}
不要忘記把 UserModule
添加到配置文件了闹蒜。
// configures your application
public func configure(_ app: Application) throws {
// ...
/// setup modules
let modules: [ModuleInterface] = [
WebModule(),
BlogModule(),
UserModule()
]
for module in modules {
try module.boot(app)
}
/// use automatic database migration
try app.autoMigrate().wait()
}
現(xiàn)在,如果你運行該應(yīng)用程序抑淫,新的用戶表會創(chuàng)建绷落,并且包含root
帳號
Sessions
首先,在配置文件丈冬,我們配置應(yīng)用的Sessions
// configures your application
public func configure(_ app: Application) throws {
// ...
/// setup Sessions
app.sessions.use(.fluent)
app.migrations.add(SessionRecord.migration)
app.middleware.use(app.sessions.middleware)
//...
}
第一行代碼表示我們使用的是Fluent Session進行存儲嘱函,第二行是添加一個底層的 _fluent_sessions表甘畅。
最后一行代碼我們很熟悉埂蕊,是添加了 app.sessions.middleware中間件,這個中間件會嘗試從客戶端的的cookie中讀取session疏唾。
登錄頁面
前面我們已經(jīng)有了用戶數(shù)據(jù)表存儲我們的用戶數(shù)據(jù)了蓄氧,當(dāng)然還需要一個登錄表單頁去輸入驗證。我們開始搭建這個頁面吧槐脏。跟之前一樣喉童,我們需要一個模版和context
/// FILE: Sources/App/Modules/User/Templates/Contexts/UserLoginContext.swift
struct UserLoginContext {
let icon: String
let title: String
let message: String
let email: String?
let password: String?
let error: String?
init(icon: String,
title: String,
message: String,
email: String? = nil,
password: String? = nil,
error: String? = nil) {
self.icon = icon
self.title = title
self.message = message
self.email = email
self.password = password
self.error = error
}
}
登錄頁面比較簡單,會用到 Form元素去搭建表單顿天。使用2個 input標(biāo)簽進行輸入堂氯。
/// FILE: Sources/App/Modules/User/Templates/Html/UserLoginTemplate.swift
import Vapor
import SwiftHtml
import SwiftSgml
struct UserLoginTemplate: TemplateRepresentable {
var context: UserLoginContext
@TagBuilder
func render(_ req: Request) -> Tag {
WebIndexTemplate.init(.init(title: context.title)) {
Div {
Section {
P (context.icon)
H1(context.title)
P(context.message)
}
.class("lead")
Form {
if let error = context.error {
Section {
Span(error)
.class(error)
}
}
Section {
Label("Email:")
.for("email")
Input()
.key("email")
.type(.email)
.value(context.email)
.class("field")
}
Section {
Label("Password:")
.for("password")
Input()
.key("password")
.type(.password)
.value(context.password)
.class("field")
}
Section {
Input()
.type(.submit)
.value("Sign in")
.class("submit")
}
}
.action("/sign-in/")
.method(.post)
}
.id("user-login")
.class("container")
}
.render(req)
}
}
這是一個面向用戶的前端登錄表單蔑担,所以我們需要套用Index模板。
現(xiàn)在咽白,如果我們渲染這個模板并按下提交按鈕啤握,瀏覽器將使用表單字段的 URLEncoded 內(nèi)容向 /sign-in/ 端點執(zhí)行 POST 請求。 所以我們需要兩個端點來處理這些事情晶框。 一個端點將負(fù)責(zé)表單呈現(xiàn)排抬,另一個端點將通過 POST 請求處理表單提交。
/// FILE: Sources/App/Modules/User/Controllers/UserFrontendController.swift
import Vapor
struct UserFrontendController {
func signInView(_ req: Request) async throws -> Response {
let template = UserLoginTemplate(context: .init(icon: "??", title: "Sign in", message: "Please log in with your existing account"))
return req.templates.renderHtml(template)
}
func signInAction(_ req: Request) async throws -> Response {
// @TODO: handle sign in action
return try await signInView(req)
}
}
再把這兩個endpoints注冊在UserRouter.swift
/// FILE: Sources/App/Modules/User/UserRouter.swift
import Vapor
struct UserRouter: RouteCollection {
let frontendController = UserFrontendController()
func boot(routes: RoutesBuilder) throws {
routes.get("sign-in", use: frontendController.signInView)
routes.post("sign-in", use: frontendController.signInAction)
}
}
同樣的授段,還需要在UserModule.swift
調(diào)用 boot方法使這兩個路由工作
/// FILE: Sources/App/Modules/User/UserModule.swift
import Vapor
struct UserModule: ModuleInterface {
let router = UserRouter()
func boot(_ app: Application) throws {
app.migrations.add(UserMigrations.v1())
app.migrations.add(UserMigrations.seed())
try router.boot(routes: app.routes)
}
}
現(xiàn)在蹲蒲,如果我們訪問 /sign-in/ 端點,我們應(yīng)該會看到一個簡單的登錄表單頁侵贵,但因為我們沒有正確處理登錄操作届搁,所以還不能進行登錄, 下一步我們需要處理登錄驗證窍育。
authenticator
authenticator是一個中間件咖祭,如果請求中存在登錄必要的數(shù)據(jù),它將嘗試使用authenticatable對象登錄蔫骂。 身份驗證數(shù)據(jù)存儲在 req.auth 屬性中么翰。
應(yīng)該注意 req.auth 變量不等同于 req.session 屬性。 它們服務(wù)于不同的目的辽旋。 可以將 SessionAuthenticatable 對象存儲在 req.session 變量中浩嫌。 這些對象將被持久化,并在客戶端使用Session cookie 來跟蹤當(dāng)前Session补胚。 這允許我們在用戶通過登錄表單正確驗證后保持登錄狀態(tài)码耐。
/// FILE: Sources/App/Framework/AuthenticatedUser.swift
import Vapor
public struct AuthenticatedUser {
public let id: UUID
public let email: String
}
extension AuthenticatedUser: SessionAuthenticatable {
public var sessionID: UUID { id }
}
基于憑據(jù)的身份驗證是指用戶必須提供正確的電子郵件和密碼組合。 然后我們可以使用這些值在 accounts 表中進行查找溶其,以檢查它是否是現(xiàn)有記錄骚腥,并查看字段是否匹配。 如果一切正確瓶逃,我們可以對用戶進行身份驗證束铭,這意味著登錄嘗試成功。 我們將實現(xiàn)一個可用于執(zhí)行此操作的獨立 UserCredentialsAuthenticator厢绝。
/// FILE: Sources/App/Modules/User/Authenticators/UserCredentialsAuthenticator.swift
import Vapor
import Fluent
struct UserCredentialsAuthenticator: AsyncCredentialsAuthenticator {
struct Credentials: Content {
let email: String
let password: String
}
func authenticate(credentials: Credentials, for request: Request) async throws {
guard let user = try await UserAccountModel
.query(on: request.db)
.filter(\.$email == credentials.email)
.first()
else { return }
do {
guard try Bcrypt.verify(credentials.password, created: user.password) else { return }
request.auth.login(AuthenticatedUser(id: user.id!, email: user.email))
}
catch {
// do nothing
}
}
}
輸入是一個 Content 對象契沫,它是 Vapor 對可以從傳入請求解碼或編碼為響應(yīng)的內(nèi)容的定義。 Vapor 有多種內(nèi)容類型昔汉,既有 JSON 也有 URLEncoded 內(nèi)容編碼器和解碼器懈万。 當(dāng)用戶按下提交按鈕時,HTML 表單正在發(fā)送一個 URLEncoded 數(shù)據(jù)。
驗證函數(shù)接收憑據(jù)并嘗試在數(shù)據(jù)庫中查找具有有效密碼的現(xiàn)有用戶会通。 如果我們找到一條記錄口予,我們可以使用之前創(chuàng)建的 AuthenticatedUser 對象調(diào)用 req.auth.login 方法。 這會將我們的用戶信息保存到身份驗證存儲中涕侈,其余的請求處理程序可以檢查是否存在現(xiàn)有的 AuthenticatedUser苹威,這將指示是否有登錄用戶。
我們將在我們的 post /sign-in/ 路由中使用這個身份驗證器
/// FILE: Sources/App/Modules/User/UserRouter.swift
import Vapor
struct UserRouter: RouteCollection {
let frontendController = UserFrontendController()
func boot(routes: RoutesBuilder) throws {
routes.get("sign-in", use: frontendController.signInView)
routes
.grouped(UserCredentialsAuthenticator())
.post("sign-in", use: frontendController.signInAction)
}
}
我們還應(yīng)該更新用戶前端控制器以實際實現(xiàn)我們的 signInAction 方法驾凶。
/// FILE: Sources/App/Modules/User/Controllers/UserFrontendController.swift
import Vapor
struct UserFrontendController {
struct Input: Decodable {
let email: String?
let password: String?
}
func renderSignInView(_ req: Request, _ input: Input? = nil, _ error: String? = nil) -> Response {
let template = UserLoginTemplate(context: .init(icon: "??",
title: "Sign in",
message: "Please log in with your existing account",
email: input?.email,
password: input?.password,
error: error))
return req.templates.renderHtml(template)
}
func signInView(_ req: Request) async throws -> Response {
return renderSignInView(req)
}
func signInAction(_ req: Request) async throws -> Response {
/// the user is authenticated, we can store the user data inside the session too
if let user = req.auth.get(AuthenticatedUser.self) {
req.session.authenticate(user)
return req.redirect(to: "/")
}
/// if the user credentials were wrong we render the form again with an error message
let input = try req.content.decode(Input.self)
return renderSignInView(req, input, "Invalid email or password.")
}
}
了解action 方法內(nèi)部的調(diào)用順序非常重要牙甫。首先,UserCredentialsAuthenticator將完成其工作调违,如果輸入正常窟哺,它將驗證用戶。到登錄處理程序?qū)⒈徽{(diào)用時技肩, req.auth 屬性應(yīng)該包含一個 AuthenticatedUser 對象且轨。我們可以通過調(diào)用 req.auth.get(AuthenticatedUser.self) 方法來檢查它。這將返回一個可選的用戶對象虚婿。
如果沒有經(jīng)過身份驗證的用戶旋奢,我們應(yīng)該解碼提交的值并使用登錄表單響應(yīng)錯誤消息,該錯誤消息將指示登錄嘗試不成功然痊。如果用戶存在至朗,我們可以將用戶保存到當(dāng)前session storage中。這可以通過 req.session.authenticate 函數(shù)來完成剧浸。在此之后锹引,我們可以將瀏覽器重定向到主屏幕,我們可以開始查看經(jīng)過身份驗證的用戶的session對象唆香。
現(xiàn)在我們可以通過登錄表單對用戶進行身份驗證并將其保存到session storage中嫌变,我們需要一種從session storage中檢索相同用戶的方法。通過這種方式躬它,我們將能確定用戶之前是否已登錄腾啥,并且我們可以在 Web 前端顯示一些與用戶相關(guān)的數(shù)據(jù)。
SessionAuthenticator 可以檢查session cookie 的值并根據(jù)該標(biāo)識符對用戶進行身份驗證冯吓。 Cookie 在 HTTP headers 中倘待,authenticator 協(xié)議會自動解析請求中的session identifier。
UserSessionAuthenticator 應(yīng)該檢查數(shù)據(jù)庫是否存在與給定 SessionID 關(guān)聯(lián)的有效用戶桑谍,如果存在則登錄返回的用戶延柠。
/// FILE: Sources/App/Modules/User/Authenticators/UserSessionAuthenticator.swift
import Vapor
import Fluent
struct UserSessionAuthenticator: AsyncSessionAuthenticator {
typealias User = AuthenticatedUser
func authenticate(sessionID: User.SessionID, for request: Request) async throws {
guard let user = try await UserAccountModel.find(sessionID, on: request.db) else {
return
}
request.auth.login(AuthenticatedUser(id: user.id!, email: user.email))
}
}
現(xiàn)在我們有了UserSessionAuthenticator
,我們將把它添加為一個全局中間件锣披,所以它會在我們注冊的每個路由處理程序之前被調(diào)用。
/// FILE: Sources/App/Modules/User/UserModule.swift
import Vapor
struct UserModule: ModuleInterface {
let router = UserRouter()
func boot(_ app: Application) throws {
app.migrations.add(UserMigrations.v1())
app.migrations.add(UserMigrations.seed())
app.middleware.use(UserSessionAuthenticator())
try router.boot(routes: app.routes)
}
}
我們應(yīng)該更新index
模板并檢查是否有登錄用和支持登錄操作, 這可以通過 req.auth 屬性來完成雹仿。
//...
Div {
A("Home")
.href("/")
.class("selected", req.url.path == "/")
A("Blog")
.href("/blog/")
.class("selected", req.url.path == "/blog/")
A("About")
.href("#")
.onClick("javascript:about();")
if req.auth.has(AuthenticatedUser.self) {
A("Sign Out")
.href("/sign-out/")
} else {
A("Sign In")
.href("/sign-in/")
}
}
.class("menu-items")
//...
實現(xiàn)登出端點很簡單增热,我們只需要注銷 AuthenticatedUser 并從Session storage存儲中取消身份驗證。 最后胧辽,我們可以在成功注銷操作后簡單地重定向回主頁峻仇。
struct UserFrontendController {
//...
func signOut(req: Request) throws -> Response {
req.auth.logout(AuthenticatedUser.self)
req.session.unauthenticate(AuthenticatedUser.self)
return req.redirect(to: "/")
}
}
最后回到 UserRouter
注冊signOut 端點。
/// FILE: Sources/App/Modules/User/UserRouter.swift
import Vapor
struct UserRouter: RouteCollection {
let frontendController = UserFrontendController()
func boot(routes: RoutesBuilder) throws {
routes.get("sign-in", use: frontendController.signInView)
routes
.grouped(UserCredentialsAuthenticator())
.post("sign-in", use: frontendController.signInAction)
routes.get("sign-out", use: frontendController.signOut)
}
}
現(xiàn)在可以啟動服務(wù)器并嘗試使用預(yù)先創(chuàng)建的用戶帳戶登錄邑商。
總結(jié)
在本篇文章中摄咆,我們搭建了新的用戶模塊,運用了身體驗證的中間件人断,完成了一套帳號登錄的流程吭从。