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
@@ -14,13 +14,14 @@
* limitations under the License.
*/
import { Handler } from 'express';
import PromiseRouter from 'express-promise-router';
import {
coreServices,
createServiceFactory,
HttpRouterServiceAuthPolicy,
} from '@backstage/backend-plugin-api';
import { Handler } from 'express';
import PromiseRouter from 'express-promise-router';
import { createCookieAuthRefreshMiddleware } from '@backstage/plugin-auth-node';
import { createLifecycleMiddleware } from './createLifecycleMiddleware';
import { createCredentialsBarrier } from './createCredentialsBarrier';
import { createAuthIntegrationRouter } from './createAuthIntegrationRouter';
@@ -85,12 +86,8 @@ export const httpRouterServiceFactory = createServiceFactory(
addAuthPolicy(policy: HttpRouterServiceAuthPolicy): void {
credentialsBarrier.addAuthPolicy(policy);
if (policy.allow === 'user-cookie') {
// Endpoint that sets the cookie for the user
// TODO: Extract this to a separate createCookieAuthRefreshMiddleware function
router.get('/.backstage/v1-cookie', async (_, res) => {
const { expiresAt } = await httpAuth.issueUserCookie(res);
res.json({ expiresAt: expiresAt.toISOString() });
});
// TODO: Make sure this is only added once
router.use(createCookieAuthRefreshMiddleware({ httpAuth }));
}
},
};