| @@ -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.") | |||
| @@ -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 | |||
| } | |||
| } | |||
| @@ -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 | |||
| } | |||
| } | |||
| @@ -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 | |||
| } | |||
| } | |||
| @@ -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) | |||
| } | |||
| @@ -1 +0,0 @@ | |||
| API_KEY=SOMEINFO | |||