From e142a2767f72cc0c1308c07a1b1b193a4501687b Mon Sep 17 00:00:00 2001 From: Mahmood Hosseini Date: Wed, 14 Oct 2020 14:21:09 -0400 Subject: [PATCH] bug(passport-strategy-helper): Better presentation of auth errors --- .changeset/spotty-apples-visit.md | 5 +++++ .../passport/PassportStrategyHelper.test.ts | 9 +++++++-- .../lib/passport/PassportStrategyHelper.ts | 19 +++++++++++++++++-- 3 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 .changeset/spotty-apples-visit.md diff --git a/.changeset/spotty-apples-visit.md b/.changeset/spotty-apples-visit.md new file mode 100644 index 0000000000..ba481fa052 --- /dev/null +++ b/.changeset/spotty-apples-visit.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-auth-backend': patch +--- + +Better presentation of authentication errors diff --git a/plugins/auth-backend/src/lib/passport/PassportStrategyHelper.test.ts b/plugins/auth-backend/src/lib/passport/PassportStrategyHelper.test.ts index d8f6d25d0f..f7a22e9ad9 100644 --- a/plugins/auth-backend/src/lib/passport/PassportStrategyHelper.test.ts +++ b/plugins/auth-backend/src/lib/passport/PassportStrategyHelper.test.ts @@ -16,6 +16,7 @@ import express from 'express'; import passport from 'passport'; +import { InternalOAuthError } from 'passport-oauth2'; import { executeRedirectStrategy, executeFrameHandlerStrategy, @@ -58,7 +59,11 @@ describe('PassportStrategyHelper', () => { } class MyCustomAuthErrorStrategy extends passport.Strategy { authenticate() { - this.error(new Error('MyCustomAuth error')); + this.error( + new InternalOAuthError('MyCustomAuth error', { + data: '{ "message": "Custom message" }', + }), + ); } } class MyCustomAuthRedirectStrategy extends passport.Strategy { @@ -97,7 +102,7 @@ describe('PassportStrategyHelper', () => { ); expect(spyAuthenticate).toBeCalledTimes(1); await expect(frameHandlerStrategyPromise).rejects.toThrow( - 'Authentication failed, Error: MyCustomAuth error', + 'Authentication failed, MyCustomAuth error - Custom message', ); }); diff --git a/plugins/auth-backend/src/lib/passport/PassportStrategyHelper.ts b/plugins/auth-backend/src/lib/passport/PassportStrategyHelper.ts index 7bc34186f4..3264fd8faa 100644 --- a/plugins/auth-backend/src/lib/passport/PassportStrategyHelper.ts +++ b/plugins/auth-backend/src/lib/passport/PassportStrategyHelper.ts @@ -18,6 +18,7 @@ import express from 'express'; import passport from 'passport'; import jwtDecoder from 'jwt-decode'; import { ProfileInfo, RedirectInfo } from '../../providers/types'; +import { InternalOAuthError } from 'passport-oauth2'; export type PassportDoneCallback = ( err?: Error, @@ -95,8 +96,22 @@ export const executeFrameHandlerStrategy = async ( ) => { reject(new Error(`Authentication rejected, ${info.message ?? ''}`)); }; - strategy.error = (error: Error) => { - reject(new Error(`Authentication failed, ${error}`)); + strategy.error = (error: InternalOAuthError) => { + let message = `Authentication failed, ${error.message}`; + + if (error.oauthError?.data) { + try { + const errorData = JSON.parse(error.oauthError.data); + + if (errorData.message) { + message += ` - ${errorData.message}`; + } + } catch (parseError) { + message += ` - ${error.oauthError}`; + } + } + + reject(new Error(message)); }; strategy.redirect = () => { reject(new Error('Unexpected redirect'));