1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-12-23 00:57:13 +00:00

add primitives for Telegram Passport API

This commit is contained in:
InsanusMokrassar 2021-01-14 14:03:25 +06:00
parent 0846e816e9
commit c0ea479fe3
22 changed files with 456 additions and 0 deletions

View File

@ -357,3 +357,13 @@ const val forceField = "force"
const val regularPollType = "regular"
const val quizPollType = "quiz"
const val dataField = "data"
const val credentialsField = "credentials"
const val hashField = "hash"
const val translationField = "translation"
const val filesField = "files"
const val frontSideField = "front_side"
const val reverseSideField = "reverse_side"
const val selfieField = "selfie"
const val secretField = "secret"

View File

@ -5,6 +5,7 @@ import dev.inmo.tgbotapi.types.FileUniqueId
internal const val fileIdField = "file_id"
internal const val fileSizeField = "file_size"
internal const val fileDateField = "file_date"
internal const val filePathField = "file_path"
/**

View File

@ -0,0 +1,20 @@
package dev.inmo.tgbotapi.types.passport
import dev.inmo.tgbotapi.types.*
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
typealias Base64EncodedData = String
typealias EncryptedAndBase64EncodedData = String
typealias EncryptedByBotRSAAndBase64EncodedData = String
typealias EncryptedData = String
@Serializable
data class EncryptedCredentials(
@SerialName(dataField)
val data: EncryptedAndBase64EncodedData,
@SerialName(hashField)
val hash: Base64EncodedData,
@SerialName(secretField)
val secret: EncryptedByBotRSAAndBase64EncodedData
)

View File

@ -0,0 +1,15 @@
package dev.inmo.tgbotapi.types.passport
import dev.inmo.tgbotapi.types.credentialsField
import dev.inmo.tgbotapi.types.dataField
import dev.inmo.tgbotapi.types.passport.encrypted_data.abstracts.EncryptedPassportElement
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class PassportData(
@SerialName(dataField)
val data: List<EncryptedPassportElement>,
@SerialName(credentialsField)
val credentials: EncryptedCredentials
)

View File

@ -0,0 +1,16 @@
package dev.inmo.tgbotapi.types.passport.encrypted_data
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.passport.Base64EncodedData
import dev.inmo.tgbotapi.types.passport.encrypted_data.abstracts.WithEmail
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable(EncryptedElementSerializer::class)
data class Email(
@SerialName(emailField)
override val email: String,
@SerialName(hashField)
override val hash: Base64EncodedData
) : WithEmail {
}

View File

@ -0,0 +1,16 @@
package dev.inmo.tgbotapi.types.passport.encrypted_data
import dev.inmo.tgbotapi.types.dataField
import dev.inmo.tgbotapi.types.hashField
import dev.inmo.tgbotapi.types.passport.*
import dev.inmo.tgbotapi.types.passport.encrypted_data.abstracts.WithData
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable(EncryptedElementSerializer::class)
data class EncryptedAddress(
@SerialName(dataField)
override val data: EncryptedAndBase64EncodedData,
@SerialName(hashField)
override val hash: Base64EncodedData
) : WithData

View File

@ -0,0 +1,86 @@
package dev.inmo.tgbotapi.types.passport.encrypted_data
import dev.inmo.tgbotapi.types.hashField
import dev.inmo.tgbotapi.types.passport.encrypted_data.abstracts.*
import dev.inmo.tgbotapi.types.typeField
import dev.inmo.tgbotapi.utils.nonstrictJsonFormat
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializer
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.*
@Serializer(EncryptedPassportElement::class)
object EncryptedElementSerializer : KSerializer<EncryptedPassportElement> {
private val jsonSerializer = JsonObject.serializer()
override val descriptor: SerialDescriptor = jsonSerializer.descriptor
override fun deserialize(decoder: Decoder): EncryptedPassportElement {
val json = jsonSerializer.deserialize(decoder)
return when (json[typeField] ?.jsonPrimitive ?.content) {
"personal_details" -> nonstrictJsonFormat.decodeFromJsonElement(EncryptedPersonalDetails.serializer(), json)
"passport" -> nonstrictJsonFormat.decodeFromJsonElement(CommonPassport.serializer(), json)
"driver_license" -> nonstrictJsonFormat.decodeFromJsonElement(DriverLicense.serializer(), json)
"identity_card" -> nonstrictJsonFormat.decodeFromJsonElement(IdentityCard.serializer(), json)
"internal_passport" -> nonstrictJsonFormat.decodeFromJsonElement(InternalPassport.serializer(), json)
"address" -> nonstrictJsonFormat.decodeFromJsonElement(EncryptedAddress.serializer(), json)
"utility_bill" -> nonstrictJsonFormat.decodeFromJsonElement(UtilityBill.serializer(), json)
"bank_statement" -> nonstrictJsonFormat.decodeFromJsonElement(BankStatement.serializer(), json)
"rental_agreement" -> nonstrictJsonFormat.decodeFromJsonElement(RentalAgreement.serializer(), json)
"passport_registration" -> nonstrictJsonFormat.decodeFromJsonElement(PassportRegistration.serializer(), json)
"temporary_registration" -> nonstrictJsonFormat.decodeFromJsonElement(TemporaryRegistration.serializer(), json)
"phone_number" -> nonstrictJsonFormat.decodeFromJsonElement(PhoneNumber.serializer(), json)
"email" -> nonstrictJsonFormat.decodeFromJsonElement(Email.serializer(), json)
else -> UnknownEncryptedPassportElement(json, json[hashField] ?.jsonPrimitive ?.content ?: "")
}
}
override fun serialize(encoder: Encoder, value: EncryptedPassportElement) {
val json = when (value) {
is EncryptedPersonalDetails -> JsonObject(
nonstrictJsonFormat.encodeToJsonElement(EncryptedPersonalDetails.serializer(), value).jsonObject + (typeField to JsonPrimitive("personal_details"))
)
is CommonPassport -> JsonObject(
nonstrictJsonFormat.encodeToJsonElement(CommonPassport.serializer(), value).jsonObject + (typeField to JsonPrimitive("passport"))
)
is DriverLicense -> JsonObject(
nonstrictJsonFormat.encodeToJsonElement(DriverLicense.serializer(), value).jsonObject + (typeField to JsonPrimitive("driver_license"))
)
is IdentityCard -> JsonObject(
nonstrictJsonFormat.encodeToJsonElement(IdentityCard.serializer(), value).jsonObject + (typeField to JsonPrimitive("identity_card"))
)
is InternalPassport -> JsonObject(
nonstrictJsonFormat.encodeToJsonElement(InternalPassport.serializer(), value).jsonObject + (typeField to JsonPrimitive("internal_passport"))
)
is EncryptedAddress -> JsonObject(
nonstrictJsonFormat.encodeToJsonElement(EncryptedAddress.serializer(), value).jsonObject + (typeField to JsonPrimitive("address"))
)
is UtilityBill -> JsonObject(
nonstrictJsonFormat.encodeToJsonElement(UtilityBill.serializer(), value).jsonObject + (typeField to JsonPrimitive("utility_bill"))
)
is BankStatement -> JsonObject(
nonstrictJsonFormat.encodeToJsonElement(BankStatement.serializer(), value).jsonObject + (typeField to JsonPrimitive("bank_statement"))
)
is RentalAgreement -> JsonObject(
nonstrictJsonFormat.encodeToJsonElement(RentalAgreement.serializer(), value).jsonObject + (typeField to JsonPrimitive("rental_agreement"))
)
is PassportRegistration -> JsonObject(
nonstrictJsonFormat.encodeToJsonElement(PassportRegistration.serializer(), value).jsonObject + (typeField to JsonPrimitive("passport_registration"))
)
is TemporaryRegistration -> JsonObject(
nonstrictJsonFormat.encodeToJsonElement(TemporaryRegistration.serializer(), value).jsonObject + (typeField to JsonPrimitive("temporary_registration"))
)
is PhoneNumber -> JsonObject(
nonstrictJsonFormat.encodeToJsonElement(PhoneNumber.serializer(), value).jsonObject + (typeField to JsonPrimitive("phone_number"))
)
is Email -> JsonObject(
nonstrictJsonFormat.encodeToJsonElement(Email.serializer(), value).jsonObject + (typeField to JsonPrimitive("email"))
)
is UnknownEncryptedPassportElement -> value.rawJson
else -> return
}
jsonSerializer.serialize(encoder, json)
}
}

View File

@ -0,0 +1,17 @@
package dev.inmo.tgbotapi.types.passport.encrypted_data
import dev.inmo.tgbotapi.types.dataField
import dev.inmo.tgbotapi.types.hashField
import dev.inmo.tgbotapi.types.passport.Base64EncodedData
import dev.inmo.tgbotapi.types.passport.EncryptedAndBase64EncodedData
import dev.inmo.tgbotapi.types.passport.encrypted_data.abstracts.WithData
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable(EncryptedElementSerializer::class)
data class EncryptedPersonalDetails(
@SerialName(dataField)
override val data: EncryptedAndBase64EncodedData,
@SerialName(hashField)
override val hash: Base64EncodedData
) : WithData

View File

@ -0,0 +1,37 @@
package dev.inmo.tgbotapi.types.passport.encrypted_data
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.passport.*
import dev.inmo.tgbotapi.types.passport.encrypted_data.abstracts.*
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable(EncryptedElementSerializer::class)
sealed class Passport : WithData, WithFrontSide, WithSelfie, Translatable
@Serializable(EncryptedElementSerializer::class)
data class CommonPassport(
@SerialName(dataField)
override val data: EncryptedAndBase64EncodedData,
@SerialName(frontSideField)
override val frontSide: PassportFile?,
@SerialName(selfieField)
override val selfie: PassportFile?,
@SerialName(translationField)
override val translations: List<PassportFile>,
@SerialName(hashField)
override val hash: Base64EncodedData
) : Passport()
@Serializable(EncryptedElementSerializer::class)
data class InternalPassport(
@SerialName(dataField)
override val data: EncryptedAndBase64EncodedData,
@SerialName(frontSideField)
override val frontSide: PassportFile?,
@SerialName(selfieField)
override val selfie: PassportFile?,
@SerialName(translationField)
override val translations: List<PassportFile>,
@SerialName(hashField)
override val hash: Base64EncodedData
) : Passport()

View File

@ -0,0 +1,25 @@
package dev.inmo.tgbotapi.types.passport.encrypted_data
import dev.inmo.tgbotapi.requests.abstracts.FileId
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.files.abstracts.*
import dev.inmo.tgbotapi.types.files.abstracts.fileIdField
import dev.inmo.tgbotapi.types.files.abstracts.fileSizeField
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
/**
* This object represents a file uploaded to Telegram Passport. Currently all Telegram Passport files are in JPEG format
* when decrypted and don't exceed 10MB.
*/
@Serializable
data class PassportFile(
@SerialName(fileIdField)
override val fileId: FileId,
@SerialName(fileUniqueIdField)
override val fileUniqueId: FileUniqueId,
@SerialName(fileSizeField)
override val fileSize: Long,
@SerialName(fileDateField)
val uploadingDate: TelegramDate
) : TelegramMediaFile

View File

@ -0,0 +1,17 @@
package dev.inmo.tgbotapi.types.passport.encrypted_data
import dev.inmo.tgbotapi.types.hashField
import dev.inmo.tgbotapi.types.passport.Base64EncodedData
import dev.inmo.tgbotapi.types.passport.encrypted_data.abstracts.WithPhoneNumber
import dev.inmo.tgbotapi.types.phoneNumberField
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable(EncryptedElementSerializer::class)
data class PhoneNumber(
@SerialName(phoneNumberField)
override val phoneNumber: String,
@SerialName(hashField)
override val hash: Base64EncodedData
) : WithPhoneNumber {
}

View File

@ -0,0 +1,58 @@
package dev.inmo.tgbotapi.types.passport.encrypted_data
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.passport.Base64EncodedData
import dev.inmo.tgbotapi.types.passport.encrypted_data.abstracts.FilesCollection
import dev.inmo.tgbotapi.types.passport.encrypted_data.abstracts.Translatable
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable(EncryptedElementSerializer::class)
sealed class TranslatableFilesCollection : Translatable, FilesCollection
@Serializable(EncryptedElementSerializer::class)
data class UtilityBill(
@SerialName(filesField)
override val files: List<PassportFile>,
@SerialName(translationField)
override val translations: List<PassportFile>,
@SerialName(hashField)
override val hash: Base64EncodedData
) : TranslatableFilesCollection()
@Serializable(EncryptedElementSerializer::class)
data class BankStatement(
@SerialName(filesField)
override val files: List<PassportFile>,
@SerialName(translationField)
override val translations: List<PassportFile>,
@SerialName(hashField)
override val hash: Base64EncodedData
) : TranslatableFilesCollection()
@Serializable(EncryptedElementSerializer::class)
data class RentalAgreement(
@SerialName(filesField)
override val files: List<PassportFile>,
@SerialName(translationField)
override val translations: List<PassportFile>,
@SerialName(hashField)
override val hash: Base64EncodedData
) : TranslatableFilesCollection()
@Serializable(EncryptedElementSerializer::class)
data class PassportRegistration(
@SerialName(filesField)
override val files: List<PassportFile>,
@SerialName(translationField)
override val translations: List<PassportFile>,
@SerialName(hashField)
override val hash: Base64EncodedData
) : TranslatableFilesCollection()
@Serializable(EncryptedElementSerializer::class)
data class TemporaryRegistration(
@SerialName(filesField)
override val files: List<PassportFile>,
@SerialName(translationField)
override val translations: List<PassportFile>,
@SerialName(hashField)
override val hash: Base64EncodedData
) : TranslatableFilesCollection()

View File

@ -0,0 +1,43 @@
package dev.inmo.tgbotapi.types.passport.encrypted_data
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.passport.Base64EncodedData
import dev.inmo.tgbotapi.types.passport.EncryptedAndBase64EncodedData
import dev.inmo.tgbotapi.types.passport.encrypted_data.abstracts.*
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable(EncryptedElementSerializer::class)
sealed class TranslatableIDDocument : WithData, WithFrontSide, WithReverseSide, WithSelfie, Translatable
@Serializable(EncryptedElementSerializer::class)
data class DriverLicense(
@SerialName(dataField)
override val data: EncryptedAndBase64EncodedData,
@SerialName(frontSideField)
override val frontSide: PassportFile?,
@SerialName(reverseSideField)
override val reverseSide: PassportFile?,
@SerialName(selfieField)
override val selfie: PassportFile?,
@SerialName(translationField)
override val translations: List<PassportFile>,
@SerialName(hashField)
override val hash: Base64EncodedData
) : TranslatableIDDocument()
@Serializable(EncryptedElementSerializer::class)
data class IdentityCard(
@SerialName(dataField)
override val data: EncryptedAndBase64EncodedData,
@SerialName(frontSideField)
override val frontSide: PassportFile?,
@SerialName(reverseSideField)
override val reverseSide: PassportFile?,
@SerialName(selfieField)
override val selfie: PassportFile?,
@SerialName(translationField)
override val translations: List<PassportFile>,
@SerialName(hashField)
override val hash: Base64EncodedData
) : TranslatableIDDocument()

View File

@ -0,0 +1,17 @@
package dev.inmo.tgbotapi.types.passport.encrypted_data.abstracts
import dev.inmo.tgbotapi.types.passport.Base64EncodedData
import dev.inmo.tgbotapi.types.passport.encrypted_data.EncryptedElementSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonObject
@Serializable(EncryptedElementSerializer::class)
interface EncryptedPassportElement {
val hash: Base64EncodedData
}
@Serializable(EncryptedElementSerializer::class)
data class UnknownEncryptedPassportElement(
val rawJson: JsonObject,
override val hash: Base64EncodedData
) : EncryptedPassportElement

View File

@ -0,0 +1,10 @@
package dev.inmo.tgbotapi.types.passport.encrypted_data.abstracts
import dev.inmo.tgbotapi.types.passport.encrypted_data.EncryptedElementSerializer
import dev.inmo.tgbotapi.types.passport.encrypted_data.PassportFile
import kotlinx.serialization.Serializable
@Serializable(EncryptedElementSerializer::class)
interface FilesCollection : EncryptedPassportElement {
val files: List<PassportFile>
}

View File

@ -0,0 +1,10 @@
package dev.inmo.tgbotapi.types.passport.encrypted_data.abstracts
import dev.inmo.tgbotapi.types.passport.encrypted_data.EncryptedElementSerializer
import dev.inmo.tgbotapi.types.passport.encrypted_data.PassportFile
import kotlinx.serialization.Serializable
@Serializable(EncryptedElementSerializer::class)
interface Translatable : EncryptedPassportElement {
val translations: List<PassportFile>
}

View File

@ -0,0 +1,10 @@
package dev.inmo.tgbotapi.types.passport.encrypted_data.abstracts
import dev.inmo.tgbotapi.types.passport.EncryptedAndBase64EncodedData
import dev.inmo.tgbotapi.types.passport.encrypted_data.EncryptedElementSerializer
import kotlinx.serialization.Serializable
@Serializable(EncryptedElementSerializer::class)
interface WithData : EncryptedPassportElement {
val data: EncryptedAndBase64EncodedData
}

View File

@ -0,0 +1,9 @@
package dev.inmo.tgbotapi.types.passport.encrypted_data.abstracts
import dev.inmo.tgbotapi.types.passport.encrypted_data.EncryptedElementSerializer
import kotlinx.serialization.Serializable
@Serializable(EncryptedElementSerializer::class)
interface WithEmail : EncryptedPassportElement {
val email: String
}

View File

@ -0,0 +1,10 @@
package dev.inmo.tgbotapi.types.passport.encrypted_data.abstracts
import dev.inmo.tgbotapi.types.passport.encrypted_data.EncryptedElementSerializer
import dev.inmo.tgbotapi.types.passport.encrypted_data.PassportFile
import kotlinx.serialization.Serializable
@Serializable(EncryptedElementSerializer::class)
interface WithFrontSide : EncryptedPassportElement {
val frontSide: PassportFile?
}

View File

@ -0,0 +1,9 @@
package dev.inmo.tgbotapi.types.passport.encrypted_data.abstracts
import dev.inmo.tgbotapi.types.passport.encrypted_data.EncryptedElementSerializer
import kotlinx.serialization.Serializable
@Serializable(EncryptedElementSerializer::class)
interface WithPhoneNumber : EncryptedPassportElement {
val phoneNumber: String
}

View File

@ -0,0 +1,10 @@
package dev.inmo.tgbotapi.types.passport.encrypted_data.abstracts
import dev.inmo.tgbotapi.types.passport.encrypted_data.EncryptedElementSerializer
import dev.inmo.tgbotapi.types.passport.encrypted_data.PassportFile
import kotlinx.serialization.Serializable
@Serializable(EncryptedElementSerializer::class)
interface WithReverseSide : EncryptedPassportElement {
val reverseSide: PassportFile?
}

View File

@ -0,0 +1,10 @@
package dev.inmo.tgbotapi.types.passport.encrypted_data.abstracts
import dev.inmo.tgbotapi.types.passport.encrypted_data.EncryptedElementSerializer
import dev.inmo.tgbotapi.types.passport.encrypted_data.PassportFile
import kotlinx.serialization.Serializable
@Serializable(EncryptedElementSerializer::class)
interface WithSelfie : EncryptedPassportElement {
val selfie: PassportFile?
}