mirror of
https://github.com/github/codeql-action.git
synced 2025-12-26 17:20:10 +08:00
44 lines
1.4 KiB
JavaScript
44 lines
1.4 KiB
JavaScript
import { request as defaultRequest } from "@octokit/request";
|
|
import { oauthRequest } from "./utils.js";
|
|
async function exchangeDeviceCode(options) {
|
|
const request = options.request || defaultRequest;
|
|
const response = await oauthRequest(
|
|
request,
|
|
"POST /login/oauth/access_token",
|
|
{
|
|
client_id: options.clientId,
|
|
device_code: options.code,
|
|
grant_type: "urn:ietf:params:oauth:grant-type:device_code"
|
|
}
|
|
);
|
|
const authentication = {
|
|
clientType: options.clientType,
|
|
clientId: options.clientId,
|
|
token: response.data.access_token,
|
|
scopes: response.data.scope.split(/\s+/).filter(Boolean)
|
|
};
|
|
if ("clientSecret" in options) {
|
|
authentication.clientSecret = options.clientSecret;
|
|
}
|
|
if (options.clientType === "github-app") {
|
|
if ("refresh_token" in response.data) {
|
|
const apiTimeInMs = new Date(response.headers.date).getTime();
|
|
authentication.refreshToken = response.data.refresh_token, authentication.expiresAt = toTimestamp(
|
|
apiTimeInMs,
|
|
response.data.expires_in
|
|
), authentication.refreshTokenExpiresAt = toTimestamp(
|
|
apiTimeInMs,
|
|
response.data.refresh_token_expires_in
|
|
);
|
|
}
|
|
delete authentication.scopes;
|
|
}
|
|
return { ...response, authentication };
|
|
}
|
|
function toTimestamp(apiTimeInMs, expirationInSeconds) {
|
|
return new Date(apiTimeInMs + expirationInSeconds * 1e3).toISOString();
|
|
}
|
|
export {
|
|
exchangeDeviceCode
|
|
};
|