Created
August 31, 2023 15:23
-
-
Save saroar/dd4b954c552579357788bcbda586225c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import BSON | |
import Vapor | |
import Foundation | |
public struct ClientAppModel: Codable, Equatable { | |
static let collectionName = "client_apps" | |
var id: ObjectId | |
var ownerId: ObjectId | |
var clientId: ObjectId | |
var clientSecret: String | |
var appName: String | |
var platform: PlatformType | |
var bundleIdentifier: String | |
var subscription: SubscriptionType = .Free | |
var billingType: BillingType = .Monthly | |
var useCaseType: UseCaseType = .B2C | |
var createdAt: Date? | |
var updatedAt: Date? | |
init( | |
id: ObjectId, | |
ownerId: ObjectId, | |
clientId: ObjectId, | |
clientSecret: String, | |
appName: String, | |
platform: PlatformType, | |
bundleIdentifier: String, | |
subscription: SubscriptionType = .Free, | |
billingType: BillingType = .Monthly, | |
useCaseType: UseCaseType = .B2C, | |
createdAt: Date? = nil, | |
updatedAt: Date? = nil | |
) { | |
self.id = id | |
self.ownerId = ownerId | |
self.clientId = clientId | |
self.clientSecret = clientSecret | |
self.appName = appName | |
self.platform = platform | |
self.bundleIdentifier = bundleIdentifier | |
self.subscription = subscription | |
self.billingType = billingType | |
self.useCaseType = useCaseType | |
self.createdAt = createdAt | |
self.updatedAt = updatedAt | |
} | |
enum CodingKeys: String, CodingKey { | |
case id = "_id" | |
case ownerId | |
case clientId | |
case clientSecret | |
case appName | |
case platform | |
case bundleIdentifier | |
case subscription | |
case billingType | |
case useCaseType | |
case createdAt | |
case updatedAt | |
} | |
} | |
extension ClientAppModel: Content {} | |
import Vapor | |
import MongoKitten | |
extension ClientAppModel { | |
static func query(_ request: Request) -> MongoCollection { | |
return request.application.mongoDB[Self.collectionName] | |
} | |
} | |
enum PlatformType: Codable, Equatable { | |
case web | |
case mobile(OSType) | |
} | |
enum OSType: String, Codable, Equatable { | |
case iOS | |
case macOS | |
case android | |
case blackberry | |
case windows | |
} | |
enum SubscriptionType: Codable, Equatable { | |
case Free | |
case ESSENTIALS(BillingType) | |
case PROFESSIONAL(BillingType) | |
case ENTERPRISE(BillingType) | |
} | |
enum BillingType: String, Codable, Equatable { | |
case Monthly, Yearly | |
} | |
enum UseCaseType: String, Codable, Equatable { | |
case B2C, B2B | |
} | |
import Crypto | |
extension ClientAppModel { | |
mutating func encryptClientSecret(using key: SymmetricKey) throws { | |
let secretData = Data(clientSecret.utf8) | |
let sealedBox = try AES.GCM.seal(secretData, using: key) | |
var encryptedData = Data() | |
encryptedData += sealedBox.nonce | |
encryptedData += sealedBox.ciphertext | |
encryptedData += sealedBox.tag | |
self.clientSecret = encryptedData.base64EncodedString() | |
} | |
mutating func decryptClientSecret(using key: SymmetricKey) throws { | |
let encryptedData = Data(base64Encoded: clientSecret)! | |
let sealedBox = try AES.GCM.SealedBox(combined: encryptedData) | |
let decryptedData = try AES.GCM.open(sealedBox, using: key) | |
self.clientSecret = String(data: decryptedData, encoding: .utf8)! | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment