Skip to content

Commit

Permalink
feat: use 303 See Other HTTP response status code for built in redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Jun 23, 2021
1 parent f3c1d5f commit c243bf6
Show file tree
Hide file tree
Showing 42 changed files with 235 additions and 226 deletions.
1 change: 1 addition & 0 deletions certification/fapi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ if (process.env.NODE_ENV === 'production') {
default:
}
} else if (ctx.method === 'GET' || ctx.method === 'HEAD') {
ctx.status = 303;
ctx.redirect(ctx.href.replace(/^http:\/\//i, 'https://'));
} else {
ctx.body = {
Expand Down
1 change: 1 addition & 0 deletions certification/oidc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ let server;
default:
}
} else if (ctx.method === 'GET' || ctx.method === 'HEAD') {
ctx.status = 303;
ctx.redirect(ctx.href.replace(/^http:\/\//i, 'https://'));
} else {
ctx.body = {
Expand Down
1 change: 1 addition & 0 deletions example/koa.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ if (process.env.NODE_ENV === 'production') {
if (ctx.secure) {
await next();
} else if (ctx.method === 'GET' || ctx.method === 'HEAD') {
ctx.status = 303;
ctx.redirect(ctx.href.replace(/^http:\/\//i, 'https://'));
} else {
ctx.body = {
Expand Down
1 change: 1 addition & 0 deletions example/routes/koa.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ module.exports = (provider) => {
ctx.cookies.set('google.state', state, { path, sameSite: 'strict' });
ctx.cookies.set('google.nonce', nonce, { path, sameSite: 'strict' });

ctx.status = 303;
return ctx.redirect(ctx.google.authorizationUrl({
state, nonce, scope: 'openid email profile',
}));
Expand Down
1 change: 1 addition & 0 deletions example/standalone.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ let server;
if (ctx.secure) {
await next();
} else if (ctx.method === 'GET' || ctx.method === 'HEAD') {
ctx.status = 303;
ctx.redirect(ctx.href.replace(/^http:\/\//i, 'https://'));
} else {
ctx.body = {
Expand Down
3 changes: 2 additions & 1 deletion lib/actions/authorization/interactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ module.exports = async function interactions(resumeRouteName, ctx, next) {
throw new errors.CustomOIDCProviderError(failedCheck.error, failedCheck.error_description);
}
} catch (err) {
const code = /^(code|device)_/.test(oidc.route) ? 400 : 302;
const code = /^(code|device)_/.test(oidc.route) ? 400 : 303;
err.status = code;
err.statusCode = code;
err.expose = true;
Expand Down Expand Up @@ -148,5 +148,6 @@ module.exports = async function interactions(resumeRouteName, ctx, next) {
);

oidc.provider.emit('interaction.started', ctx, prompt);
ctx.status = 303;
ctx.redirect(destination);
};
1 change: 1 addition & 0 deletions lib/actions/end_session.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ module.exports = {

ctx.oidc.provider.emit('end_session.success', ctx);

ctx.status = 303;
ctx.redirect(uri);

await next();
Expand Down
2 changes: 1 addition & 1 deletion lib/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ class Provider extends events.EventEmitter {
async interactionFinished(req, res, result, { mergeWithLastSubmission = true } = {}) {
const returnTo = await this.interactionResult(req, res, result, { mergeWithLastSubmission });

res.statusCode = 302; // eslint-disable-line no-param-reassign
res.statusCode = 303; // eslint-disable-line no-param-reassign
res.setHeader('Location', returnTo);
res.setHeader('Content-Length', '0');
res.end();
Expand Down
1 change: 1 addition & 0 deletions lib/response_modes/fragment.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ const formatUri = require('../helpers/redirect_uri');

module.exports = (ctx, redirectUri, payload) => {
const uri = formatUri(redirectUri, payload, 'fragment');
ctx.status = 303;
ctx.redirect(uri);
};
1 change: 1 addition & 0 deletions lib/response_modes/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ const formatUri = require('../helpers/redirect_uri');

module.exports = (ctx, redirectUri, payload) => {
const uri = formatUri(redirectUri, payload, 'query');
ctx.status = 303;
ctx.redirect(uri);
};
10 changes: 5 additions & 5 deletions test/auth_time/auth_time.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('responds with a id_token containing auth_time', () => {
let id_token;

await this.wrap({ route: '/auth', verb: 'get', auth })
.expect(302)
.expect(303)
.expect(auth.validateFragment)
.expect(auth.validatePresence(['id_token', 'state']))
.expect(auth.validateState)
Expand Down Expand Up @@ -54,7 +54,7 @@ describe('responds with a id_token containing auth_time', () => {
let id_token;

await this.wrap({ route: '/auth', verb: 'get', auth })
.expect(302)
.expect(303)
.expect(auth.validateFragment)
.expect(auth.validatePresence(['id_token', 'state']))
.expect(auth.validateState)
Expand All @@ -76,7 +76,7 @@ describe('responds with a id_token containing auth_time', () => {
let id_token;

await this.wrap({ route: '/auth', verb: 'get', auth })
.expect(302)
.expect(303)
.expect(auth.validateFragment)
.expect(auth.validatePresence(['id_token', 'state']))
.expect(auth.validateState)
Expand All @@ -99,7 +99,7 @@ describe('responds with a id_token containing auth_time', () => {
let id_token;

await this.wrap({ route: '/auth', verb: 'get', auth })
.expect(302)
.expect(303)
.expect(auth.validateFragment)
.expect(auth.validatePresence(['id_token', 'state']))
.expect(auth.validateState)
Expand All @@ -121,7 +121,7 @@ describe('responds with a id_token containing auth_time', () => {
let id_token;

await this.wrap({ route: '/auth', verb: 'get', auth })
.expect(302)
.expect(303)
.expect(auth.validateFragment)
.expect(auth.validatePresence(['id_token', 'state']))
.expect(auth.validateState)
Expand Down
6 changes: 3 additions & 3 deletions test/authorization_code/code.grant.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('grant_type=authorization_code', () => {
response_type: 'code',
redirect_uri: 'https://client.example.com/cb',
})
.expect(302)
.expect(303)
.expect((response) => {
const { query: { code } } = parseUrl(response.headers.location, true);
const jti = this.getTokenJti(code);
Expand Down Expand Up @@ -310,7 +310,7 @@ describe('grant_type=authorization_code', () => {
response_type: 'code',
redirect_uri: 'https://client.example.com/cb3',
})
.expect(302)
.expect(303)
.expect((response) => {
const { query: { code } } = parseUrl(response.headers.location, true);
this.ac = code;
Expand Down Expand Up @@ -354,7 +354,7 @@ describe('grant_type=authorization_code', () => {
scope: 'openid',
response_type: 'code',
})
.expect(302)
.expect(303)
.expect((response) => {
const { query: { code } } = parseUrl(response.headers.location, true);
const jti = this.getTokenJti(code);
Expand Down
8 changes: 4 additions & 4 deletions test/backchannel_logout/backchannel_logout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('Back-Channel Logout 1.0', () => {
response_type: 'code id_token',
redirect_uri: 'https://client.example.com/cb',
})
.expect(302)
.expect(303)
.expect((response) => {
const { query } = parseUrl(response.headers.location.replace('#', '?'), true);
expect(query).to.have.property('code');
Expand Down Expand Up @@ -176,7 +176,7 @@ describe('Back-Channel Logout 1.0', () => {
return this.agent.post('/session/end/confirm')
.send(params)
.type('form')
.expect(302)
.expect(303)
.expect(() => {
(() => {
const { sid } = session.authorizations.client;
Expand Down Expand Up @@ -211,7 +211,7 @@ describe('Back-Channel Logout 1.0', () => {
return this.agent.post('/session/end/confirm')
.send(params)
.type('form')
.expect(302)
.expect(303)
.expect(() => {
expect(client.backchannelLogout.called).to.be.true;
expect(client.backchannelLogout.calledWith(accountId, sid)).to.be.true;
Expand All @@ -234,7 +234,7 @@ describe('Back-Channel Logout 1.0', () => {
return this.agent.post('/session/end/confirm')
.send(params)
.type('form')
.expect(302)
.expect(303)
.expect(() => {
expect(client.backchannelLogout.called).to.be.false;
client.backchannelLogout.restore();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ describe('features.mTLS.certificateBoundAccessTokens', () => {
});

await this.wrap({ route: '/auth', verb: 'get', auth })
.expect(302)
.expect(303)
.expect(auth.validateClientLocation)
.expect(({ headers: { location } }) => {
const { query: { code } } = url.parse(location, true);
Expand Down Expand Up @@ -376,7 +376,7 @@ describe('features.mTLS.certificateBoundAccessTokens', () => {
});

await this.wrap({ route: '/auth', verb: 'get', auth })
.expect(302)
.expect(303)
.expect(auth.validateClientLocation)
.expect(({ headers: { location } }) => {
const { query: { code } } = url.parse(location, true);
Expand Down
Loading

0 comments on commit c243bf6

Please sign in to comment.