Skip to content

Commit

Permalink
Force refresh OBO token every 15 min from browser
Browse files Browse the repository at this point in the history
  • Loading branch information
cskrov committed Sep 12, 2024
1 parent ea466fe commit 26a2842
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
17 changes: 10 additions & 7 deletions frontend/src/components/smart-editor/tabbed-editors/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,16 @@ const EditorWithNewCommentAndFloatingToolbar = ({ id }: { id: string }) => {
useEffect(() => {
console.log('isConnected', isConnected);

const interval = setInterval(() => {
try {
fetch('/refresh-obo-access-token/kabal-api');
} catch (e) {
console.warn('Failed to refresh OBO token.', e);
}
}, 10_000);
const interval = setInterval(
() => {
try {
fetch('/refresh-obo-access-token/kabal-api');
} catch (e) {
console.warn('Failed to refresh OBO token.', e);
}
},
15 * 60 * 1_000,
);

return () => clearInterval(interval);
}, [isConnected]);
Expand Down
2 changes: 1 addition & 1 deletion server/src/auth/on-behalf-of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const getOnBehalfOfAccessToken = async (
return token;
};

const refreshOnBehalfOfAccessToken = async (
export const refreshOnBehalfOfAccessToken = async (
authClient: Client,
accessToken: string,
cacheKey: string,
Expand Down
8 changes: 6 additions & 2 deletions server/src/plugins/obo-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { FastifyReply, FastifyRequest } from 'fastify';
import { getAzureADClient } from '@app/auth/get-auth-client';
import { getDuration } from '@app/helpers/duration';
import { getLogger } from '@app/logger';
import { getOnBehalfOfAccessToken } from '@app/auth/on-behalf-of';
import { getOnBehalfOfAccessToken, refreshOnBehalfOfAccessToken } from '@app/auth/on-behalf-of';
import fastifyPlugin from 'fastify-plugin';
import { isDeployed } from '@app/config/env';
import { oboRequestDuration } from '@app/auth/cache/cache-gauge';
Expand Down Expand Up @@ -71,8 +71,12 @@ export const oboAccessTokenPlugin = fastifyPlugin(
{ schema: { params: Type.Object({ appName: Type.Enum(ApiClientEnum) }) } },
async (req, reply) => {
const { appName } = req.params;
const { navIdent, accessToken, trace_id, span_id } = req;

await getOboToken(appName, req, reply);
const authClient = await getAzureADClient();
const cacheKey = `${navIdent}-${appName}`;

await refreshOnBehalfOfAccessToken(authClient, accessToken, cacheKey, appName, trace_id, span_id);

return reply.status(200).send('Refreshed');
},
Expand Down

0 comments on commit 26a2842

Please sign in to comment.