-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseconnection.txt
68 lines (57 loc) · 2.14 KB
/
Databaseconnection.txt
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
//
// File.swift
//
//
// Created by Bhesh Raj Regmi on 09/03/2023.
//
import Vapor
import SwiftOracle
/*
/*
//.env file in root folder
TNS_NAME=Docker_Oracle
DATABASE_USER=apple
DATABASE_PASSW=passwordnew
LOG_LEVEL=debug, trace
*/
// MARK: - Basic authenticator
// This is a more robust Basic Authenticator, It relies on storing hashed passowrds and salts in the database.
struct UserAuthenticator: AsyncBasicAuthenticator {
typealias User = App.User
func authenticate(
basic: BasicAuthorization,
for request: Request
) async throws {
if isAuthenticated(basicAuth: basic, for: request) {
request.auth.login(User(name: "vapor"))
}
}
func isAuthenticated(basicAuth: BasicAuthorization, for req: Request) -> Bool {
req.logger.debug("starting user authentication in the database")
// getting the connection pool descriptor from the request
let conn = req.oraConnPool.pool.getConnection(tag: "", autoCommit: true)
// making sure to return the connection upon exit
defer {
req.oraConnPool.pool.returnConnection(conn: conn)
}
let cursor: SwiftOracle.Cursor
do {
cursor = try conn.cursor()
//SQL statement hashes the input password
let sqlStr = "select 1 as authed from test_username where user_name = :u and password = dbms_crypto.hash(utl_i18n.string_to_raw(:p , 'AL32UTF8'), 6)"
req.logger.debug("executing a query")
try cursor.execute(sqlStr, params: [":u": BindVar(basicAuth.username), ":p": BindVar(basicAuth.password)])
// fetch the data
guard let row = cursor.fetchone(), row["AUTHED"]?.int == 1 else {
req.logger.debug("username and password combination did not match any record in the database")
return false
}
req.logger.debug("username and password matched to a record in the database")
return true // authenticated
} catch {
req.logger.error(Logger.Message(stringLiteral: error.localizedDescription))
return false
}
}
}
*/