refactor: extract a create cookie middleware

Signed-off-by: Camila Belo <camilaibs@gmail.com>
This commit is contained in:
Camila Belo
2024-03-18 10:27:26 +01:00
committed by Patrik Oldsberg
parent 7b77befdfb
commit f17da85b08
9 changed files with 56 additions and 12 deletions
@@ -0,0 +1,37 @@
/*
* Copyright 2024 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { HttpAuthService } from '@backstage/backend-plugin-api';
import { Router } from 'express';
/**
* @public
* Creates a middleware that can be used to refresh the cookie for the user.
*/
export function createCookieAuthRefreshMiddleware(options: {
httpAuth: HttpAuthService;
}) {
const { httpAuth } = options;
const router = Router();
// Endpoint that sets the cookie for the user
router.get('/.backstage/v1-cookie', async (_, res) => {
const { expiresAt } = await httpAuth.issueUserCookie(res);
res.json({ expiresAt: expiresAt.toISOString() });
});
return router;
}
+1
View File
@@ -22,3 +22,4 @@ export {
} from './DefaultIdentityClient';
export { IdentityClient } from './IdentityClient';
export type { IdentityApi, IdentityApiGetIdentityRequest } from './IdentityApi';
export { createCookieAuthRefreshMiddleware } from './createCookieAuthRefreshMiddleware';