【问题标题】:Deno way of creating adobe JWT token?Deno 创建 adobe JWT 令牌的方式?
【发布时间】:2022-09-23 18:56:47
【问题描述】:

NodeJS 有一个 adobe https://www.npmjs.com/package/@adobe/jwt-auth 支持的依赖项。

这不适用于 deno。有什么选择吗?

    标签: jwt adobe deno


    【解决方案1】:

    这是我经过大量调试后提出的可行解决方案。但是,非常欢迎对此进行适当的依赖!

    我很确定很多人都需要这样的东西。

    import { create } from "https://deno.land/x/djwt@v2.7/mod.ts";
    import "https://deno.land/x/dotenv/load.ts";
    
    const clientId = Deno.env.get("client_id") || "";
    const clientSecret = Deno.env.get("client_secret") || "";
    const technicalAccountId = Deno.env.get("technical_account_id") || "";
    const orgId = Deno.env.get("org_id") || "";
    const metaScopes = JSON.parse(Deno.env.get("meta_scopes") || "");
    const pemEncodedPrivateKey = Deno.env.get("private_key") || "";
    
    const PRIVATE_KEY_HEADER = "-----BEGIN PRIVATE KEY-----";
    const PRIVATE_KEY_FOOTER = "-----END PRIVATE KEY-----";
    const ims = "https://ims-na1.adobelogin.com";
    
    const str2ab = (str: string) => {
      const buf = new ArrayBuffer(str.length);
      const bufView = new Uint8Array(buf);
      for (let i = 0, strLen = str.length; i < strLen; i++) {
        bufView[i] = str.charCodeAt(i);
      }
      return buf;
    };
    
    const generateAdobeToken = async (): Promise<string | null> => {
      console.log(
        "*** Generating Authentication-Token for the Adobe-Target Api ***"
      );
      const rawBase64PrivateKey = pemEncodedPrivateKey
        .substring(
          PRIVATE_KEY_HEADER.length,
          pemEncodedPrivateKey.length - PRIVATE_KEY_FOOTER.length
        )
        .trim();
      const binaryDerString = atob(rawBase64PrivateKey);
      const binaryDer = str2ab(binaryDerString);
    
      const key = await crypto.subtle.importKey(
        "pkcs8",
        binaryDer,
        {
          name: "RSASSA-PKCS1-v1_5",
          hash: "SHA-256",
        },
        true,
        ["sign"]
      );
    
      const jwtPayload = {
        exp: Math.round(300 + Date.now() / 1000),
        iss: orgId,
        sub: technicalAccountId,
        aud: `${ims}/c/${clientId}`,
      };
    
      (jwtPayload as any)[`${ims}/s/${metaScopes[0]}`] = true;
    
      let token;
      try {
        token = await create({ alg: "RS256", typ: "JWT" }, jwtPayload, key);
      } catch (e) {
        console.error("Local token generation failed.");
        throw e;
      }
    
      const form = new FormData();
      form.append("client_id", clientId);
      form.append("client_secret", clientSecret);
      form.append("jwt_token", token);
    
      const postOptions = {
        method: "POST",
        body: form,
      };
    
      console.log("\n*** Retrieving auth-token from adobe ***");
      return fetch(`${ims}/ims/exchange/jwt/`, postOptions)
        .catch((e) => {
          console.error("Could not fetch token from adobe: Call failed!", e);
          throw e;
        })
        .then((res) => {
          return res.json().then((data) => {
            return {
              ok: res.ok,
              json: data,
            };
          });
        })
        .then(({ ok, json }) => {
          const { access_token, error, error_description } = json;
          if (ok && access_token) {
            return json.access_token;
          }
    
          if (error && error_description) {
            const swapError = new Error(error_description);
            (swapError as any).code = error;
            throw swapError;
          } else {
            console.error(
              `The response body is as follows: ${JSON.stringify(json)}`
            );
          }
          return null;
        });
    };
    
    export const requestAdobeApiToken = async () => {
      try {
        return await generateAdobeToken();
      } catch (e) {
        console.error("Requesting token failed!", e);
        return null;
      }
    };
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-20
      • 2021-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-25
      • 2017-05-28
      • 2019-03-27
      相关资源
      最近更新 更多