Skip to content

Commit e3c520b

Browse files
committed
track and evaluate createdAt for identity
1 parent 1e19dce commit e3c520b

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

src/models/schemas/Identity.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import Joi from 'joi';
1717

1818
export const metaSchema = Joi.object({
1919
name: Joi.string().required(),
20+
createdAt: Joi.string().required(),
2021
}).label('IdentityMeta');
2122

2223
export const identity = {

src/services/GithubService.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { Identity } from '../models/interfaces';
2020
import crypto from 'crypto';
2121
import { IdentityModel } from '../models/IdentityModel';
2222
import { StateLockRequest } from '../models/interfaces/StateLockRequest';
23+
import moment from 'moment';
2324
// import seedrandom from 'seedrandom';
2425

2526
export type IdentityWithToken = Identity & {
@@ -117,6 +118,7 @@ export class GithubService {
117118
workspace?: string,
118119
stateLockRequest?: StateLockRequest,
119120
): Promise<Identity> => {
121+
const now = moment();
120122
const tokenSha = crypto.createHash('sha256').update(auth).digest().toString('base64');
121123

122124
console.log(
@@ -140,6 +142,16 @@ export class GithubService {
140142
return { ...storedIdentity.attrs, workspace: workspace || 'default' };
141143
}
142144

145+
if (
146+
storedIdentity &&
147+
!stateLockRequest &&
148+
storedIdentity.attrs.meta.createdAt &&
149+
moment(storedIdentity.attrs.meta.createdAt).add(1, 'hour').isAfter(now)
150+
) {
151+
console.log(`Found previously known identity used within 1 hour (sha: ${tokenSha})`);
152+
return { ...storedIdentity.attrs, workspace: workspace || 'default' };
153+
}
154+
143155
const octokit = new Octokit({ auth });
144156

145157
// Server-to-server tokens from GH Actions are permitted on a single repository
@@ -176,6 +188,7 @@ export class GithubService {
176188
tokenSha,
177189
meta: {
178190
name,
191+
createdAt: now.toISOString(),
179192
},
180193
};
181194
}
@@ -197,6 +210,7 @@ export class GithubService {
197210
tokenSha,
198211
meta: {
199212
name,
213+
createdAt: now.toISOString(),
200214
},
201215
};
202216
}
@@ -218,6 +232,7 @@ export class GithubService {
218232
tokenSha,
219233
meta: {
220234
name,
235+
createdAt: now.toISOString(),
221236
},
222237
};
223238
}

0 commit comments

Comments
 (0)