Skip to content

Commit 7e56f90

Browse files
committed
Linting, fixes, and small cleanup
1 parent 6bf7260 commit 7e56f90

7 files changed

Lines changed: 41 additions & 39 deletions

File tree

src/lib/server/oidc.ts

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { browser, dev } from '$app/environment';
1+
import { dev } from '$app/environment';
22
import { env } from '$env/dynamic/private';
33
import type { MaybeToken, Rule } from '$lib/types/oidc';
44
import { type Cookies, type RequestEvent } from '@sveltejs/kit';
@@ -51,10 +51,18 @@ function getSupportedAlgorithms(): jwt.Algorithm[] {
5151
* - Your IdP's token mapper configuration
5252
*/
5353
export const CLAIMS_CONFIG = {
54-
get namespace() { return env.OIDC_CLAIMS_NAMESPACE || 'https://hasura.io/jwt/claims'; },
55-
get userId() { return env.OIDC_CLAIMS_USER_ID || 'x-hasura-user-id'; },
56-
get allowedRoles() { return env.OIDC_CLAIMS_ALLOWED_ROLES || 'x-hasura-allowed-roles'; },
57-
get defaultRole() { return env.OIDC_CLAIMS_DEFAULT_ROLE || 'x-hasura-default-role'; },
54+
get allowedRoles() {
55+
return env.OIDC_CLAIMS_ALLOWED_ROLES || 'x-hasura-allowed-roles';
56+
},
57+
get defaultRole() {
58+
return env.OIDC_CLAIMS_DEFAULT_ROLE || 'x-hasura-default-role';
59+
},
60+
get namespace() {
61+
return env.OIDC_CLAIMS_NAMESPACE || 'https://hasura.io/jwt/claims';
62+
},
63+
get userId() {
64+
return env.OIDC_CLAIMS_USER_ID || 'x-hasura-user-id';
65+
},
5866
};
5967

6068
/**
@@ -66,9 +74,9 @@ export const CLAIMS_CONFIG = {
6674
* @throws Error if required claims are missing
6775
*/
6876
export function extractClaims(token: jwt.JwtPayload): {
69-
userId: string;
7077
allowedRoles: string[];
7178
defaultRole: string;
79+
userId: string;
7280
} {
7381
const namespace = token[CLAIMS_CONFIG.namespace];
7482
if (!namespace || typeof namespace !== 'object') {
@@ -88,10 +96,12 @@ export function extractClaims(token: jwt.JwtPayload): {
8896
);
8997
}
9098
if (!defaultRole || typeof defaultRole !== 'string') {
91-
throw new Error(`JWT missing or invalid default role claim: ${CLAIMS_CONFIG.namespace}.${CLAIMS_CONFIG.defaultRole}`);
99+
throw new Error(
100+
`JWT missing or invalid default role claim: ${CLAIMS_CONFIG.namespace}.${CLAIMS_CONFIG.defaultRole}`,
101+
);
92102
}
93103

94-
return { userId, allowedRoles, defaultRole };
104+
return { allowedRoles, defaultRole, userId };
95105
}
96106

97107
/**
@@ -240,8 +250,8 @@ export function verifyNonce(idToken: string, expectedNonce: string): void {
240250
*
241251
*/
242252
export class Client {
243-
private static _instance: Client;
244253
private static _initPromise: Promise<Client>;
254+
private static _instance: Client;
245255

246256
private authorizationEndpoint!: string;
247257
private client!: arctic.OAuth2Client;

src/lib/stores/auth.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/routes/+layout.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,6 @@
11
import '../css/app.css';
22
import type { LayoutLoad } from './$types';
33

4-
import { browser } from '$app/environment';
5-
import { createClient } from 'graphql-ws';
6-
import { gqlWsClient, userStore } from '../lib/stores/auth';
7-
import { getClientOptions } from '../stores/subscribable';
8-
94
export const load: LayoutLoad = async ({ data }) => {
10-
if (browser) {
11-
userStore.set(data.user);
12-
gqlWsClient.set(createClient(getClientOptions()));
13-
}
14-
15-
// no PageData should be used client-side. but if it is accessed in other +page.ts or +layout.ts files, that should be okay, for SSR purposes. but anywhere on client, userStore should be used.
165
return { ...data };
176
};

src/routes/auth/changeRole/+server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { dev } from '$app/environment';
1+
import { base } from '$app/paths';
22
import type { RequestHandler } from '@sveltejs/kit';
33
import { json } from '@sveltejs/kit';
44
import type { CookieSerializeOptions } from 'cookie';
5-
import { computeRolesFromJWT } from '../../../hooks.server';
65
import type { ChangeUserRoleRequestBody } from '../../../types/auth';
6+
import { computeRolesFromJWT } from '../../../utilities/auth';
77

88
export const POST: RequestHandler = async event => {
99
const body: ChangeUserRoleRequestBody = await event.request.json();

src/routes/login/+page.svelte

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,21 @@
88
import { Button, Input, Label } from '@nasa-jpl/stellar-svelte';
99
import AlertError from '../../components/ui/AlertError.svelte';
1010
import { SearchParameters } from '../../enums/searchParameters';
11-
import { userStore } from '../../lib/stores/auth';
11+
import { getUserStore } from '../../stores/user';
1212
import type { LoginResponseBody } from '../../types/auth';
1313
import { EXPIRED_JWT, hasNoAuthorization } from '../../utilities/permissions';
1414
import { removeQueryParam } from '../../utilities/url';
1515
16+
const user = getUserStore();
17+
1618
let error: string | null = null;
1719
let fullError: string | null = null;
1820
let loginButtonText = 'Login';
1921
let password = '';
2022
let reason = $page.url.searchParams.get(SearchParameters.REASON);
2123
let username = '';
2224
23-
$: if ($userStore?.permissibleQueries && hasNoAuthorization($userStore)) {
25+
$: if ($user?.permissibleQueries && hasNoAuthorization($user)) {
2426
error = 'You are not authorized';
2527
fullError =
2628
'You are not authorized to access the page that you attempted to view. Please contact a tool administrator to request access.';
@@ -85,9 +87,7 @@
8587

8688
{#if isOidcEnabled()}
8789
<fieldset class="pt-4">
88-
<div>
89-
<Button type="button" on:click={() => goto(`${base}/oidc/login`)}>Login Using OIDC</Button>
90-
</div>
90+
<Button size="lg" type="button" on:click={() => goto(`${base}/oidc/login`)}>Login Using OIDC</Button>
9191
</fieldset>
9292
{:else}
9393
<fieldset>

src/routes/workspaces/[workspaceId]/actions/+page.svelte

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
<script lang="ts">
44
import PageTitle from '../../../../components/app/PageTitle.svelte';
55
import Actions from '../../../../components/sequencing/actions/Actions.svelte';
6-
import { userStore } from '../../../../lib/stores/auth';
6+
import { getUserStore } from '../../../../stores/user';
77
import type { PageData } from './$types';
88
9+
const user = getUserStore();
10+
911
export let data: PageData;
1012
1113
const { initialWorkspace } = data;
1214
</script>
1315

1416
<PageTitle title="Workspace: {initialWorkspace?.name} - Actions" />
1517

16-
<Actions user={$userStore} workspace={initialWorkspace} />
18+
<Actions user={$user} workspace={initialWorkspace} />

src/utilities/requests.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import type { BaseUser, User } from '../types/app';
44
import type { BaseError, LogMessage } from '../types/errors';
55
import type { ExtensionPayload, ExtensionResponse } from '../types/extension';
66
import type { QueryVariables } from '../types/subscribable';
7-
import { INVALID_JWT } from '../utilities/permissions';
87
import { ErrorTypes } from './errors';
8+
import { INVALID_JWT } from './permissions';
99

1010
/**
1111
* Used to make calls to application external to Aerie.
@@ -239,8 +239,16 @@ export async function reqHasura<T = any>(
239239
}
240240
}
241241
} else if (code === INVALID_JWT) {
242-
// awaiting here only works if SSR is disabled
243-
logout(error?.message);
242+
// This should never be triggered in the OIDC case, because we have refreshes.
243+
// In any case, we do the following:
244+
// * Display an error message.
245+
// * Tell the user they need to log in again
246+
// * Provide a way to do so.
247+
// Don't automatically initiate logout.
248+
console.error('Expired JWT in reqHasura for query:', query);
249+
throw new Error(
250+
`JWT Expired in reqHasura.\nCited Reason: ${json.errors[0]?.message ?? error?.message}\nFor query: ${query}.`,
251+
);
244252
} else {
245253
errors.push({
246254
...defaultError,

0 commit comments

Comments
 (0)