Vapor 框架學(xué)習(xí)記錄(4)Sessions 和驗證

在本篇中棚愤,我們將專注于構(gòu)建基于session的 Web 身份驗證層。 用戶將能夠使用表單登錄,并且已經(jīng)登錄的用戶將在session cookie 和使用 Fluent 的持久session存儲的幫助下被檢測到。 我們會使用自定義身份驗證器中間件茎杂,通過sessioncredentials對用戶進行身份驗證珍德。

User module

用戶模塊將負(fù)責(zé)用戶管理和認(rèn)證摔竿。 請創(chuàng)建一個新的用戶模塊目錄結(jié)構(gòu)真慢,就像我們?yōu)椴┛湍K所做的那樣。 我們將需要一個 User 文件夾榕吼,一個包含 MigrationsModels 目錄的 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)識符對用戶進行身份驗證冯吓。 CookieHTTP 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)建的用戶帳戶登錄邑商。

登錄.png

總結(jié)

在本篇文章中摄咆,我們搭建了新的用戶模塊,運用了身體驗證的中間件人断,完成了一套帳號登錄的流程吭从。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市恶迈,隨后出現(xiàn)的幾起案子涩金,更是在濱河造成了極大的恐慌,老刑警劉巖暇仲,帶你破解...
    沈念sama閱讀 221,430評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件步做,死亡現(xiàn)場離奇詭異,居然都是意外死亡奈附,警方通過查閱死者的電腦和手機全度,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,406評論 3 398
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來斥滤,“玉大人讼载,你說我怎么就攤上這事≈械” “怎么了咨堤?”我有些...
    開封第一講書人閱讀 167,834評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長漩符。 經(jīng)常有香客問我一喘,道長,這世上最難降的妖魔是什么嗜暴? 我笑而不...
    開封第一講書人閱讀 59,543評論 1 296
  • 正文 為了忘掉前任凸克,我火速辦了婚禮,結(jié)果婚禮上闷沥,老公的妹妹穿的比我還像新娘萎战。我一直安慰自己,他們只是感情好舆逃,可當(dāng)我...
    茶點故事閱讀 68,547評論 6 397
  • 文/花漫 我一把揭開白布蚂维。 她就那樣靜靜地躺著戳粒,像睡著了一般。 火紅的嫁衣襯著肌膚如雪虫啥。 梳的紋絲不亂的頭發(fā)上蔚约,一...
    開封第一講書人閱讀 52,196評論 1 308
  • 那天,我揣著相機與錄音涂籽,去河邊找鬼苹祟。 笑死,一個胖子當(dāng)著我的面吹牛评雌,可吹牛的內(nèi)容都是我干的树枫。 我是一名探鬼主播,決...
    沈念sama閱讀 40,776評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼景东,長吁一口氣:“原來是場噩夢啊……” “哼砂轻!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起耐薯,我...
    開封第一講書人閱讀 39,671評論 0 276
  • 序言:老撾萬榮一對情侶失蹤舔清,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后曲初,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體体谒,經(jīng)...
    沈念sama閱讀 46,221評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,303評論 3 340
  • 正文 我和宋清朗相戀三年臼婆,在試婚紗的時候發(fā)現(xiàn)自己被綠了抒痒。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,444評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡颁褂,死狀恐怖故响,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情颁独,我是刑警寧澤彩届,帶...
    沈念sama閱讀 36,134評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站誓酒,受9級特大地震影響樟蠕,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜靠柑,卻給世界環(huán)境...
    茶點故事閱讀 41,810評論 3 333
  • 文/蒙蒙 一寨辩、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧歼冰,春花似錦靡狞、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,285評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽甘穿。三九已至,卻和暖如春蕾各,著一層夾襖步出監(jiān)牢的瞬間扒磁,已是汗流浹背庆揪。 一陣腳步聲響...
    開封第一講書人閱讀 33,399評論 1 272
  • 我被黑心中介騙來泰國打工式曲, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人缸榛。 一個月前我還...
    沈念sama閱讀 48,837評論 3 376
  • 正文 我出身青樓吝羞,卻偏偏與公主長得像,于是被迫代替她去往敵國和親内颗。 傳聞我的和親對象是個殘疾皇子钧排,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,455評論 2 359

推薦閱讀更多精彩內(nèi)容