Skip to content

Commit

Permalink
Merge pull request #71 from kinde-oss/dave/trim-callbacks
Browse files Browse the repository at this point in the history
Make sure always a single slash between domain and path in callbacks
  • Loading branch information
marcosmartini authored Oct 19, 2023
2 parents 4b04387 + bb39f43 commit 1654801
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
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};

0 comments on commit 1654801

Please sign in to comment.