AppAuth iOS 實作 1-原理&執行專案

Peggy Tsai 
彼得潘的 Swift iOS App 開發教室
6 min readDec 15, 2021

--

(AppAuth iOS, openid, OAuth)

iOS 和 macOS 的 AppAuth 以及 tvOS 是一個客戶端 SDK,用於本地應用程序使用 OAuth 2.0 OpenID Connect 對最終用戶進行身份驗證和授權。Library 遵循 RFC 8252 — OAuth 2.0 for Native Apps 中規定的最佳實踐 ,包括使用 SFSafariViewController 進行授權請求。因此,出於可用性和安全原因, WebView 是明確不支持。

OAuth 2.0介紹

本文將透過AppAuth-iOS專案,簡化流程在APP做OAuth機制

下載專案後,透過cmd到專案資料夾下,檢查Cartfile中是否有定義

github "openid/AppAuth-iOS" "master"

接下來執行 carthage bootstrap — use-xcframeworks

carthage bootstrap --use-xcframeworks

*如果出現 carthage: command not found ,可以參考下方文章,先安裝carthage套件或是重新安裝

執行Example.xcodeproj
(AppAuth-iOS-master -> Examples -> Example-iOS_Swift-Carthage -> Example.xcodeproj)

開啟專案後
設定TARGETS 的 Example 中 General 設定Bundle Identifier

Frameworks, Libraries, and Embedded Content中
啟用AppAuth的Framework(選擇Embed & Sign)

點Add Other..匯入AppAuthCore的Framework

匯入AppAuthCore的Framework

成功匯入

設定TARGETS中Example的Signing & Capabilities,Signing跟App Groups設定

刪除Example_Extension

修改 AppAuthExampleViewController中下面餐數

  • Issuer
  • Client ID
  • Redirect URI

詳細介紹

以下以Google登入為範例,需做參數設定

  • Issuer
  • Client ID
  • Redirect URI

詳細介紹

*kIssuer: 瀏覽器將使用者導頁至 Authorization Server

*kClientID: Google Cloud Platform上 https://console.developers.google.com/apis/credentials?project=_的OAuth 2.0 用戶端 ID

*kRedirectURI: 將使用者導回應用程式的指定路徑中

設定authorizationEndpoint跟tokenEndpoint,並將資訊打包至configuration

--

--