Force the password-reset state to Unauthorized on most auth-required routes

This commit is contained in:
Savanni D'Gerinel 2025-02-16 15:54:34 -05:00
parent 41bb21c254
commit 0663a70c97
4 changed files with 15 additions and 28 deletions

View File

@ -13,4 +13,4 @@ tasks:
test:
cmds:
- npx jest src/
- npx jest

View File

@ -1,7 +1,8 @@
/** @type {import('ts-jest').JestConfigWithTsJest} **/
module.exports = {
testEnvironment: "node",
testMatch: [ "**/*.test.ts" ],
transform: {
"^.+.tsx?$": ["ts-jest",{}],
},
};
};

View File

@ -6,7 +6,7 @@ describe('what happens in an authentication', () => {
let response = await client.auth('vakarian', 'aoeu')
expect(response).toEqual({
status: 'ok',
content: 'vakarian-session-id',
content: { type: 'success', content: 'vakarian-session-id' },
})
})
@ -28,8 +28,8 @@ describe('what happens in an authentication', () => {
{
let response = await client.auth('shephard', 'aoeu')
expect(response).toEqual({
status: 'password-reset',
content: 'shephard-session-id',
status: 'ok',
content: { type: 'password-reset', content: 'shephard-session-id' },
})
}
{
@ -42,8 +42,8 @@ describe('what happens in an authentication', () => {
let client = new Connection(new URL('http://127.0.0.1:8001'))
{
let authResponse = await client.auth('vakarian', 'aoeu')
if (authResponse.status === 'ok') {
let sessionId = authResponse.content
if (authResponse.status === 'ok' && authResponse.content.type === 'success') {
let sessionId = authResponse.content.content
let response = await client.listUsers(sessionId)
expect(response).toEqual({
status: 'ok',
@ -74,12 +74,11 @@ describe('what happens in an authentication', () => {
}
{
let authResponse = await client.auth('shephard', 'aoeu')
if (authResponse.status === 'password-reset') {
let sessionId = authResponse.content
let response = await client.listUsers(sessionId)
expect(response).toEqual({ status: 'unauthorized' })
if (authResponse.status === 'ok' && authResponse.content.type === 'password-reset') {
let sessionId = authResponse.content.content
expect(await client.listUsers(sessionId)).toEqual({ status: 'unauthorized' })
} else {
throw new Error('authorization should have been password-reset')
throw new Error('Authorization shuld have been password-reset')
}
}
/*

View File

@ -1,17 +1,16 @@
import { VResponse, SessionId, UserOverview } from '../gen/types'
import { AuthResponse, SessionId, UserOverview } from '../gen/types'
export interface Client {
auth: (
username: string,
password: string,
) => Promise<ClientResponse<SessionId>>
) => Promise<ClientResponse<AuthResponse<SessionId>>>
listUsers: (sessionId: SessionId) => Promise<ClientResponse<UserOverview[]>>
}
export type ClientResponse<A> =
| { status: 'ok'; content: A }
| { status: 'password-reset'; content: SessionId }
| { status: 'unauthorized' }
| { status: 'unexpected'; code: number }
@ -25,7 +24,7 @@ export class Connection implements Client {
async auth(
username: string,
password: string,
): Promise<ClientResponse<SessionId>> {
): Promise<ClientResponse<AuthResponse<SessionId>>> {
const url = new URL(this.base)
url.pathname = `/api/test/auth`
const response = await fetch(url, {
@ -35,12 +34,6 @@ export class Connection implements Client {
})
if (response.ok) {
let resp = await response.json()
switch (resp.type) {
case 'success':
return { status: 'ok', content: resp.content }
case 'password-reset':
return { status: 'password-reset', content: resp.content }
}
return { status: 'ok', content: resp }
} else if (response.status == 401) {
return { status: 'unauthorized' }
@ -60,12 +53,6 @@ export class Connection implements Client {
})
if (response.ok) {
let resp = await response.json()
switch (resp.type) {
case 'success':
return { status: 'ok', content: resp.content }
case 'password-reset':
return { status: 'password-reset', content: resp.content }
}
return { status: 'ok', content: resp }
} else if (response.status == 401) {
return { status: 'unauthorized' }