ฉันใช้ AWS Congito User Pools สำหรับการจัดการบัญชีด้วย Cognito Identity Pool ที่มี User Pool เป็นผู้ให้บริการข้อมูลประจำตัว ฉันใช้สิ่งนี้เพื่อควบคุมการเข้าถึง API ผ่าน API Gateway ที่ส่งคำขอไปยัง Lambda My Lambda ใช้งานกับ Java 8 โดยใช้ Micronaut ทั้งหมดนี้ทำงานได้ดี
ในแลมบ์ดา, ฉันได้รับชื่อจากPrincipalในHttpRequest:
  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 บางสิ่งเช่นนี้
เราตะวันออก-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, รหัสคำขอ: 64e36646-612b-4985-91d1-82aca770XXXXX)
การพยายามทำสิ่งนี้ผ่าน CLI จะให้ผลลัพธ์ที่คล้ายกัน:
aws cognito-identity lookup-developer-identity --identity-id us-east-1: xxxxe650-53f4-4cba-b553-5dff42bexxxx --identity-pool-id us-east-1: xxxx0aa1-89f9-4418-be04- 7e83c838xxxx --max-results = 10
เกิดข้อผิดพลาด (NotAuthorizedException) เมื่อเรียกใช้การดำเนินการ LookupDeveloperIdentity: คุณไม่สามารถเข้าถึงข้อมูลประจำตัวนี้
ฉันทำให้แน่ใจว่านโยบาย IAM ในสถานที่ควรจะสามารถจัดการกับมันได้และเมื่อฉันลองด้วยบทบาทที่ไม่มีนโยบายนี้ฉันได้รับข้อผิดพลาดที่แตกต่างกัน
    {
        "Effect": "Allow",
        "Action": [
            "cognito-identity:LookupDeveloperIdentity"
        ],
        "Resource": [
            "arn:aws:cognito-identity:us-east-1:##########:identitypool/us-east-1:xxxx0aa1-89f9-4418-be04-7e83c838xxxx"
        ]
    }
ดังนั้นคำถามต้มลงไปที่:
- นี่เป็นวิธีที่ดีที่สุดในการรับชื่อผู้ใช้กลุ่มจากรหัสประจำตัวหรือไม่
- ถ้าเป็น - ฉันทำอะไรผิด
- ถ้าไม่ใช่ - อะไรคือวิธีที่ดีกว่าในการทำเช่นนี้?
 
Are you sure you are using the credentials from the account which owns the identity pool you are requesting lookupDeveloperIdentity for?- forums.aws.amazon.com/thread.jspa?threadID=231354สำหรับฉันดูเหมือนว่าได้รับอนุญาตจากผู้ใช้ไม่ใช่ปัญหาบทบาทของ IAM