Signed-off-by: Stephen Glass <stephen@stephen.glass>
This commit is contained in:
Stephen Glass
2024-07-29 00:45:41 -04:00
parent 672a4b4876
commit 17c9a1a330
2 changed files with 25 additions and 2 deletions
@@ -14,7 +14,6 @@
* limitations under the License.
*/
import { Response } from 'express';
import { CookieConfigurer } from '../types';
import { serializeError } from '@backstage/errors';
const ONE_MINUTE_MS = 60 * 1000;
@@ -29,7 +28,7 @@ function configureAuthErrorCookie(apiUrl: string, appOrigin: string) {
// different domain than the app, we set the SameSite attribute
// to 'none' to allow third-party access to the cookie, but
// only if it's in a secure context (https).
let sameSite: ReturnType<CookieConfigurer>['sameSite'] = 'lax';
let sameSite: 'lax' | 'none' = 'lax';
if (new URL(appOrigin).hostname !== domain && secure) {
sameSite = 'none';
}
@@ -742,5 +742,29 @@ describe('createOAuthRouteHandlers', () => {
},
});
});
it('should set cookie on caught error redirect', async () => {
const app = wrapInApp(createOAuthRouteHandlers(baseConfig));
const res = await request(app)
.get('/my-provider/handler/frame')
.query({
state: encodeOAuthState({
env: 'development',
nonce: '123',
flow: 'redirect',
redirectUrl: 'http://localhost:3000',
}),
});
// redirects on error with auth error cookie
expect(res.status).toBe(302);
const setCookieHeader = res.header['set-cookie'];
expect(setCookieHeader).toBeDefined();
const authErrorCookie = setCookieHeader.find((cookie: string) =>
cookie.startsWith('auth-error='),
);
expect(authErrorCookie).toBeDefined();
});
});
});