github.com/nektos/act@v0.2.63/pkg/runner/testdata/actions/node12/node_modules/@octokit/auth-token/dist-node/index.js (about)

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