OIDC 提供者的用戶端和對象驗證 - Amazon Verified Permissions

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

OIDC 提供者的用戶端和對象驗證

當您將身分來源新增至政策存放區時,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宣告一律會與對象建立關聯。

OIDC ID 字符具有包含用戶端 IDs 的aud宣告,例如 1example23456789

OIDC Access 字符具有包含字符受眾 URL 的aud宣告,例如 https://myapplication.example.com,以及包含用戶端 IDs的client_id宣告,例如 1example23456789

設定您的政策存放區時,請輸入一或多個用於對象驗證的值,您的政策存放區會使用這些值來驗證權杖的對象。

  • ID 字符 – Verified Permissions 透過檢查aud宣告中至少有一個用戶端 IDs成員符合對象驗證值來驗證用戶端 ID。

  • 存取字符 – Verified Permissions 透過檢查aud宣告中的 URL 是否符合對象驗證值來驗證對象。如果沒有aud宣告,可以使用 cidclient_id宣告來驗證對象。請洽詢您的身分提供者,了解正確的對象聲明和格式。

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。