跳至內容
文件 · iOS 與 Android

iOS 與 Android

原生 App 會在系統瀏覽器中透過環境的 Lobby 讓用户登入,絕不使用網頁檢視:用户可以用自己的密碼匙、已儲存的密碼和 Usher 工作階段,而 App 永遠看不到密碼。其餘的 OpenID Connect 流程由 SDK 替你完成,並將權杖保存在裝置上。

註冊 App

在 Usher Console 中打開環境,然後選擇 應用程式 → 新增應用程式 → 流動裝置或桌面。為它設定一個以你擁有的網域組成的私人用途重新導向,例如 com.example.notes:/oauth/callback。它會取得客户端 ID,但沒有機密:手機上的 App 無法妥善保管機密,因此改用 PKCE。Console 會顯示兩個平台的設定方式。

iOS 與 macOS

swift
import UsherKit

let usher = UsherClient(configuration: .init(
    issuer: URL(string: "https://login.example.com")!,
    clientID: "…",
    redirectURI: URL(string: "com.example.notes:/oauth/callback")!))

let session = try await usher.signIn()
var request = URLRequest(url: URL(string: "https://api.example.com/notes")!)
try await usher.authorize(&request)

權杖保存在鑰匙圈中,僅限此裝置。

Android

kotlin
// build.gradle.kts: manifestPlaceholders["usherRedirectScheme"] = "com.example.notes"
val usher = Usher.client(context, UsherConfiguration(
    issuer = "https://login.example.com",
    clientId = "…",
    redirectUri = "com.example.notes:/oauth/callback"))

val session = usher.signIn(Usher.customTabs(activity))

登入會在 Custom Tab 中開啟。工作階段以 Android Keystore 中的密鑰加密。

SDK 會檢查什麼

  • 每次登入時檢查 PKCE、state 與 nonce,以及回應中指名的簽發者(RFC 9207)。
  • 以你環境的密鑰檢查 ID 權杖的簽名,並檢查它的簽發者、對象與到期時間。
  • 重新整理權杖會輪替:每個只能使用一次。登出時會在 Usher 撤銷它。
  • DPoP(如果你開啟的話):權杖綁定到裝置上的密鑰,被複製到其他地方就毫無用處。

若要使用存取權杖呼叫你自己的 API,請指名該 API,並透過 resources 要求它:請參閱你自己的 API。