refactor: apply review suggestions

Co-authored-by: Patrik Oldsberg <poldsberg@gmail.com>
Signed-off-by: Camila Belo <camilaibs@gmail.com>
This commit is contained in:
Camila Belo
2024-04-03 13:03:53 +02:00
committed by Patrik Oldsberg
parent c884b9a478
commit ffd71105a6
43 changed files with 351 additions and 369 deletions
@@ -23,8 +23,9 @@ describe('createCookieAuthRefreshMiddleware', () => {
let app: express.Express;
beforeAll(async () => {
const auth = mockServices.auth();
const httpAuth = mockServices.httpAuth();
const router = createCookieAuthRefreshMiddleware({ httpAuth });
const router = createCookieAuthRefreshMiddleware({ auth, httpAuth });
app = express().use(router);
});
@@ -33,7 +34,7 @@ describe('createCookieAuthRefreshMiddleware', () => {
});
it('should issue the user cookie', async () => {
const response = await request(app).get('/.backstage/v1-cookie');
const response = await request(app).get('/.backstage/auth/v1/cookie');
expect(response.status).toBe(200);
expect(response.header['set-cookie'][0]).toMatch(
`backstage-auth=${mockCredentials.limitedUser.token()}`,
@@ -41,7 +42,7 @@ describe('createCookieAuthRefreshMiddleware', () => {
});
it('should remove the user cookie', async () => {
const response = await request(app).delete('/.backstage/v1-cookie');
const response = await request(app).delete('/.backstage/auth/v1/cookie');
expect(response.status).toBe(200);
expect(response.header['set-cookie'][0]).toMatch('backstage-auth=');
});
@@ -14,19 +14,20 @@
* limitations under the License.
*/
import { HttpAuthService } from '@backstage/backend-plugin-api';
import { AuthService, HttpAuthService } from '@backstage/backend-plugin-api';
import { Router } from 'express';
const WELL_KNOWN_COOKIE_PATH_V1 = '/.backstage/v1-cookie';
const WELL_KNOWN_COOKIE_PATH_V1 = '/.backstage/auth/v1/cookie';
/**
* @public
* Creates a middleware that can be used to refresh the cookie for the user.
*/
export function createCookieAuthRefreshMiddleware(options: {
auth: AuthService;
httpAuth: HttpAuthService;
}) {
const { httpAuth } = options;
const { auth, httpAuth } = options;
const router = Router();
// Endpoint that sets the cookie for the user
@@ -37,7 +38,8 @@ export function createCookieAuthRefreshMiddleware(options: {
// Endpoint that removes the cookie for the user
router.delete(WELL_KNOWN_COOKIE_PATH_V1, async (_, res) => {
httpAuth.removeUserCookie(res);
const credentials = await auth.getNoneCredentials();
await httpAuth.issueUserCookie(res, { credentials });
res.send(200);
});