-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add authRefreshToken cookie * Ignore standaloneAjv for CodeQL --------- Signed-off-by: Carl Gieringer <78054+carlgieringer@users.noreply.github.com>
- Loading branch information
1 parent
170b2d6
commit a95df32
Showing
73 changed files
with
1,262 additions
and
361 deletions.
There are no files selected for viewing
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,38 @@ | ||
import { Moment } from "moment"; | ||
import { z } from "zod"; | ||
|
||
import { AuthRefreshToken, momentObject } from "howdju-common"; | ||
|
||
export const Cookie = z.object({ | ||
name: z.string(), | ||
value: z.string(), | ||
domain: z.string().optional(), | ||
path: z.string().optional(), | ||
expires: momentObject, | ||
secure: z.boolean().optional(), | ||
sameSite: z.enum(["strict", "lax", "none"]).optional(), | ||
httpOnly: z.boolean().optional(), | ||
}); | ||
export type Cookie = z.infer<typeof Cookie>; | ||
|
||
export const cookieNames = { | ||
AUTH_REFRESH_TOKEN: "auth-refresh-token", | ||
}; | ||
|
||
export function createAuthRefreshCookie( | ||
token: AuthRefreshToken, | ||
expires: Moment, | ||
isSecure: boolean | ||
): Cookie { | ||
return { | ||
name: cookieNames.AUTH_REFRESH_TOKEN, | ||
value: token, | ||
// undefined sets it to the current domain, ignoring subdomains. | ||
domain: undefined, | ||
path: "/", | ||
expires, | ||
secure: isSecure, | ||
sameSite: "strict", | ||
httpOnly: true, | ||
}; | ||
} |
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,100 @@ | ||
import { momentAdd, utcNow } from "howdju-common"; | ||
import { expectToBeSameMomentDeep, mockLogger } from "howdju-test-common"; | ||
|
||
import { | ||
endPoolAndDropDb, | ||
initDb, | ||
makeTestClientProvider, | ||
makeTestDbConfig, | ||
} from "@/util/testUtil"; | ||
import { Database, PoolClientProvider } from "../database"; | ||
import { makeTestProvider } from "@/initializers/TestProvider"; | ||
import TestHelper from "@/initializers/TestHelper"; | ||
import { AuthDao } from "./AuthDao"; | ||
|
||
const dbConfig = makeTestDbConfig(); | ||
|
||
describe("AuthDao", () => { | ||
let dbName: string; | ||
let clientProvider: PoolClientProvider; | ||
let database: Database; | ||
|
||
let dao: AuthDao; | ||
let testHelper: TestHelper; | ||
beforeEach(async () => { | ||
dbName = await initDb(dbConfig); | ||
|
||
clientProvider = makeTestClientProvider({ | ||
...dbConfig, | ||
database: dbName, | ||
}); | ||
database = new Database(mockLogger, clientProvider); | ||
|
||
const provider = makeTestProvider(database); | ||
|
||
dao = provider.authDao; | ||
testHelper = provider.testHelper; | ||
}); | ||
afterEach(async () => { | ||
await endPoolAndDropDb(clientProvider, dbConfig, dbName); | ||
}); | ||
|
||
describe("createAuthRefreshToken", () => { | ||
test("cannot create duplicate auth refresh tokens", async () => { | ||
const { user } = await testHelper.makeUser(); | ||
const { id: userId } = user; | ||
const authRefreshToken = "the-auth-refresh-token"; | ||
const created = utcNow(); | ||
const expires = momentAdd(utcNow(), { days: 1 }); | ||
await dao.createAuthRefreshToken( | ||
userId, | ||
authRefreshToken, | ||
created, | ||
expires | ||
); | ||
|
||
await expect( | ||
dao.createAuthRefreshToken(userId, authRefreshToken, created, expires) | ||
).rejects.toThrow("duplicate key value violates unique constraint"); | ||
}); | ||
}); | ||
describe("readAuthRefreshToken", () => { | ||
test("can read an auth refresh token", async () => { | ||
const { user } = await testHelper.makeUser(); | ||
const { id: userId } = user; | ||
const authRefreshToken = "the-auth-refresh-token"; | ||
const created = utcNow(); | ||
const expires = momentAdd(utcNow(), { days: 1 }); | ||
await dao.createAuthRefreshToken( | ||
userId, | ||
authRefreshToken, | ||
created, | ||
expires | ||
); | ||
|
||
const result = await dao.readAuthRefreshToken(authRefreshToken); | ||
|
||
expect(result).toEqual(expectToBeSameMomentDeep({ userId, expires })); | ||
}); | ||
}); | ||
describe("deleteAuthRefreshToken", () => { | ||
test("cannot read a deleted auth refresh token", async () => { | ||
const { user } = await testHelper.makeUser(); | ||
const { id: userId } = user; | ||
const authRefreshToken = "the-auth-refresh-token"; | ||
const created = utcNow(); | ||
const expires = momentAdd(utcNow(), { days: 1 }); | ||
await dao.createAuthRefreshToken( | ||
userId, | ||
authRefreshToken, | ||
created, | ||
expires | ||
); | ||
|
||
await dao.deleteAuthRefreshToken(authRefreshToken); | ||
|
||
const result = await dao.readAuthRefreshToken(authRefreshToken); | ||
expect(result).toBeUndefined(); | ||
}); | ||
}); | ||
}); |
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,20 @@ | ||
import moment from "moment"; | ||
|
||
import { mockLogger } from "howdju-test-common"; | ||
|
||
import { makeTimestampToUtcMomentParser } from "./pg"; | ||
|
||
const parser = makeTimestampToUtcMomentParser(mockLogger); | ||
|
||
describe("makeTimestampToUtcMomentParser", () => { | ||
test("parses a timezone-naive timestamp", () => { | ||
expect(parser("2024-08-31 21:24:30.45")).toBeSameMoment( | ||
moment.utc("2024-08-31 21:24:30.45") | ||
); | ||
}); | ||
test("parses a timezone-aware timestamp", () => { | ||
expect(parser("2024-11-29 20:20:00.119+00")).toBeSameMoment( | ||
moment.utc("2024-11-29 20:20:00.119+00") | ||
); | ||
}); | ||
}); |
Oops, something went wrong.