Amazon Cognito 的用戶端和對象驗證 - Amazon Verified Permissions

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

Amazon Cognito 的用戶端和對象驗證

當您將身分來源新增至政策存放區時,Verified Permissions 具有組態選項,可驗證 ID 和存取權杖是否如預期般使用。此驗證會在處理 IsAuthorizedWithTokenBatchIsAuthorizedWithToken API 請求時發生。ID 和存取字符,以及 Amazon Cognito 和 OIDC 身分來源的行為有所不同。使用 Amazon Cognito 使用者集區提供者,Verified Permissions 可以驗證 ID 和存取權杖中的用戶端 ID。透過 OIDC 供應商,Verified Permissions 可以驗證 ID 字符中的用戶端 ID,以及存取字符中的對象。

用戶端 ID 是與您應用程式使用的身分提供者執行個體相關聯的識別符,例如 1example23456789對象是與存取字符的預期依賴方或目的地相關聯的 URL 路徑,例如 https://mytoken.example.com。使用存取權杖時,aud宣告一律會與對象建立關聯。

Amazon Cognito ID 權杖具有包含應用程式用戶端 ID 的aud宣告。存取字符的client_id宣告也包含應用程式用戶端 ID。

當您在身分來源中輸入一或多個用戶端應用程式驗證值時,Verified Permissions 會將此應用程式用戶端 IDs 清單與 ID 字符aud宣告或存取字符client_id宣告進行比較。Verified Permissions 不會驗證 Amazon Cognito 身分來源的依賴方對象 URL。

JWTs的用戶端授權

您可能想要在應用程式中處理 JSON Web 權杖,並在不使用政策存放區身分來源的情況下將其宣告傳遞給 Verified Permissions。您可以從 JSON Web Token (JWT) 擷取實體屬性,並將其剖析為已驗證的許可。

此範例示範如何使用 JWT.1 從應用程式呼叫 Verified Permissions。

async function authorizeUsingJwtToken(jwtToken) { const payload = await verifier.verify(jwtToken); let principalEntity = { entityType: "PhotoFlash::User", // the application needs to fill in the relevant user type entityId: payload["sub"], // the application need to use the claim that represents the user-id }; let resourceEntity = { entityType: "PhotoFlash::Photo", //the application needs to fill in the relevant resource type entityId: "jane_photo_123.jpg", // the application needs to fill in the relevant resource id }; let action = { actionType: "PhotoFlash::Action", //the application needs to fill in the relevant action id actionId: "GetPhoto", //the application needs to fill in the relevant action type }; let entities = { entityList: [], }; entities.entityList.push(...getUserEntitiesFromToken(payload)); let policyStoreId = "PSEXAMPLEabcdefg111111"; // set your own policy store id const authResult = await client .isAuthorized({ policyStoreId: policyStoreId, principal: principalEntity, resource: resourceEntity, action: action, entities, }) .promise(); return authResult; } function getUserEntitiesFromToken(payload) { let attributes = {}; let claimsNotPassedInEntities = ['aud', 'sub', 'exp', 'jti', 'iss']; Object.entries(payload).forEach(([key, value]) => { if (claimsNotPassedInEntities.includes(key)) { return; } if (Array.isArray(value)) { var attibuteItem = []; value.forEach((item) => { attibuteItem.push({ string: item, }); }); attributes[key] = { set: attibuteItem, }; } else if (typeof value === 'string') { attributes[key] = { string: value, } } else if (typeof value === 'bigint' || typeof value ==='number') { attributes[key] = { long: value, } } else if (typeof value === 'boolean') { attributes[key] = { boolean: value, } } }); let entityItem = { attributes: attributes, identifier: { entityType: "PhotoFlash::User", entityId: payload["sub"], // the application needs to use the claim that represents the user-id } }; return [entityItem]; }

1 此程式碼範例使用 aws-jwt-verify 程式庫來驗證由 OIDC 相容 IdPs 簽署的 JWTs。