約 2 分鐘閱讀

Read in English →

使用自訂挑戰的AWS Cognito用戶池與iOS的Mobile SDK

我最近將一個AWS Cognito用戶池整合到一個iOS應用程式中。登入功能使用自訂挑戰進行認證。然而,關於如何使用iOS SDK達到此目的的文件資料很有限。在多次嘗試和失敗後,我終於成功登入。以下是達成此目的的步驟:

我最近將一個AWS Cognito用戶池整合到一個iOS應用程式中。登入功能使用自訂挑戰進行認證。然而,關於如何使用iOS SDK達到此目的的文件資料很有限。在多次嘗試和失敗後,我終於成功登入。以下是達成此目的的步驟:

步驟1:建立一個CognitoUserPool

在AppDelegate中,完成didFinishLaunchingWithOptions後,用戶池如下所示初始化:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

  // Set up service configuration
  let serviceConfiguration = AWSServiceConfiguration(region: CognitoIdentityUserPoolRegion, credentialsProvider: nil)

  // Create pool configuration
  let poolConfiguration = AWSCognitoIdentityUserPoolConfiguration(clientId: CognitoIdentityUserPoolAppClientId, clientSecret: nil, poolId: CognitoIdentityUserPoolId)

  // Initialize user pool client
  AWSCognitoIdentityUserPool.register(with: serviceConfiguration, userPoolConfiguration: poolConfiguration, forKey: AWSCognitoUserPoolsSignInProviderKey)

  // Fetch the user pool client we initialized in the above step
  let pool = AWSCognitoIdentityUserPool(forKey: AWSCognitoUserPoolsSignInProviderKey)

  self.storyboard = UIStoryboard(name: "Main", bundle: nil)

  pool.delegate = self

  return true
}

步驟2:執行協定代表

extension AppDelegate: AWSCognitoIdentityCustomAuthentication {

  func didCompleteStepWithError(_ error: Error?) {

  }

  func getCustomChallengeDetails(_ authenticationInput: AWSCognitoIdentityCustomAuthenticationInput, customAuthCompletionSource: AWSTaskCompletionSource<AWSCognitoIdentityCustomChallengeDetails>) {

  }

  func startCustomAuthentication() -> AWSCognitoIdentityCustomAuthentication {
    if self.navigationController == nil {
      self.navigationController = self.storyboard?.instantiateViewController(withIdentifier: "signinController") as? UINavigationController
    }

    if self.signInViewController == nil {
      self.signInViewController = self.navigationController?.viewControllers[0] as? SignInViewController
    }

    DispatchQueue.main.async {
      self.navigationController!.popToRootViewController(animated: true)

      if !self.navigationController!.isViewLoaded || self.navigationController!.view.window == nil {
        self.window?.rootViewController?.present(self.navigationController!, animated: true, completion: nil)
      }
    }

    return self.signInViewController!
  }
}

步驟3:在登入視圖控制器中處理自訂挑戰

extension SignInViewController: AWSCognitoIdentityCustomAuthentication {

  func getCustomChallengeDetails(_ authenticationInput: AWSCognitoIdentityCustomAuthenticationInput, customAuthCompletionSource: AWSTaskCompletionSource<AWSCognitoIdentityCustomChallengeDetails>) {

    let authDetails = AWSCognitoIdentityCustomChallengeDetails(challengeResponses: ["USERNAME": "YourUserName", "ANSWER": "123456"])
    customAuthCompletionSource.set(result: authDetails)
  }

  public func didCompleteStepWithError(_ error: Error?) {
    DispatchQueue.main.async {
      if let error = error as? NSError {
        print("error")
      } else {
        print("success")
        self.dismiss(animated: true, completion: nil)
      }
    }
  }
}

步驟4:成功登入後存取用戶屬性

self.user?.getDetails().continueOnSuccessWith { (task) -> AnyObject? in
  DispatchQueue.main.async(execute: {
    self.response = task.result

    // Display user details
    print(response)
  })

  return nil
}

如果你有任何問題,請隨時提問。我希望AWS能更新文檔,並提供範例代碼,以便在不需要透過試驗和錯誤的情況下理解SDK。

繼續探索這個主題

  • 比較 AWS Cognito 與 AWS IAM Identity Center

    今天,我收到一位顧問的問題,他對 AWS Cognito 和 AWS IAM Identity Center(AWS 單一登入的後繼者)感到非常困惑。它們都是亞馬遜網路服務(AWS)提供的身份和訪問管理(IAM)服務。兩種服務都可以用來管理用戶身份和訪問AWS資源。然而,这两种服务之间有一些关键的差异。

  • 實用 AWS FinOps推動雲端成功的關鍵策略

    雲端已經徹底改變了組織消費科技資源的方式。在傳統 IT 世界中,工程師需要提交基礎設施申請,經過冗長的採購流程。容量通常以峰值需求為基準提前購買,結果往往是大量閒置的資源。成本雖然可預測,但與實際使用情況脫節,而硬體申請與部署流程的延遲也限制了創新速度。

  • 從 AWS RDS 遷移至 Aurora

    遷移資料庫對於任何希望提升效能、可擴展性和成本效率的組織而言,都是一項關鍵任務。AWS Aurora 相較於傳統的 RDS(關聯式資料庫服務)提供了顯著的優勢,例如更快的效能、高可用性和內建的容錯機制。如果您考慮從 RDS 遷移至 Aurora,有三個主要選項可供選擇:快照遷移(Snapshot Migration)、Aurora 讀取副本(Read…

返回文章列表