Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure always a single slash between domain and path in callbacks #71

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/authMiddleware/authMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {NextResponse} from 'next/server';
import {config} from '../config/index';

const trimTrailingSlash = (str) =>
str.charAt(str.length - 1) === '/' ? str.slice(0, -1) : str;
str && str.charAt(str.length - 1) === '/' ? str.slice(0, -1) : str;

export function authMiddleware(request) {
let isAuthenticated = false;
Expand Down
6 changes: 5 additions & 1 deletion src/handlers/appRouter/callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {version} from '../../utils/version';
import {cookies} from 'next/headers';
import {redirect} from 'next/navigation';
import {sanitizeRedirect} from '../../utils/sanitizeRedirect';
import {generateCallbackUrl} from '../../utils/generateCallbackUrl';

export const callback = async (request) => {
const code = request.nextUrl.searchParams.get('code');
Expand Down Expand Up @@ -39,7 +40,10 @@ export const callback = async (request) => {
code: code,
code_verifier: code_verifier,
grant_type: 'authorization_code',
redirect_uri: `${config.redirectURL}${config.redirectRoutes.callback}`
redirect_uri: generateCallbackUrl(
config.redirectURL,
config.redirectRoutes.callback
)
})
}
);
Expand Down
11 changes: 6 additions & 5 deletions src/handlers/pageRouter/callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {config} from '../../config/index';
import {isTokenValid} from '../../utils/pageRouter/isTokenValid';
import {version} from '../../utils/version';
import {sanitizeRedirect} from '../../utils/sanitizeRedirect';
import {generateCallbackUrl} from '../../utils/generateCallbackUrl';

var cookie = require('cookie');

Expand All @@ -17,10 +18,7 @@ export const callback = async (req, res) => {

if (jsonCookieValue) {
try {
const {
code_verifier,
options,
} = JSON.parse(jsonCookieValue);
const {code_verifier, options} = JSON.parse(jsonCookieValue);

if (options?.post_login_redirect_url) {
redirectUrl = sanitizeRedirect({
Expand All @@ -43,7 +41,10 @@ export const callback = async (req, res) => {
code,
code_verifier,
grant_type: 'authorization_code',
redirect_uri: config.redirectURL + config.redirectRoutes.callback
redirect_uri: generateCallbackUrl(
config.redirectURL,
config.redirectRoutes.callback
)
})
}
);
Expand Down
6 changes: 5 additions & 1 deletion src/utils/generateAuthUrl.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import {config} from '../config/index';
import {generateCallbackUrl} from '../utils/generateCallbackUrl';

export function generateAuthUrl(options, type = 'login') {
const {org_code, is_create_org, org_name = ''} = options;
const authUrl = new URL(config.issuerURL + config.issuerRoutes[type]);

let searchParams = {
redirect_uri: config.redirectURL + config.redirectRoutes.callback,
redirect_uri: generateCallbackUrl(
config.redirectURL,
config.redirectRoutes.callback
),
client_id: config.clientID,
response_type: config.responseType,
scope: config.scope,
Expand Down
7 changes: 7 additions & 0 deletions src/utils/generateCallbackUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const generateCallbackUrl = (base, path) => {
const siteUrl = base.endsWith('/') ? base.slice(0, -1) : base;
const callbackPath = path.startsWith('/') ? path.substr(1) : path;
return `${siteUrl}/${callbackPath}`;
};

export {generateCallbackUrl};