apple-notary-api-kotlin-client:Apple Notary API 的 Kotlin 客户端,无需 Xcode 和 macOS 即可公证 macOS 应用

Apple Notary API client for Kotlin

Branch2Tags11

apple-notary-api-kotlin-client

JetBrains 团队项目 Github 构建 GitHub 许可证

适用于 Kotlin 的 Apple Notary API 客户端。

无需依赖 Xcode 或 macOS,即可通过 Kotlin 代码对您的 macOS 应用进行公证。

添加依赖项

此库可在 Space Packages 上获取,您需要将此 Maven 仓库添加到您的 Gradle 配置中:

import java.net.URI

repositories {
    maven {
        url = URI("https://packages.jetbrains.team/maven/p/ij/intellij-dependencies")
    }
}

然后添加依赖项

implementation("org.jetbrains:apple-notary-api-kotlin-client:$version")

你还需要提供一个 Ktor 引擎实现,例如,如果你想使用 CIO 引擎:

implementation("io.ktor:ktor-client-cio:$ktorVersion")

用法

首先,您需要为 App Store Connect API 创建一个 API 密钥,具体步骤请参见 Apple 官方文档。

在此过程中,请确保保存以下信息:

  • App Store Connect 中 API 密钥页面的签发者 ID(例如:57246542-96fe-1a63-e053-0824d011072a)
  • App Store Connect 中的私钥 ID(例如:2X9R4HXF34)
  • 可从 App Store Connect 中 API 密钥页面下载的私钥 .p8 文件

获取这些信息后,您可以设置一个凭证对象:

import com.jetbrains.notary.NotaryClientV2
import com.jetbrains.notary.auth.AppStoreConnectAPIKey

val credentials = AppStoreConnectAPIKey(
    issuerId = "your-issuer-id",
    keyId = "your-private-key-id",
    privateKey = "your-private-key-file-content",
)

val client = NotaryClientV2(credentials)

// ... now you can use your client!

// For example retrieving your previous submissions:
val submissions = client.getPreviousSubmissions()
println(submissions)

扩展

apple-notary-api-kotlin-client 提供了一些扩展函数,可满足 Apple Notary API 的基本使用场景。

函数 notarize 或 notarizeBlocking

notarize(或其阻塞等效函数 notarizeBlocking)可满足“我想要对文件进行公证,等待公证完成,并在完成后获取提交结果的状态和日志”这一基本使用场景。 在底层,它将执行以下操作:

  • 为指定文件发起公证提交
  • 将指定文件上传至 Apple Notary API 要求的位置
  • 轮询 Apple Notary API 直至提交完成(或超时)
    • 注意:在默认配置下,ignoreServerError 和 ignoreTimeoutExceptions 均设为 true,此配置可增强轮询的稳定性,避免因常见的误判情况导致失败
  • 获取已完成提交的日志(无论提交成功与否)
  • 返回已完成提交的状态和日志
示例用法代码片段
import com.jetbrains.notary.NotaryClientV2
import com.jetbrains.notary.auth.AppStoreConnectAPIKey
import kotlin.time.Duration.Companion.minutes

val credentials = AppStoreConnectAPIKey(
    issuerId = "your-issuer-id",
    keyId = "your-private-key-id",
    privateKey = "your-private-key-file-content",
)

val notaryApiClient = NotaryClientV2(credentials)
val notarizationResult = notaryApiClient.notarizeBlocking(file, StatusPollingConfiguration(
    timeout = 30.minutes,
    pollingPeriod = 1.minutes,
    ignoreServerError = true,
    ignoreTimeoutExceptions = true,
))

val json = Json { prettyPrint = true }
println("Notarization logs:\n${json.encodeToString(notarizationResult.logs)}")

when (notarizationResult.status) {
    SubmissionResponse.Status.ACCEPTED -> println("Notarization of $file successful")
    SubmissionResponse.Status.IN_PROGRESS, null -> error("Timed out polling status of $file notarization submission")
    SubmissionResponse.Status.INVALID, SubmissionResponse.Status.REJECTED -> error("Notarization of $file failed, see logs above")
}

配置

使用您自己的 ktor 客户端实现

您可以自由使用自己的 ktor 客户端实现及相应配置。 我们提供 NotaryClientV2.defaultHttpClient,以便您的客户端配置能够基于默认配置进行扩展。

配置示例:修改重试策略
import com.jetbrains.notary.NotaryClientV2
import com.jetbrains.notary.auth.AppStoreConnectAPIKey
import kotlin.time.Duration.Companion.minutes

val credentials = AppStoreConnectAPIKey(
  issuerId = "your-issuer-id",
  keyId = "your-private-key-id",
  privateKey = "your-private-key-file-content",
)

val myOwnClient = NotaryClientV2.defaultHttpClient.config { // keep all the default configuration
  install(HttpRequestRetry) {
    retryOnExceptionOrServerErrors(maxRetries = 10) // but increase the number of retry of the default configuration
    constantDelay(millis = 1.minutes.inWholeMilliseconds) // but change the default exponential retry by a constant one
  }
}

val notaryApiClient = NotaryClientV2(credentials, httpClient = myOwnClient)

许可协议

本项目基于 Apache 2.0 许可协议进行分发。

致谢

特别感谢构建 Rust 库 apple-codesign 的开发者们,他们的工作为本文档提供了灵感。

Introduction

Apple Notary API client for Kotlin

Customize your domain