From ed51c5b34059b49be70886adbe111a8701a4977b Mon Sep 17 00:00:00 2001 From: Roman Privalov Date: Tue, 13 Aug 2024 23:33:08 +0300 Subject: [PATCH 1/3] Update create-for-sa.md * Update Node.js JWT token creation example with more common jwt library and modern way to write code (const against var) * Update Node.js JWT token creation example with more common jwt library and modern way to write code (const against var) * Added Node.js example for JWT token exchange for IAM token --- ru/iam/operations/iam-token/create-for-sa.md | 63 ++++++++++++++------ 1 file changed, 45 insertions(+), 18 deletions(-) diff --git a/ru/iam/operations/iam-token/create-for-sa.md b/ru/iam/operations/iam-token/create-for-sa.md index 9aeeac0ae7..2c2baf4305 100644 --- a/ru/iam/operations/iam-token/create-for-sa.md +++ b/ru/iam/operations/iam-token/create-for-sa.md @@ -430,38 +430,33 @@ yc iam create-token - Node.js {#node} - Пример создания JWT с использованием [node-jose](https://github.com/cisco/node-jose): + Пример создания JWT с использованием [node-jsonwebtoken](https://github.com/auth0/node-jsonwebtoken): - Проверено для Node.js v20.12.1 и node-jose 2.2.0. - Необходимые данные читаются из JSON-файла, полученного при создании авторизованного ключа. ```js - var jose = require('node-jose'); - var fs = require('fs'); + const jwt = require('jsonwebtoken'); + const fs = require('fs'); - var json = JSON.parse(fs.readFileSync(require.resolve(''))); + const json = JSON.parse(fs.readFileSync(require.resolve(''))); - var key = json.private_key; - var serviceAccountId = json.service_account_id; - var keyId = json.id; + const key = json.private_key; + const serviceAccountId = json.service_account_id; + const keyId = json.id; - var now = Math.floor(new Date().getTime() / 1000); + const now = Math.floor(new Date.now() / 1000); - var payload = { + const payload = { aud: "https://iam.{{ api-host }}/iam/v1/tokens", iss: serviceAccountId, iat: now, exp: now + 3600 }; - jose.JWK.asKey(key, 'pem', { kid: keyId, alg: 'PS256' }) - .then(function (result) { - jose.JWS.createSign({ format: 'compact' }, result) - .update(JSON.stringify(payload)) - .final() - .then(function (result) { - console.log(result); - }); - }); + const token = jwt.sign(payload, key, { + algorithm: "PS256", + keyid: keyId + }); ``` - PHP {#php} @@ -703,6 +698,38 @@ yc iam create-token return data.IAMToken } ``` + - Node.js {#node} + + Пример обмена JWT на IAM-токен: + + ```js + async getYCIAMToken(token) { + try { + + const response = await fetch(process.env.YC_IAM_URL, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({jwt: token}) + }); + + if(response.ok && response.status === 200) { + const data = await response.json(); + return data; + } + + else { + // обработка ошибки ответа + throw new Error(`Server response error - response.status: ${response.status}`); + } + } + catch(e) { + console.log(e); + } + } + + ``` {% endlist %} From ff5709068e75feb81c8874bc24738c00725f108f Mon Sep 17 00:00:00 2001 From: Roman Privalov Date: Wed, 14 Aug 2024 06:07:41 +0300 Subject: [PATCH 2/3] Update create-for-sa.md *Fix Node.js example for tokens exchange (mistake in MD list syntax) --- ru/iam/operations/iam-token/create-for-sa.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ru/iam/operations/iam-token/create-for-sa.md b/ru/iam/operations/iam-token/create-for-sa.md index 2c2baf4305..caa967e980 100644 --- a/ru/iam/operations/iam-token/create-for-sa.md +++ b/ru/iam/operations/iam-token/create-for-sa.md @@ -698,7 +698,7 @@ yc iam create-token return data.IAMToken } ``` - - Node.js {#node} +- Node.js {#node} Пример обмена JWT на IAM-токен: From 65aba9af48ce408feec27d966d1962e28c89f593 Mon Sep 17 00:00:00 2001 From: Roman Privalov Date: Wed, 14 Aug 2024 06:24:07 +0300 Subject: [PATCH 3/3] Update create-for-sa.md * Fix url for token exchange * Add comment for function param --- ru/iam/operations/iam-token/create-for-sa.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ru/iam/operations/iam-token/create-for-sa.md b/ru/iam/operations/iam-token/create-for-sa.md index caa967e980..49a1b629f9 100644 --- a/ru/iam/operations/iam-token/create-for-sa.md +++ b/ru/iam/operations/iam-token/create-for-sa.md @@ -703,10 +703,13 @@ yc iam create-token Пример обмена JWT на IAM-токен: ```js + /** + * @param {string} token - JWT, который был сформирован в примере выше + */ async getYCIAMToken(token) { try { - const response = await fetch(process.env.YC_IAM_URL, { + const response = await fetch('https://iam.api.cloud.yandex.net/iam/v1/tokens', { method: 'POST', headers: { 'Content-Type': 'application/json',