add Player model

This commit is contained in:
Andrew Glaze
2025-05-18 16:53:08 -04:00
parent a93308afb1
commit 62260ffc73
11 changed files with 390 additions and 11 deletions

View File

@@ -6,6 +6,7 @@ struct AuthController: RouteCollection {
routes.post("v4", "device", "accessToken", "create", use: self.createAccessToken)
routes.post("v3", "agreement", "getForLogin", use: self.loginAgreement)
routes.post("v4", "auth", "loginDevice", use: self.loginDevice)
routes.post("v3", "zat", "login", use: self.zatLogin)
}
@Sendable
@@ -67,14 +68,19 @@ struct AuthController: RouteCollection {
} else if let existingAccount = try await Account.query(on: req.db).filter(\.$idpId == idpId).first() {
account = existingAccount
} else {
let account = Account(appId: body.appId, idpAlias: idpAlias, idpCode: "zd3", idpId: idpId, status: "normal")
try await account.create(on: req.db)
account = Account(appId: body.appId, idpAlias: idpAlias, idpCode: "zd3", idpId: idpId, status: "normal")
try await account!.create(on: req.db)
}
guard let account = account else {
throw Abort(.badRequest)
}
if account.idpAlias != idpAlias {
account.idpAlias = idpAlias
try await account.save(on: req.db)
}
let zatExpiry = Date.now.advanced(by: 43200)
let zrtExpiry = Date.now.advanced(by: 2592000)
@@ -91,13 +97,17 @@ struct AuthController: RouteCollection {
externalToken: "",
zat: zatToken,
zrt: zrtToken,
player: Player(
player: LoginPlayer(
idpId: account.idpId,
appId: account.appId,
playerId: String(try account.requireID()),
agreemenet: nil,
pushOption: PushOptionResponse(night: "n", player: "n"),
regTime: Int(account.regDate.timeIntervalSince1970),
idpAlias: idpAlias,
idpCode: nil,
lang: nil,
lastLoginTime: nil,
firstLoginTime: Int(account.firstLogin.timeIntervalSince1970),
status: account.status
)
@@ -105,6 +115,110 @@ struct AuthController: RouteCollection {
return res
}
@Sendable
func zatLogin(req: Request) async throws -> LoginDeviceRes {
let body = try req.content.decode(ZatLoginReq.self, as: .json)
if let session = try? await req.jwt.verify(body.zat, as: SessionPayload.self) {
guard session.type == SessionType.ZAT.rawValue && session.accountId.value == body.playerId else {
throw Abort(.badRequest, reason: "Invalid zat provided.")
}
}
guard
let accountId = Int(body.playerId),
let account = try await Account.query(on: req.db)
.filter(\.$id == accountId)
.first()
else {
throw Abort(.badRequest, reason: "Invalid playerId")
}
account.lastLogin = Date.now
try await account.save(on: req.db)
let zatExpiry = Date.now.advanced(by: 43200)
let session = generateToken(accountId: try account.requireID(), expires: zatExpiry, type: SessionType.ZAT)
let zatToken = try await req.jwt.sign(session)
return LoginDeviceRes(
zatExpiryTime: Int(zatExpiry.timeIntervalSince1970) * 1000,
zrtExpiryTime: nil,
firstLogin: false,
externalToken: "",
zat: zatToken,
zrt: nil,
player: LoginPlayer(
idpId: account.idpId,
appId: account.appId,
playerId: String(accountId),
agreemenet: AgreementResponse(E001: "y", E002: "y", E006: "y", N002: "n", N003: "n", timestamp: "1717623430484"),
pushOption: PushOptionResponse(night: "n", player: "n"),
regTime: Int(account.regDate.timeIntervalSince1970) * 1000,
idpAlias: account.idpAlias,
idpCode: account.idpCode,
lang: body.lang,
lastLoginTime: Int(account.lastLogin.timeIntervalSince1970) * 1000,
firstLoginTime: Int(account.firstLogin.timeIntervalSince1970) * 1000,
status: account.status
)
)
}
}
struct ZatLoginReq: Content {
let adid: String
let appId: String
let appSecret: String
let appVer: String
let clientTime: Int
let country: String
let deviceId: String
let deviceModel: String
let fields: [String]
let gsiToken: Bool?
let lang: String
let loginType: String
let market: String
let network: String
let os: String
let playerId: String
let resume: Bool
let retryNo: Int?
let sdkVer: String
let telecom: String
let timezoneOffset: Int
let usimCountry: String?
let whiteKey: String
let zat: String
init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
adid = try container.decode(String.self, forKey: .adid)
appId = try container.decode(String.self, forKey: .appId)
appSecret = try container.decode(String.self, forKey: .appSecret)
appVer = try container.decode(String.self, forKey: .appVer)
clientTime = try container.decode(Int.self, forKey: .clientTime)
country = try container.decode(String.self, forKey: .country)
deviceId = try container.decode(String.self, forKey: .deviceId)
deviceModel = try container.decode(String.self, forKey: .deviceModel)
fields = try container.decode([String].self, forKey: .fields)
gsiToken = try container.decodeIfPresent(Bool.self, forKey: .gsiToken)
lang = try container.decode(String.self, forKey: .lang)
loginType = try container.decode(String.self, forKey: .loginType)
market = try container.decode(String.self, forKey: .market)
network = try container.decode(String.self, forKey: .network)
os = try container.decode(String.self, forKey: .os)
playerId = try container.decode(String.self, forKey: .playerId)
resume = try container.decode(Bool.self, forKey: .resume)
retryNo = try container.decodeIfPresent(Int.self, forKey: .retryNo)
sdkVer = try container.decode(String.self, forKey: .sdkVer)
telecom = try container.decode(String.self, forKey: .telecom)
timezoneOffset = try container.decode(Int.self, forKey: .timezoneOffset)
usimCountry = try container.decodeIfPresent(String.self, forKey: .usimCountry)
whiteKey = try container.decode(String.self, forKey: .whiteKey)
zat = try container.decode(String.self, forKey: .zat)
}
}
struct AccessTokenRes: Content {
@@ -152,21 +266,25 @@ struct LoginDeviceReq: Content {
struct LoginDeviceRes: Content {
let zatExpiryTime: Int
let zrtExpiryTime: Int
let zrtExpiryTime: Int?
let firstLogin: Bool
let externalToken: String
let zat: String
let zrt: String
let player: Player
let zrt: String?
let player: LoginPlayer
}
struct Player: Content {
struct LoginPlayer: Content {
let idpId: String
let appId: String
let playerId: String
let agreemenet: AgreementResponse?
let pushOption: PushOptionResponse
let regTime: Int
let idpAlias: String
let idpCode: String?
let lang: String?
let lastLoginTime: Int?
let firstLoginTime: Int
let status: String
}