impl session tokens

This commit is contained in:
Andrew Glaze
2025-05-16 11:56:30 -04:00
parent 0fb66832e1
commit a508348fac
6 changed files with 113 additions and 8 deletions

View File

@@ -53,7 +53,7 @@ struct AuthController: RouteCollection {
}
@Sendable
func loginDevice(req: Request) async throws -> Response {
func loginDevice(req: Request) async throws -> LoginDeviceRes {
let body = try req.content.decode(LoginDeviceReq.self, as: .json)
let idpAlias = generateIdpAlias(appId: body.appId, deviceId: body.deviceId, serialNo: body.serialNo)
@@ -72,9 +72,38 @@ struct AuthController: RouteCollection {
}
guard let account = account else {
return Response(status: .badRequest, body: "{\"error\": \"Bad Request\", \"message\": \"Invalid playerId provided.\"}")
throw Abort(.badRequest)
}
return Response(status: .notImplemented)
let zatExpiry = Date.now.advanced(by: 43200)
let zrtExpiry = Date.now.advanced(by: 2592000)
let zatTokenJWT = generateToken(accountId: try account.requireID(), expires: zatExpiry, type: .ZAT)
let zrtTokenJWT = generateToken(accountId: try account.requireID(), expires: zrtExpiry, type: .ZRT)
let zatToken = try await req.jwt.sign(zatTokenJWT)
let zrtToken = try await req.jwt.sign(zrtTokenJWT)
let res = LoginDeviceRes(
zatExpiryTime: Int(zatExpiry.timeIntervalSince1970) * 1000,
zrtExpiryTime: Int(zrtExpiry.timeIntervalSince1970) * 1000,
firstLogin: true,
externalToken: "",
zat: zatToken,
zrt: zrtToken,
player: Player(
idpId: account.idpId,
appId: account.appId,
playerId: String(try account.requireID()),
pushOption: PushOptionResponse(night: "n", player: "n"),
regTime: Int(account.regDate.timeIntervalSince1970),
idpAlias: idpAlias,
firstLoginTime: Int(account.firstLogin.timeIntervalSince1970),
status: account.status
)
)
return res
}
}
@@ -134,11 +163,8 @@ struct LoginDeviceRes: Content {
struct Player: Content {
let idpId: String
let appId: String
let lang: String
let playerId: String
let agreement: AgreementResponse
let pushOption: PushOptionResponse
let lastLoginTime: Int
let regTime: Int
let idpAlias: String
let firstLoginTime: Int
@@ -158,3 +184,9 @@ struct PushOptionResponse: Content {
let night: String
let player: String
}
enum SessionType: Int {
case ZAT = 0
case ZRT = 1
case VIEWER = 2
}