-
Notifications
You must be signed in to change notification settings - Fork 581
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8814 from ever-co/fix/passport-strategy
[Fix] Passport Strategy Warning
- Loading branch information
Showing
42 changed files
with
538 additions
and
347 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,38 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
import { Strategy } from 'passport-auth0'; | ||
import { ConfigService, IEnvironment } from '@gauzy/config'; | ||
import { ConfigService } from '@gauzy/config'; | ||
|
||
@Injectable() | ||
export class Auth0Strategy extends PassportStrategy(Strategy, 'auth0') { | ||
constructor(readonly configService: ConfigService) { | ||
super(config(configService)); | ||
super(parseAuth0Config(configService)); | ||
} | ||
} | ||
|
||
/** | ||
* Generates the configuration object for Auth0 authentication. | ||
* | ||
* @param configService - The `ConfigService` instance used to access environment-specific configurations. | ||
* @returns {object} - The configuration object for Auth0, including client ID, client secret, domain, and callback URL. | ||
* @param {ConfigService} configService - The configuration service instance. | ||
* @returns {Record<string, string>} - The Auth0 configuration object. | ||
* @throws {Error} If required Auth0 configuration values are missing. | ||
*/ | ||
export const config = (configService: ConfigService) => { | ||
const AUTH0_CONFIG = configService.get('auth0Config') as IEnvironment['auth0Config']; | ||
const { baseUrl } = configService.apiConfigOptions; | ||
export const parseAuth0Config = (configService: ConfigService): Record<string, string> => { | ||
// Retrieve Auth0 configuration from the environment | ||
const auth0Config = configService.get('auth0Config'); | ||
// Retrieve API base URL | ||
const { baseUrl } = configService.getConfigValue('apiConfigOptions'); | ||
|
||
return { | ||
clientID: AUTH0_CONFIG.clientID || 'disabled', // Auth0 Client ID or default value | ||
clientSecret: AUTH0_CONFIG.clientSecret || 'disabled', // Auth0 Client Secret or default value | ||
domain: AUTH0_CONFIG.domain || 'disabled', // Auth0 Domain or default value | ||
callbackURL: `${baseUrl}/auth/auth0/callback` // Constructed callback URL | ||
}; | ||
// Validate required configurations | ||
if (!auth0Config.clientID || !auth0Config.clientSecret || !auth0Config.domain) { | ||
console.warn('⚠️ Auth0 configuration is missing some required values. Defaulting to "disabled".'); | ||
} | ||
|
||
// Construct and return the Auth0 configuration object | ||
return { | ||
clientID: auth0Config.clientID ?? 'disabled', | ||
clientSecret: auth0Config.clientSecret ?? 'disabled', | ||
domain: auth0Config.domain ?? 'disabled', | ||
callbackURL: `${baseUrl ?? 'http://localhost:3000'}/api/auth/auth0/callback` // Ensure a fallback URL | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,63 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
import { Strategy } from 'passport-google-oauth20'; | ||
import { ConfigService, IEnvironment } from '@gauzy/config'; | ||
import { ConfigService } from '@gauzy/config'; | ||
import passport from 'passport'; | ||
|
||
@Injectable() | ||
export class FiverrStrategy extends PassportStrategy(Strategy, 'fiverr') { | ||
constructor(readonly configService: ConfigService) { | ||
super(config(configService)); | ||
super(parseFiverrConfig(configService)); | ||
} | ||
|
||
async validate(profile, done: Function) { | ||
passport['_strategies'].session.role_name = ''; | ||
/** | ||
* Validates and extracts user information from Fiverr's OAuth profile. | ||
* | ||
* @param {any} profile - The user profile returned by Fiverr. | ||
* @param {Function} done - The callback function to complete authentication. | ||
*/ | ||
async validate(profile: any, done: (error: any, user?: any) => void) { | ||
try { | ||
try { | ||
const { emails } = profile; | ||
const user = { | ||
emails | ||
}; | ||
done(null, user); | ||
} catch (err) { | ||
done(err, false); | ||
console.log('Fiverr OAuth validate:', profile); | ||
|
||
// Ensure session strategy exists before modifying | ||
if (passport['_strategies'].session) { | ||
passport['_strategies'].session.role_name = ''; | ||
} | ||
} catch (err) { | ||
done(err, false); | ||
|
||
const { emails } = profile || {}; | ||
const user = { emails }; | ||
|
||
done(null, user); | ||
} catch (error) { | ||
console.error('Fiverr OAuth validation error:', error); | ||
done(error, false); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Retrieves the configuration for the Fiverr OAuth strategy. | ||
* | ||
* @param configService | ||
* @returns | ||
* @param {ConfigService} configService - The configuration service instance. | ||
* @returns {Record<string, string | boolean>} - The configuration object for Fiverr authentication. | ||
* @throws {Error} If required Fiverr configuration values are missing. | ||
*/ | ||
export const config = (configService: ConfigService) => { | ||
const FIVERR_CONFIG = configService.get('fiverrConfig') as IEnvironment['fiverrConfig']; | ||
export const parseFiverrConfig = (configService: ConfigService): Record<string, string | boolean> => { | ||
// Retrieve Fiverr configuration from the environment | ||
const fiverrConfig = configService.get('fiverrConfig'); | ||
// Retrieve API base URL | ||
const { baseUrl } = configService.apiConfigOptions; | ||
|
||
// Validate required Fiverr configurations | ||
if (!fiverrConfig?.clientId || !fiverrConfig?.clientSecret) { | ||
console.warn('⚠️ Fiverr authentication configuration is incomplete. Defaulting to "disabled".'); | ||
} | ||
|
||
return { | ||
clientID: FIVERR_CONFIG.clientId || 'disabled', | ||
clientSecret: FIVERR_CONFIG.clientSecret || 'disabled', | ||
callbackURL: `${baseUrl}/api/auth/fiverr/callback`, | ||
clientID: fiverrConfig?.clientId ?? 'disabled', | ||
clientSecret: fiverrConfig?.clientSecret ?? 'disabled', | ||
callbackURL: `${baseUrl ?? 'http://localhost:3000'}/api/auth/fiverr/callback`, // Ensure a fallback URL | ||
passReqToCallback: true | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.