-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoids having to call DynamoDB on every request. Cache local value for 1 hour before calling GitHub again. This still requires the user to refresh / login with GitHub every 8 hours. Consider later using GitHub refresh tokens.
- Loading branch information
1 parent
752a649
commit 2ad8f92
Showing
24 changed files
with
230 additions
and
97 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
src/app/domain/entityStore/entities/GithubUserTokenEntity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { entityFromPkOnlyEntity, typePredicateParser } from '@symphoniacloud/dynamodb-entity-store' | ||
import { GITHUB_USER_TOKEN } from '../entityTypes' | ||
import { GithubUserToken, isGithubUserToken } from '../../types/GithubUserToken' | ||
|
||
export const GithubUserTokenEntity = entityFromPkOnlyEntity({ | ||
type: GITHUB_USER_TOKEN, | ||
parse: typePredicateParser(isGithubUserToken, GITHUB_USER_TOKEN), | ||
pk(source: Pick<GithubUserToken, 'token'>) { | ||
return `USER_TOKEN#${source.token}` | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { AppState } from '../../environment/AppState' | ||
import { GithubUserTokenEntity } from '../entityStore/entities/GithubUserTokenEntity' | ||
import { Clock, secondsTimestampInFutureHours, timestampSecondsIsInPast } from '../../util/dateAndTime' | ||
import { GithubUserToken } from '../types/GithubUserToken' | ||
|
||
const EXPIRE_CACHED_GITHUB_TOKENS_HOURS = 1 | ||
|
||
export async function saveOrRefreshGithubUserToken( | ||
appState: AppState, | ||
tokenRecord: Pick<GithubUserToken, 'token' | 'userId' | 'userLogin'> | ||
) { | ||
await appState.entityStore.for(GithubUserTokenEntity).put( | ||
{ | ||
...tokenRecord, | ||
nextCheckTime: secondsTimestampInFutureHours(appState.clock, EXPIRE_CACHED_GITHUB_TOKENS_HOURS) | ||
}, | ||
{ | ||
ttlInFutureDays: 7 | ||
} | ||
) | ||
} | ||
|
||
export async function getGithubUserTokenOrUndefined(appState: AppState, token: string) { | ||
return await appState.entityStore.for(GithubUserTokenEntity).getOrUndefined({ token }) | ||
} | ||
|
||
// If token record was saved more than EXPIRE_CACHED_GITHUB_TOKENS_HOURS ago then check user token with GitHub agaion | ||
export function isGithubCheckRequired(clock: Clock, token: GithubUserToken) { | ||
return timestampSecondsIsInPast(clock, token.nextCheckTime) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export interface GithubUserToken { | ||
token: string | ||
userId: number | ||
userLogin: string | ||
nextCheckTime: number | ||
} | ||
|
||
export function isGithubUserToken(x: unknown): x is GithubUserToken { | ||
const candidate = x as GithubUserToken | ||
return ( | ||
candidate.token !== undefined && | ||
candidate.userId !== undefined && | ||
candidate.userLogin !== undefined && | ||
candidate.nextCheckTime !== undefined | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { WithHeadersEvent } from '../../inboundInterfaces/lambdaTypes' | ||
|
||
export function getToken(event: WithHeadersEvent) { | ||
const tokenCookie = getTokenCookie(event) | ||
return tokenCookie ? tokenCookie.split('=')[1] : undefined | ||
} | ||
|
||
// Using both single and multi value headers because there may only be one cookie | ||
// if user manually delete "isLoggedIn" cookie, otherwise more than one | ||
function getTokenCookie(event: WithHeadersEvent) { | ||
const singleHeaderTokenCookie = (event.headers?.Cookie ?? '') | ||
.split(';') | ||
.map((x) => x.trim()) | ||
.find((x) => x.startsWith('token=')) | ||
|
||
if (singleHeaderTokenCookie) return singleHeaderTokenCookie | ||
|
||
return (event.multiValueHeaders?.Cookie ?? []).find((x) => x.startsWith('token=')) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.