Skip to content

Commit

Permalink
feat(authentication-oauth): Set oAuth redirect URL dynamically (#1608)
Browse files Browse the repository at this point in the history
  • Loading branch information
fadiquader authored and daffl committed Nov 27, 2019
1 parent 5e65629 commit 1293e08
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
22 changes: 11 additions & 11 deletions packages/authentication-oauth/src/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default (options: OauthSetupSettings) => {
if (feathers_token) {
debug(`Got feathers_token query parameter to link accounts`, feathers_token);
req.session.accessToken = feathers_token;
req.session.query = query;
}

res.redirect(`${path}/connect/${name}?${qs.stringify(query)}`);
Expand All @@ -55,12 +56,20 @@ export default (options: OauthSetupSettings) => {

authApp.get('/:name/authenticate', async (req, res, next) => {
const { name } = req.params as any;
const { accessToken, grant } = req.session;
const { accessToken, grant, query = {} } = req.session;
const service = app.defaultAuthentication(authService);
const [ strategy ] = service.getStrategies(name) as OAuthStrategy[];
const params = {
authStrategies: [ name ],
authentication: accessToken ? {
strategy: linkStrategy,
accessToken
} : null,
query
};
const sendResponse = async (data: AuthenticationResult|Error) => {
try {
const redirect = await strategy.getRedirect(data);
const redirect = await strategy.getRedirect(data, params);

if (redirect !== null) {
res.redirect(redirect);
Expand All @@ -78,15 +87,6 @@ export default (options: OauthSetupSettings) => {
try {
const payload = config.defaults.transport === 'session' ?
grant.response : req.query;

const params = {
authStrategies: [ name ],
authentication: accessToken ? {
strategy: linkStrategy,
accessToken
} : null
};

const authentication = {
strategy: name,
...payload
Expand Down
6 changes: 4 additions & 2 deletions packages/authentication-oauth/src/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,15 @@ export class OAuthStrategy extends AuthenticationBaseStrategy {
return null;
}

async getRedirect (data: AuthenticationResult|Error) {
async getRedirect (data: AuthenticationResult|Error, params?: Params) {
const queryRedirect = (params && params.query && params.query.redirect) || '';
const { redirect } = this.authentication.configuration.oauth;

if (!redirect) {
return null;
}

const redirectUrl = redirect + queryRedirect;
const separator = redirect.endsWith('?') ? '' :
(redirect.indexOf('#') !== -1 ? '?' : '#');
const authResult: AuthenticationResult = data;
Expand All @@ -88,7 +90,7 @@ export class OAuthStrategy extends AuthenticationBaseStrategy {
error: data.message || 'OAuth Authentication not successful'
};

return redirect + separator + querystring.stringify(query);
return redirectUrl + separator + querystring.stringify(query);
}

async findEntity (profile: OAuthProfile, params: Params) {
Expand Down
5 changes: 5 additions & 0 deletions packages/authentication-oauth/test/strategy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ describe('@feathersjs/authentication-oauth/strategy', () => {
let redirect = await strategy.getRedirect({ accessToken: 'testing' });
assert.equal(redirect, '/home#access_token=testing');

redirect = await strategy.getRedirect({ accessToken: 'testing' }, {
query: { redirect: '/hi-there' }
});
assert.strictEqual('/home/hi-there#access_token=testing', redirect);

redirect = await strategy.getRedirect(new Error('something went wrong'));
assert.equal(redirect, '/home#error=something%20went%20wrong');

Expand Down

0 comments on commit 1293e08

Please # to comment.