github.com/jfrog/frogbot@v1.1.1-0.20231221090046-821a26f50338/action/node_modules/@octokit/auth-token/dist-web/index.js (about)

     1  const REGEX_IS_INSTALLATION_LEGACY = /^v1\./;
     2  const REGEX_IS_INSTALLATION = /^ghs_/;
     3  const REGEX_IS_USER_TO_SERVER = /^ghu_/;
     4  async function auth(token) {
     5      const isApp = token.split(/\./).length === 3;
     6      const isInstallation = REGEX_IS_INSTALLATION_LEGACY.test(token) ||
     7          REGEX_IS_INSTALLATION.test(token);
     8      const isUserToServer = REGEX_IS_USER_TO_SERVER.test(token);
     9      const tokenType = isApp
    10          ? "app"
    11          : isInstallation
    12              ? "installation"
    13              : isUserToServer
    14                  ? "user-to-server"
    15                  : "oauth";
    16      return {
    17          type: "token",
    18          token: token,
    19          tokenType,
    20      };
    21  }
    22  
    23  /**
    24   * Prefix token for usage in the Authorization header
    25   *
    26   * @param token OAuth token or JSON Web Token
    27   */
    28  function withAuthorizationPrefix(token) {
    29      if (token.split(/\./).length === 3) {
    30          return `bearer ${token}`;
    31      }
    32      return `token ${token}`;
    33  }
    34  
    35  async function hook(token, request, route, parameters) {
    36      const endpoint = request.endpoint.merge(route, parameters);
    37      endpoint.headers.authorization = withAuthorizationPrefix(token);
    38      return request(endpoint);
    39  }
    40  
    41  const createTokenAuth = function createTokenAuth(token) {
    42      if (!token) {
    43          throw new Error("[@octokit/auth-token] No token passed to createTokenAuth");
    44      }
    45      if (typeof token !== "string") {
    46          throw new Error("[@octokit/auth-token] Token passed to createTokenAuth is not a string");
    47      }
    48      token = token.replace(/^(token|bearer) +/i, "");
    49      return Object.assign(auth.bind(null, token), {
    50          hook: hook.bind(null, token),
    51      });
    52  };
    53  
    54  export { createTokenAuth };
    55  //# sourceMappingURL=index.js.map