簡體   English   中英

從 Cognito 身份池 identityId 獲取 Cognito 用戶池用戶名

[英]Getting cognito user pool username from cognito identity pool identityId

我正在使用 AWS Congito 用戶池通過 Cognito 身份池進行賬戶管理,該身份池將此用戶池作為身份提供者。 我使用它來控制通過 API 網關對 API 的訪問,該網關將請求發送到 Lambda。 我的 Lambda 是使用 Micronaut 用 Ja​​va 8 實現的。 所有這些都運行良好。

在 Lambda 中,我從HttpRequestPrincipal獲取名稱:

  protected String resolveUser( HttpRequest request ){
    String ret = null;

    Optional<Principal> principal = request.getUserPrincipal();
    if( principal.isPresent() ){
      ret = principal.get().getName();
    }

    if( ret == null || ret.length() == 0 ){
      ret = "unknown";
    }
    return ret;
  }

Cognito identityId 的字符串名稱中返回的內容。 像這樣的東西:

us-east-1:xxxxe650-53f4-4cba-b553-5dff42bexxxx

我想記錄實際的用戶登錄信息,或者至少有一些方法可以在需要時將 identityId 轉換為登錄信息。

LookupDeveloperIdentity API 調用似乎是解決此問題的正確方法,但我無法使其正常工作。

嘗試使用 Java 和 AWS Java SDK 2 執行此操作:

  protected String loadUsername( String user ){
    String ret = "unknown:"+user;
    CognitoIdentityClient cognito = CognitoIdentityClient.create();

    LookupDeveloperIdentityRequest request = LookupDeveloperIdentityRequest.builder()
      .identityPoolId( identityPoolId )
      .identityId( user )
      .build();
    LookupDeveloperIdentityResponse response = cognito.lookupDeveloperIdentity( request );
    List<String> identifiers = response.developerUserIdentifierList();
    if( identifiers != null && identifiers.size() > 0 ){
      ret = identifiers.get( 0 );
    }

    return ret;    
  }

拋出異常

software.amazon.awssdk.services.cognitoidentity.model.NotAuthorizedException:您無權訪問此身份(服務:CognitoIdentity,狀態代碼:400,請求 ID:64e36646-612b-4985-91d1-82aca770XXXX)

嘗試通過 CLI 執行此操作會產生類似的結果:

aws 認知身份查找-開發人員身份 --identity-id us-east-1:xxxxe650-53f4-4cba-b553-5dff42bexxxx --identity-pool-id us-east-1:xxxx0aa1-89f9-4418-be04- 7e83c838xxxx --max-results=10

調用 LookupDeveloperIdentity 操作時發生錯誤 (NotAuthorizedException):您無權訪問此身份

我已經確保現有的 IAM 策略應該能夠處理這個問題,當我嘗試使用沒有此策略的角色時,我收到了不同的錯誤

    {
        "Effect": "Allow",
        "Action": [
            "cognito-identity:LookupDeveloperIdentity"
        ],
        "Resource": [
            "arn:aws:cognito-identity:us-east-1:##########:identitypool/us-east-1:xxxx0aa1-89f9-4418-be04-7e83c838xxxx"
        ]
    }

所以問題歸結為:

  • 這是從身份池 ID 中獲取用戶池用戶名的最佳方法嗎?
    • 如果是 - 我做錯了什么?
    • 如果不是 - 這樣做的更好方法是什么?

替代方法

為了檢索用戶的用戶池用戶 ID,您可以在 lambda 中檢索:

authProvider = event.requestContext.identity.cognitoAuthenticationProvider;

這將返回一個字符串,其中包含用戶的用戶池用戶 ID,它看起來像:

cognito-idp.us-east-1.amazonaws.com/us-east-1_xxxxxxxxx,cognito-idp.us-east-1.amazonaws.com/us-east-1_aaaaaaaaa:CognitoSignIn:qqqqqqqq-1111-2222-3333-rrrrrrrrrrrr

其中 us-east-1_aaaaaaaaa 是用戶池 ID,qqqqqqqq-1111-2222-3333-rrrrrrrrrrrr 是用戶池用戶 ID。 然后,您可以拆分字符串並提取用戶 ID。

請注意,根據您使用的身份驗證提供程序,這些信息會有所不同。

然后,如果您需要用戶名而不是用戶 ID,您可以通過獲取該特定用戶 ID 的適當詳細信息直接從用戶池中提取它。

參考

https://serverless-stack.com/chapters/mapping-cognito-identity-id-and-user-pool-id.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM