From e24dc9b69732cd8eb45e4a7837f429e8eab72b3a Mon Sep 17 00:00:00 2001 From: rpm-mcarman <65228808+rpm-mcarman@users.noreply.github.com> Date: Tue, 2 Apr 2024 14:14:39 -0400 Subject: [PATCH] Commit for Environment Variables --- .../AuthControllers/cntrlUser.swift | 18 +++---- .../BaseControllers/cntrlComparisonType.swift | 49 +++++++++++++++++-- .../BaseControllers/cntrlContactType.swift | 49 +++++++++++++++++-- .../EncryptionController.swift | 29 +++++++++-- Sources/App/configure.swift | 3 ++ env | 1 - 6 files changed, 124 insertions(+), 25 deletions(-) delete mode 100644 env diff --git a/Sources/App/Controllers/AuthControllers/cntrlUser.swift b/Sources/App/Controllers/AuthControllers/cntrlUser.swift index c49b7a8..2db1a49 100644 --- a/Sources/App/Controllers/AuthControllers/cntrlUser.swift +++ b/Sources/App/Controllers/AuthControllers/cntrlUser.swift @@ -21,6 +21,7 @@ struct cntrlUser: RouteCollection { rts.group(":id") { todo in rts.delete(use: delete) } + rts.get("api", use: api) //let encryptionKey = SymmetricKey(data: Environment.get("ENCRYPTION_KEY")!.data(using: .utf8)!) // rts.get(use: "login"){ @@ -37,22 +38,17 @@ struct cntrlUser: RouteCollection { // } } + func api(req: Request) async throws -> String{ + return Environment.get("API_KEY")! + } + func login(req: Request) async throws -> mdlUser{ guard let dataQuery = req.parameters.get("credentials") else{ throw Abort(.badRequest) } - let base64Key = "rv0ywT7Ygwh01jP1dQWBP39ogjq5ladO+EoNUGcBVq0=" // Replace with your actual key - guard let keyData = Data(base64Encoded: base64Key) else { - fatalError("Invalid Base64 key data") - } - let data = Data(base64Encoded: dataQuery) - let encryptionKey = SymmetricKey(data: keyData) - let sealedBox = try AES.GCM.SealedBox(combined: data!) - let decryptedData = try AES.GCM.open(sealedBox, using: encryptionKey) - guard let decryptedString = String(data: decryptedData, encoding: .utf8) else{ - throw Abort(.internalServerError, reason: "Fialed to decode decrypted data.") - } + let decryptedString = try CustomCrypto.DecryptString(input: dataQuery) + let credentials = decryptedString.components(separatedBy: ":") guard credentials.count == 2 else { throw Abort(.badRequest, reason: "Invalid credential format.") diff --git a/Sources/App/Controllers/BaseControllers/cntrlComparisonType.swift b/Sources/App/Controllers/BaseControllers/cntrlComparisonType.swift index f2f19ea..956b388 100644 --- a/Sources/App/Controllers/BaseControllers/cntrlComparisonType.swift +++ b/Sources/App/Controllers/BaseControllers/cntrlComparisonType.swift @@ -1,8 +1,49 @@ // -// File 2.swift -// +// cntrlComparisonType.swift // -// Created by Michiel Carman on 4/2/24. // +// Created by Michiel Carman on 4/1/24. +// + +import Fluent +import Vapor -import Foundation +struct cntrlComparisonType: RouteCollection { + func boot(routes: RoutesBuilder) throws { + let rts = routes.grouped("comparisonTypes") + rts.get(use: index) + rts.post(use: create) + rts.put(use: update) + rts.group(":id") { rt in + rts.delete(use: delete) + } + } + + func index(req: Request) async throws -> [mdlComparisonType] { + try await mdlComparisonType.query(on: req.db).all() + } + + func create(req: Request) async throws -> mdlComparisonType { + let obj = try req.content.decode(mdlComparisonType.self) + try await obj.save(on: req.db) + return obj + } + func update(req: Request) async throws -> mdlComparisonType{ + let obj = try req.content.decode(mdlComparisonType.self) + let dbobj = try await mdlComparisonType.query(on: req.db).filter(\.$id == obj.id!).first()! + + dbobj.updatedate = Date() + dbobj.updateuserid = -1 + try await dbobj.update(on: req.db) + return dbobj + + } + + func delete(req: Request) async throws -> HTTPStatus { + guard let obj = try await mdlComparisonType.find(req.parameters.get("id"), on: req.db) else { + throw Abort(.notFound) + } + try await obj.delete(on: req.db) + return .noContent + } +} diff --git a/Sources/App/Controllers/BaseControllers/cntrlContactType.swift b/Sources/App/Controllers/BaseControllers/cntrlContactType.swift index d0b8c0e..fa0a821 100644 --- a/Sources/App/Controllers/BaseControllers/cntrlContactType.swift +++ b/Sources/App/Controllers/BaseControllers/cntrlContactType.swift @@ -1,8 +1,49 @@ // -// File 3.swift -// +// cntrlContactType.swift // -// Created by Michiel Carman on 4/2/24. // +// Created by Michiel Carman on 4/1/24. +// + +import Fluent +import Vapor -import Foundation +struct cntrlContactType: RouteCollection { + func boot(routes: RoutesBuilder) throws { + let rts = routes.grouped("contactTypes") + rts.get(use: index) + rts.post(use: create) + rts.put(use: update) + rts.group(":id") { rt in + rts.delete(use: delete) + } + } + + func index(req: Request) async throws -> [mdlContactType] { + try await mdlContactType.query(on: req.db).all() + } + + func create(req: Request) async throws -> mdlContactType { + let obj = try req.content.decode(mdlContactType.self) + try await obj.save(on: req.db) + return obj + } + func update(req: Request) async throws -> mdlContactType{ + let obj = try req.content.decode(mdlContactType.self) + let dbobj = try await mdlContactType.query(on: req.db).filter(\.$id == obj.id!).first()! + + dbobj.updatedate = Date() + dbobj.updateuserid = -1 + try await dbobj.update(on: req.db) + return dbobj + + } + + func delete(req: Request) async throws -> HTTPStatus { + guard let obj = try await mdlContactType.find(req.parameters.get("id"), on: req.db) else { + throw Abort(.notFound) + } + try await obj.delete(on: req.db) + return .noContent + } +} diff --git a/Sources/App/Controllers/GlobalFunctions/EncryptionController.swift b/Sources/App/Controllers/GlobalFunctions/EncryptionController.swift index f917d77..e920f6d 100644 --- a/Sources/App/Controllers/GlobalFunctions/EncryptionController.swift +++ b/Sources/App/Controllers/GlobalFunctions/EncryptionController.swift @@ -6,10 +6,29 @@ // import Foundation +import Vapor import Crypto -//struct CustomCrypto{ -// public static func DecryptString(input: String) -> [String]{ -// -// } -//} +enum CryptoError: Error{ + case badRequest +} + +struct CustomCrypto{ + public static func DecryptString(input: String) throws -> String{ + let apikey = Environment.get("API_KEY") + let base64Key = apikey! // Replace with your actual key + guard let keyData = Data(base64Encoded: base64Key) else { + fatalError("Invalid Base64 key data") + } + let data = Data(base64Encoded: input) + let encryptionKey = SymmetricKey(data: keyData) + let sealedBox = try! AES.GCM.SealedBox(combined: data!) + let decryptedData = try! AES.GCM.open(sealedBox, using: encryptionKey) + guard let decryptedString = String(data: decryptedData, encoding: .utf8) else{ + throw CryptoError.badRequest + } + return decryptedString + } +} + + diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index 7baa775..7595aae 100644 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -2,6 +2,7 @@ import NIOSSL import Fluent import FluentPostgresDriver import Vapor +import Foundation // configures your application public func configure(_ app: Application) async throws { @@ -105,3 +106,5 @@ public func configure(_ app: Application) async throws { // register routes try routes(app) } + + diff --git a/env b/env deleted file mode 100644 index e07460d..0000000 --- a/env +++ /dev/null @@ -1 +0,0 @@ -API_KEY=SOMEINFO