Skip to content

Commit

Permalink
feat(action): log token hash
Browse files Browse the repository at this point in the history
  • Loading branch information
qoomon committed Jul 22, 2024
1 parent 7d65f1c commit fbc39b9
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
3 changes: 0 additions & 3 deletions action/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,3 @@ build:

## Resources
* App icon: https://img.icons8.com/cotton/256/000000/grand-master-key.png

## TODO
- add token hash to output in main and post action
3 changes: 2 additions & 1 deletion action/dist/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61153,13 +61153,14 @@ runAction(async () => {
if (input.repository) {
input.repositories.unshift(input.repository);
}
core.info('Get access token.');
core.info('Get access token...');
const accessToken = await getAccessToken({
scope: input.scope,
permissions: input.permissions,
repositories: input.repositories,
owner: input.owner,
});
core.info('Access token hash: ' + accessToken.token_hash);
core.setSecret(accessToken.token);
core.setOutput('token', accessToken.token);
// save token to state to be able to revoke it in post-action
Expand Down
8 changes: 5 additions & 3 deletions action/src/action-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ runAction(async () => {
input.repositories.unshift(input.repository);
}

core.info('Get access token.');
core.info('Get access token...');
const accessToken = await getAccessToken({
scope: input.scope,
permissions: input.permissions,
repositories: input.repositories,
owner: input.owner,
});
core.info('Access token hash: ' + accessToken.token_hash);

core.setSecret(accessToken.token);
core.setOutput('token', accessToken.token);
Expand Down Expand Up @@ -133,10 +134,11 @@ async function httpRequest(request: HttpRequest, options?: {

interface GitHubAccessTokenResponse {
token: string
token_hash: string
expires_at: string
owner: string
repositories: string[]
permissions: GitHubAppPermissions
repositories: string[]
owner: string
}

type GitHubAppPermissions = Record<string, string>
Expand Down
17 changes: 10 additions & 7 deletions server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ import process from 'process';
import {hasEntries, toBase64} from './common/common-utils.js';
import {buildJwksKeyFetcher} from './common/jwt-utils.js';
import {
GitHubActionsJwtPayload, GitHubAppPermissions,
GitHubAppPermissionsSchema, GitHubAppRepositoryPermissions,
GitHubRepositoryOwnerSchema, GitHubRepositoryNameSchema,
GitHubActionsJwtPayload,
GitHubAppPermissions,
GitHubAppPermissionsSchema,
GitHubAppRepositoryPermissions,
GitHubRepositoryNameSchema,
GitHubRepositoryOwnerSchema,
normalizePermissionScopes,
parseRepository,
verifyRepositoryPermissions,
Expand Down Expand Up @@ -97,8 +100,8 @@ app.post(
const invalidRepositoryPermissionScopes = verifyRepositoryPermissions(it.permissions).invalid;
if (hasEntries(invalidRepositoryPermissionScopes)) {
throw new HTTPException(Status.BAD_REQUEST, {
message: `Invalid permissions scopes for token scope 'repos'.\n${
Object.keys(invalidRepositoryPermissionScopes).map((scope) => `- ${scope}`).join('\n')}`,
message: `Invalid permissions scopes for token scope 'repos'.\n` +
Object.keys(invalidRepositoryPermissionScopes).map((scope) => `- ${scope}`).join('\n'),
});
}

Expand All @@ -123,17 +126,17 @@ app.post(
// --- response with requested access token --------------------------------------------------------------------
const tokenResponseBody = {
token: githubActionsAccessToken.token,
token_hash: await sha256(githubActionsAccessToken.token).then(toBase64),
expires_at: githubActionsAccessToken.expires_at,
permissions: githubActionsAccessToken.permissions ?
normalizePermissionScopes(githubActionsAccessToken.permissions) : undefined,
normalizePermissionScopes(githubActionsAccessToken.permissions) : undefined,
repositories: githubActionsAccessToken.repositories?.map((it) => it.name),
owner: githubActionsAccessToken.owner,
};

requestLog.info({
...tokenResponseBody,
token: undefined,
token_hash: await sha256(githubActionsAccessToken.token).then(toBase64),
}, 'Access Token');

return context.json(tokenResponseBody);
Expand Down

0 comments on commit fbc39b9

Please sign in to comment.