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

Improved invalid JWT handling #9058

Merged
merged 2 commits into from
Oct 22, 2021
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
20 changes: 10 additions & 10 deletions api/src/auth/drivers/oauth2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,18 @@ export function createOAuth2AuthRouter(providerName: string): Router {
router.get(
'/callback',
asyncHandler(async (req, res, next) => {
const token = req.cookies[`oauth2.${providerName}`];
let tokenData;

if (!token) {
try {
tokenData = jwt.verify(req.cookies[`oauth2.${providerName}`], env.SECRET as string, { issuer: 'directus' }) as {
verifier: string;
redirect?: string;
};
} catch (e) {
throw new InvalidCredentialsException();
}

const { verifier, redirect } = jwt.verify(token, env.SECRET as string, { issuer: 'directus' }) as {
verifier: string;
redirect: string;
};
const { verifier, redirect } = tokenData;

const authenticationService = new AuthenticationService({
accountability: {
Expand All @@ -236,10 +238,8 @@ export function createOAuth2AuthRouter(providerName: string): Router {
try {
res.clearCookie(`oauth2.${providerName}`);

const { code } = req.query;

if (!code) {
logger.warn(`Couldn't extract oAuth2 code from query: ${JSON.stringify(req.query)}`);
if (!req.query.code) {
logger.warn(`Couldn't extract OAuth2 code from query: ${JSON.stringify(req.query)}`);
}

authResponse = await authenticationService.login(providerName, {
Expand Down
21 changes: 16 additions & 5 deletions api/src/auth/drivers/openid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,18 @@ export function createOpenIDAuthRouter(providerName: string): Router {
router.get(
'/callback',
asyncHandler(async (req, res, next) => {
const token = req.cookies[`openid.${providerName}`];
const { verifier, redirect } = jwt.verify(token, env.SECRET as string, { issuer: 'directus' }) as {
verifier: string;
redirect: string;
};
let tokenData;

try {
tokenData = jwt.verify(req.cookies[`openid.${providerName}`], env.SECRET as string, { issuer: 'directus' }) as {
verifier: string;
redirect?: string;
};
} catch (e) {
throw new InvalidCredentialsException();
}

const { verifier, redirect } = tokenData;

const authenticationService = new AuthenticationService({
accountability: {
Expand All @@ -237,6 +244,10 @@ export function createOpenIDAuthRouter(providerName: string): Router {
try {
res.clearCookie(`openid.${providerName}`);

if (!req.query.code) {
logger.warn(`Couldn't extract OAuth2 code from query: ${JSON.stringify(req.query)}`);
}

authResponse = await authenticationService.login(providerName, {
code: req.query.code,
codeVerifier: verifier,
Expand Down
2 changes: 1 addition & 1 deletion api/src/types/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type AuthData = Record<string, any> | null;
export interface Session {
token: string;
expires: Date;
data: string | null;
data: string | Record<string, unknown> | null;
}

export type SessionData = Record<string, any> | null;