Skip to content

Commit

Permalink
change return value for function creating access tokens so it contain…
Browse files Browse the repository at this point in the history
…s full token data
  • Loading branch information
ldgit committed Sep 23, 2024
1 parent 0ea449d commit 621f375
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
23 changes: 19 additions & 4 deletions library/oauth2/accessToken.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,29 @@ describe("generating access token", () => {
userId,
codeChallenge,
});
const expectedCreationDate = new Date();

const {
id,
value,
scope,
expiresIn,
authorizationTokenId,
clientId,
userId: actualUserId,
createdAt,
} = await createAccessTokenForAuthorizationToken(authorizationToken);

const { value, scope, expiresIn } =
await createAccessTokenForAuthorizationToken(authorizationToken);

expect(value).not.toBeFalsy();
expect(value.length).toEqual(64);
expect(scope).toEqual("openid");
expect(expiresIn).toStrictEqual(86400);
expect(clientId).toEqual(DUMMY_CLIENT_ID);
expect(actualUserId).toEqual(userId);
expect(differenceInSeconds(createdAt, expectedCreationDate)).toBeLessThan(2);
expect(id).toEqual((await findAccessTokenByValue(value))?.id);
expect(authorizationTokenId).toStrictEqual(
(await findAuthorizationTokenByCode(authorizationToken))?.id,
);
});

it("generating access token should throw error if provided authorization token does not exist", async () => {
Expand Down
23 changes: 16 additions & 7 deletions library/oauth2/accessToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export interface AccessTokenData {
*/
export async function createAccessTokenForAuthorizationToken(
authorizationToken: string,
): Promise<{ value: string; expiresIn: number; scope: string }> {
): Promise<AccessTokenData> {
const value = cryptoRandomString({
length: 64,
characters: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-._~",
Expand All @@ -33,25 +33,34 @@ export async function createAccessTokenForAuthorizationToken(

const queryResult = await query(
"SELECT id FROM access_tokens WHERE authorization_token_id = $1",
[authorizationTokenData.id],
[authorizationTokenData.id.toString()],
);
if (queryResult.rowCount !== null && queryResult.rowCount > 0) {
throw new Error("Authorization code already has an access token.");
}

await query(
"INSERT INTO access_tokens(value, scope, client_id, user_id, authorization_token_id, expires_in) VALUES($1, $2, $3, $4, $5, $6)",
const insertResult = await query(
"INSERT INTO access_tokens(value, scope, client_id, user_id, authorization_token_id, expires_in) VALUES($1, $2, $3, $4, $5, $6) RETURNING *",
[
value,
authorizationTokenData?.scope,
authorizationTokenData?.clientId,
authorizationTokenData?.userId,
authorizationTokenData?.id,
authorizationTokenData?.id.toString(),
"86400",
],
);

return { value, expiresIn: 86400, scope: authorizationTokenData?.scope };
return {
value,
expiresIn: 86400,
scope: authorizationTokenData?.scope,
authorizationTokenId: authorizationTokenData.id,
clientId: authorizationTokenData.clientId,
createdAt: insertResult.rows[0].created_at,
id: insertResult.rows[0].id,
userId: authorizationTokenData?.userId,
};
}

export async function findAccessTokenByValue(accessToken: string): Promise<AccessTokenData | null> {
Expand Down Expand Up @@ -102,6 +111,6 @@ export async function revokeAccessTokenIssuedByAuthorizationToken(authorizationT

await revokeAuthorizationToken(authorizationToken);
await query("DELETE FROM access_tokens WHERE authorization_token_id = $1", [
authorizationCodeData.id,
authorizationCodeData.id.toString(),
]);
}
2 changes: 1 addition & 1 deletion library/oauth2/authorizationToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { differenceInSeconds, isAfter } from "date-fns";
import { query } from "../../database/database.js";

export interface AuthorizationTokenData {
id: string;
id: number;
value: string;
scope: string;
createdAt: Date;
Expand Down

0 comments on commit 621f375

Please sign in to comment.