與作為AEMCloud Service的整合必須能夠安全地驗證AEM。 Developer AEM Console授與「服務認證」的存取權,此服務認證可協助外部應用程式、系統和服務透過HTTP以程式設計方式與AEM Author或Publish服務互動。
「服務憑證」可能會以類似的「本機開發存取Token」(a1/>)顯示,但以下列幾種主要方式不同:
服務憑證及其產生的存取Token以及本機開發存取Token都應保密,因為這三個Token都可用來存取其個別的AEMCloud Service環境
生成服務憑據分為兩個步驟:
與本機開發存取Token不同,服務認證需要您的Adobe組織IMS管理員進行一次性初始化,才能下載。
這是作為Cloud Service環境的每AEM次一次初始化
一旦初始AEM化Cloud Service環境的「服務認證」後,您的AEMAdobeIMS組織中的其他開發人員就可以下載這些認證。
下載服務憑據的步驟與初始化步驟相同。 如果尚未進行初始化,則會點選取得服務憑證按鈕,顯示使用者錯誤。
服務憑證提供產生JWT所需的詳細資訊,此JWT會交換為存取Token,用來以Cloud ServiceAEM身分驗證。 服務憑證必須儲存在可供使用其進行存取的外部應用程式、系統或服務存取的安全位置AEM。 每個客戶對服務認證的管理方式和位置都是獨一無二的。
為簡單起見,本教學課程會透過命令列傳遞服務認證,但請與您的IT安全團隊合作,瞭解如何根據貴組織的安全性准則儲存及存取這些認證。
service_token.json
的檔案
服務認證是完整格式的JSON物件,與JWT或存取Token不相同。 而是使用「服務憑證」(包含私密金鑰)來產生JWT,該JWT會與AdobeIMS API交換以取得存取Token。
若要使AEM用「服務認證」以Cloud Service形式存取,外部應用程式必須以3種方式更新:
在本教程中,Adobe的@adobe/jwt-auth
npm模組用於兩者,(1)從服務憑據生成JWT,(2)在單個函式調用中將其交換為訪問令牌。 如果您的應用程式並非以JavaScript為基礎,請檢閱其他語言的范常式式碼,瞭解如何從「服務認證」建立JWT,並以AdobeIMS交換它以取用Token。
請檢閱getCommandLineParams()
,並查看我們可以使用本機開發存取Token JSON中用來讀取的相同程式碼,在「服務認證JSON」檔案中讀取。
function getCommandLineParams() {
...
// Read in the credentials from the provided JSON file
// Since both the Local Development Access Token and Service Credentials files are JSON, this same approach can be re-used
if (parameters.file) {
parameters.developerConsoleCredentials = JSON.parse(fs.readFileSync(parameters.file));
}
...
return parameters;
}
一旦讀取服務憑證,它們就用來產生JWT,然後與AdobeIMS API交換以取得存取Token,然後以Cloud ServiceAEM形式存取。
此範例應用程式是以Node.js為基礎,因此最好使用@adobe/jwt-auth npm模組來促進(1)JWT產生和(20與AdobeIMS交換)。 如果您的應用程式是使用其他語言開發,請檢閱適當的程式碼範例,以瞭解如何使用其他程式設計語言來建構HTTP要求以AdobeIMS。
更新getAccessToken(..)
以檢查JSON檔案內容,並判斷其代表本機開發存取Token或服務憑證。 這可透過檢查.accessToken
屬性是否存在而輕鬆實現,此屬性僅存在於本機開發存取Token JSON。
如果提供服務認證,應用程式生成JWT,並與AdobeIMS交換該JWT以獲取訪問令牌。 我們將使用@adobe/jwt-auth的auth(...)
函式,此函式會產生JWT,並在單一函式呼叫中將它交換為存取Token。 auth(..)
的參數是JSON物件,由「服務認證JSON」中可用的特定資訊組成,如程式碼中所述。
async function getAccessToken(developerConsoleCredentials) {
if (developerConsoleCredentials.accessToken) {
// This is a Local Development access token
return developerConsoleCredentials.accessToken;
} else {
// This is the Service Credentials JSON object that must be exchanged with Adobe IMS for an access token
let serviceCredentials = developerConsoleCredentials.integration;
// Use the @adobe/jwt-auth library to pass the service credentials generated a JWT and exchange that with Adobe IMS for an access token.
// If other programming languages are used, please see these code samples: https://www.adobe.io/authentication/auth-methods.html#!AdobeDocs/adobeio-auth/master/JWT/samples/samples.md
let { access_token } = await auth({
clientId: serviceCredentials.technicalAccount.clientId, // Client Id
technicalAccountId: serviceCredentials.id, // Technical Account Id
orgId: serviceCredentials.org, // Adobe IMS Org Id
clientSecret: serviceCredentials.technicalAccount.clientSecret, // Client Secret
privateKey: serviceCredentials.privateKey, // Private Key to sign the JWT
metaScopes: serviceCredentials.metascopes.split(','), // Meta Scopes defining level of access the access token should provide
ims: `https://${serviceCredentials.imsEndpoint}`, // IMS endpoint used to obtain the access token from
});
return access_token;
}
}
現在,視透過file
命令列參數傳入的JSON檔案(本機開發存取Token JSON或服務認證JSON)而定,應用程式將衍生存取Token。
請記住,當服務憑證未過期時,JWT和對應的存取Token會過期,而且必須在過期前重新整理。 這可以通過使用AdobeIMS](https://www.adobe.io/authentication/auth-methods.html#!AdobeDocs/adobeio-auth/master/OAuth/OAuth.md#access-tokens)提供的refresh_token
[來完成。
在進行這些變更後,以及從AEMDeveloper Console下載的服務認證JSON(為簡單起見,儲存為service_token.json
與此index.js
相同的資料夾),執行應用程式,將命令列參數file
取代為service_token.json
,並將propertyValue
更新為新值,以便效果明顯AEM。
$ node index.js \
aem=https://author-p1234-e5678.adobeaemcloud.com \
folder=/wknd/en/adventures/napa-wine-tasting \
propertyName=metadata/dc:rights \
propertyValue="WKND Restricted Use" \
file=service_token.json
到終端的輸出如下所示:
200 - OK @ https://author-p1234-e5678.adobeaemcloud.com/api/assets/wknd/en/adventures/napa-wine-tasting.json
403 - Forbidden @ https://author-p1234-e5678.adobeaemcloud.com/api/assets/wknd/en/adventures/napa-wine-tasting/AdobeStock_277654931.jpg.json
403 - Forbidden @ https://author-p1234-e5678.adobeaemcloud.com/api/assets/wknd/en/adventures/napa-wine-tasting/AdobeStock_239751461.jpg.json
403 - Forbidden @ https://author-p1234-e5678.adobeaemcloud.com/api/assets/wknd/en/adventures/napa-wine-tasting/AdobeStock_280313729.jpg.json
403 - Forbidden @ https://author-p1234-e5678.adobeaemcloud.com/api/assets/wknd/en/adventures/napa-wine-tasting/AdobeStock_286664352.jpg.json
403 - Forbidden行會將HTTP API呼叫中的錯誤指AEM示為Cloud Service。 嘗試更新資產的中繼資料時,會發生這403個「禁止」錯誤。
原因是服務憑證衍生的存取Token會驗證使用自動建立的技術帳AEM戶使用者的要AEM求,依預設只有讀取存取權。 要向應用程式提供寫訪問權AEM,必須授AEM予與訪問Token關聯的技術帳戶用戶的權AEM限。
「服務憑證」衍生的存取Token使用「參與者」使AEM用者群組成員資AEM格的技術帳戶使用者。
一旦技術帳戶AEM使用者存在AEM(第一次使用存取Token的HTTP要求後),就可以像其他使用者AEM一樣管理該使用者的AEM權限。
integration.email
值,其外觀應類似:12345678-abcd-9000-efgh-0987654321c@techacct.adobe.com
。使用中授予的技術帳AEM戶對資產擁有寫權限,請重新執行應用程式:
$ node index.js \
aem=https://author-p1234-e5678.adobeaemcloud.com \
folder=/wknd/en/adventures/napa-wine-tasting \
propertyName=metadata/dc:rights \
propertyValue="WKND Restricted Use" \
file=service_token.json
到終端的輸出如下所示:
200 - OK @ https://author-p1234-e5678.adobeaemcloud.com/api/assets/wknd/en/adventures/napa-wine-tasting.json
200 - OK @ https://author-p1234-e5678.adobeaemcloud.com/api/assets/wknd/en/adventures/napa-wine-tasting/AdobeStock_277654931.jpg.json
200 - OK @ https://author-p1234-e5678.adobeaemcloud.com/api/assets/wknd/en/adventures/napa-wine-tasting/AdobeStock_286664352.jpg.json
200 - OK @ https://author-p1234-e5678.adobeaemcloud.com/api/assets/wknd/en/adventures/napa-wine-tasting/AdobeStock_239751461.jpg.json
200 - OK @ https://author-p1234-e5678.adobeaemcloud.com/api/assets/wknd/en/adventures/napa-wine-tasting/AdobeStock_280313729.jpg.json
aem
命令行參數中提供的相同主機名)folder
命令列參數指定的資產資料夾,例如WKND > 英文 > 冒險 > 納帕品酒會metadata/dc:rights
JCR屬性的Copyright,現在會反映propertyValue
參數中提供的值,例如WKND Restricted Use現在,我們已使用本機開AEM發存取Token以及生產就緒型服務對服務存取Token,以程式設計方式存取為Cloud Service!