-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.txt
More file actions
196 lines (152 loc) · 5.75 KB
/
Test.txt
File metadata and controls
196 lines (152 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//
// File.swift
//
//
// Created by Bhesh Raj Regmi on 08/03/2023.
//
import Vapor
import JWT
/*
// To test this route, first send the following request:
curl -v http://127.0.0.1:8080/me -H "Accept: application/json" -H "Authorization: Bearer test"
//This will cause UserBearerAuthenticator to authenticate the user. Once authenticated, UserSessionAuthenticator will persist the user's identifier in session storage and generate a cookie. Use the cookie from the response in a second request to the route.
curl --cookie "vapor-session=0PT5ast463n3jCuvWZfTVtHBV17dBYvqbh8LaYTKgzA=" http://127.0.0.1:8080/me
*/
//MARK: View Controller
struct AuthController {
func loginHandler(_ req: Request) async throws -> View {
do {
return try await req.view.render("login")
} catch {
throw Abort(.internalServerError, reason: "Failed to render login template.")
}
}
//func loginSubmitHandler(_ req: Request) async throws -> ClientTokenReponse {
func loginSubmitHandler(_ req: Request) async throws -> View {
// Validate provided credential for user
let username: String = try req.content.get(at: "username")
let password: String = try req.content.get(at: "password")
print("username \(username)")
//Dummy Userid, usually obtained from DB
let userId = UUID()
// Get userId for provided user
let payload = try SessionToken(userId: userId)
// Generate JWT client token
let token = try req.jwt.sign(payload)
//Use this if token has to be sent as response body
// return ClientTokenReponse(token: try req.jwt.sign(payload))
// Set cookie with JWT client token
let cookie = HTTPCookies.Value(string: token, expires: .init(timeIntervalSinceNow: 3600))
// req.response.headers.setCookie(HTTPCookies)
// do something with the username and password, like check if they are valid
// return req.redirect(to: "/dashboard")
return try await req.view.render("profile", ["user": UserDetail(name: "Everest", address: "Asia")])
}
func dashboardHandler(_ req: Request)async throws -> View {
return try await req.view.render("dashboard", ["user": UserDetail(name: "Everest2", address: "Asia2")])
}
func currentUserHandler(_ req: Request) async throws -> UserDetail {
let user = try req.auth.require(User.self)
print("SessionId: \(user.sessionID)")
return UserDetail(name: "Vaporrrr", address: "Cali")
}
}
//MARK: Middleware
// MARK: - Bearer authenticator
// This is a more robust Bearer Authenticator
struct UserBearerAuthenticator: AsyncBearerAuthenticator {
func authenticate(bearer: BearerAuthorization, for request: Request) async throws {
// Dummy token and email for testing
if bearer.token == "test" {
let user = User(email: "hello@vapor.codes")
request.auth.login(user)
}
}
}
// MARK: - Session authenticator
struct UserSessionAuthenticator: AsyncSessionAuthenticator {
typealias User = App.User
func authenticate(sessionID: String, for request: Request) async throws {
let user = User(email: sessionID)
request.auth.login(user)
}
}
//MARK: Models
// JWT payload structure.
struct TestPayload: JWTPayload {
// Maps the longer Swift property names to the
// shortened keys used in the JWT payload.
enum CodingKeys: String, CodingKey {
case subject = "sub"
case expiration = "exp"
case isAdmin = "admin"
}
// The "sub" (subject) claim identifies the principal that is the
// subject of the JWT.
var subject: SubjectClaim
// The "exp" (expiration time) claim identifies the expiration time on
// or after which the JWT MUST NOT be accepted for processing.
var expiration: ExpirationClaim
// Custom data.
// If true, the user is an admin.
var isAdmin: Bool
// Run any additional verification logic beyond
// signature verification here.
// Since we have an ExpirationClaim, we will
// call its verify method.
func verify(using signer: JWTSigner) throws {
try self.expiration.verifyNotExpired()
}
}
// Example JWT payload.
struct SessionToken: Content, Authenticatable, JWTPayload {
// Constants
let expirationTime: TimeInterval = 60 * 15
// Token Data
var expiration: ExpirationClaim
var userId: UUID
init(userId: UUID) {
self.userId = userId
self.expiration = ExpirationClaim(value: Date().addingTimeInterval(expirationTime))
}
init(user: User) throws {
self.userId = try user.requireID()
self.expiration = ExpirationClaim(value: Date().addingTimeInterval(expirationTime))
}
func verify(using signer: JWTSigner) throws {
try expiration.verifyNotExpired()
}
}
struct User: Authenticatable, Content {
//Generates random dummy UUID
// var id: String = UUID().uuidString
var id: UUID?
var email: String
init(id: UUID? = nil, email: String) {
self.id = id
self.email = email
}
}
extension User {
func requireID() throws -> UUID {
guard let id = self.id else {
throw Abort(.internalServerError, reason: "ID is missing for user")
}
return id
}
}
// To conform to SessionAuthenticatable, you will need to specify a sessionID. This is the value that will be stored in the session data and must uniquely identify the user.
extension User: SessionAuthenticatable {
var sessionID: String {
self.email
}
}
struct UserDetail: Content {
// var name: String
var name: String
var address: String
}
// This is to send the token response after login
struct ClientTokenReponse: Content {
var token: String
}