Skip to content

Commit

Permalink
Remove deprecated AbstractProvider.redirect method
Browse files Browse the repository at this point in the history
  • Loading branch information
LoicPoullain committed Aug 22, 2024
1 parent c61eca9 commit a6d98cd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 57 deletions.
3 changes: 2 additions & 1 deletion docs/blog/version-5.0-release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,5 @@ Version 5.0 of [Foal](https://foalts.org/) is out!
## Removal of deprecated components
- The deprecated hook `@Log` has been removed. Use the `Logger` service in a custom `@Hook` instead.
- The command alias `npx foal run-script` has been removed. Use `npx foal run` instead.
- The command alias `npx foal run-script` has been removed. Use `npx foal run` instead.
- The deprecated method `AbstractProvider.redirect` has been removed. Use `AbstractProvider.createHttpResponseWithConsentPageUrl({ isRedirection: true })` instead.
56 changes: 15 additions & 41 deletions packages/social/src/abstract-provider.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,32 +357,6 @@ describe('AbstractProvider', () => {

});

describe('has a "redirect" method that', () => {

it('should behave like the "createHttpResponseWithConsentPageUrl" method with the isRedirection option set to true.', async () => {
const actual = await provider.redirect({ scopes: ['foo'] });
const expected = await provider.createHttpResponseWithConsentPageUrl({ scopes: ['foo'], isRedirection: true });

if (!isHttpResponseRedirect(actual)) {
throw new Error('The response should be an HttpResponseRedirect object.');
}

if (!isHttpResponseRedirect(expected)) {
throw new Error('The response should be an HttpResponseRedirect object.');
}

const actualConsentPageUrl = new URL(actual.path);
const expectedConsentPageUrl = new URL(expected.path);

// Remove values generated randomly.
actualConsentPageUrl.searchParams.delete('state');
expectedConsentPageUrl.searchParams.delete('state');

strictEqual(actualConsentPageUrl.href, expectedConsentPageUrl.href);
});

});

describe('has a "getTokens" method that', () => {

let server: Server;
Expand Down Expand Up @@ -749,19 +723,19 @@ describe('Abstract Provider With PKCE', () => {
Config.remove('settings.social.cookie.domain');
});

describe('has a "redirect" method that', () => {
describe('has a "createHttpResponseWithConsentPageUrl" method that', () => {

it('should fail if secret is not configured', async () => {
try {
await provider.redirect();
await provider.createHttpResponseWithConsentPageUrl();
} catch(error) {
if(!(error instanceof ConfigNotFoundError)){
throw error;
}
}
});

describe('should return an HttpResponseRedirect object', () => {
describe('should return an HttpResponse object', () => {

beforeEach(() => {
Config.set('settings.social.secret.codeVerifierSecret', 'SECRET');
Expand All @@ -771,21 +745,21 @@ describe('Abstract Provider With PKCE', () => {
Config.remove('settings.social.secret.codeVerifierSecret');
});

it('with a redirect path which contains a client ID, a response type, a redirect URI, code_challenge and code_challenge_method (S256) if pkce enabled.', async () => {
const response = await provider.redirect();
ok(response.path.startsWith(
it('with a consentPageUrl which contains a client ID, a response type, a redirect URI, code_challenge and code_challenge_method (S256) if pkce enabled.', async () => {
const response = await provider.createHttpResponseWithConsentPageUrl();
ok(response.body.consentPageUrl.startsWith(
'https://example2.com/auth?'
+ 'response_type=code&'
+ 'client_id=clientIdXXX&'
+ 'redirect_uri=https%3A%2F%2Fexample.com%2Fcallback'
));
const searchParams = new URLSearchParams(response.path);
const searchParams = new URLSearchParams(response.body.consentPageUrl);
ok(searchParams.get('code_challenge'));
strictEqual(searchParams.get('code_challenge_method'), 'S256');
});

it('that sets a cookie containing the code verifier encrypted.', async () =>{
const response = await provider.redirect();
const response = await provider.createHttpResponseWithConsentPageUrl();

const stateCookieValue = response.getCookie(CODE_VERIFIER_COOKIE_NAME).value;
const stateCookieOptions = response.getCookie(CODE_VERIFIER_COOKIE_NAME).options;
Expand All @@ -804,7 +778,7 @@ describe('Abstract Provider With PKCE', () => {
it('that sets a cookie that can have a custom domain.', async () =>{
Config.set('settings.social.cookie.domain', 'foalts.org');

const response = await provider.redirect();
const response = await provider.createHttpResponseWithConsentPageUrl();
const { options } = response.getCookie(CODE_VERIFIER_COOKIE_NAME);

strictEqual(options.domain, 'foalts.org');
Expand Down Expand Up @@ -950,8 +924,8 @@ describe('Abstract Provider With PKCE and Plain Method', () => {
Config.remove('settings.social.cookie.domain');
});

describe('has a "redirect" method that', () => {
describe('should return an HttpResponseRedirect object', () => {
describe('has a "createHttpResponseWithConsentPageUrl" method that', () => {
describe('should return an HttpResponse object', () => {

beforeEach(() => {
Config.set('settings.social.secret.codeVerifierSecret', 'SECRET');
Expand All @@ -961,15 +935,15 @@ describe('Abstract Provider With PKCE and Plain Method', () => {
Config.remove('settings.social.secret.codeVerifierSecret');
});

it('with a redirect path which contains a client ID, a response type, a redirect URI, code_challenge and code_challenge_method (plain) if pkce enabled.', async () => {
const response = await provider.redirect();
ok(response.path.startsWith(
it('with a consentPageUrl which contains a client ID, a response type, a redirect URI, code_challenge and code_challenge_method (plain) if pkce enabled.', async () => {
const response = await provider.createHttpResponseWithConsentPageUrl();
ok(response.body.consentPageUrl.startsWith(
'https://example2.com/auth?'
+ 'response_type=code&'
+ 'client_id=clientIdXXX&'
+ 'redirect_uri=https%3A%2F%2Fexample.com%2Fcallback'
));
const searchParams = new URLSearchParams(response.path);
const searchParams = new URLSearchParams(response.body.consentPageUrl);
ok(searchParams.get('code_challenge'));
strictEqual(searchParams.get('code_challenge_method'), 'plain');
});
Expand Down
15 changes: 0 additions & 15 deletions packages/social/src/abstract-provider.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,21 +308,6 @@ export abstract class AbstractProvider<AuthParameters extends ObjectType, UserIn
.setCookie(STATE_COOKIE_NAME, state, cookieOptions)
}

/**
* Returns an HttpResponseRedirect object to redirect the user to the social provider's authorization page.
*
* This function is deprecated. Use createHttpResponseWithConsentPageUrl instead with isRedirection set to true.
*
* @param {{ scopes?: string[] }} [{ scopes }={}] - Custom scopes to override the default ones used by the provider.
* @param {AuthParameters} [params] - Additional parameters (specific to the social provider).
* @returns {Promise<HttpResponseRedirect>} The HttpResponseRedirect object.
* @memberof AbstractProvider
* @deprecated
*/
async redirect({ scopes }: { scopes?: string[] } = {}, params?: AuthParameters): Promise<HttpResponseRedirect> {
return this.createHttpResponseWithConsentPageUrl({ scopes, isRedirection: true }, params) as Promise<HttpResponseRedirect>;
}

/**
* Function to use in the controller method that handles the provider redirection.
*
Expand Down

0 comments on commit a6d98cd

Please sign in to comment.