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

Open oauth callback endpoints to be public #3168

Merged
merged 1 commit into from
Apr 25, 2022
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
10 changes: 10 additions & 0 deletions packages/cli/src/CredentialsHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -753,3 +753,13 @@ export async function getCredentialForUser(

return sharedCredential.credentials as ICredentialsDb;
}

/**
* Get a credential without user check
*/
export async function getCredentialWithoutUser(
credentialId: string,
): Promise<ICredentialsDb | undefined> {
const credential = await Db.collections.Credentials.findOne(credentialId);
return credential;
}
23 changes: 12 additions & 11 deletions packages/cli/src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ import {
WorkflowHelpers,
WorkflowRunner,
getCredentialForUser,
getCredentialWithoutUser,
} from '.';

import config from '../config';
Expand Down Expand Up @@ -1822,18 +1823,18 @@ class App {
LoggerProxy.error(
'OAuth1 callback failed because of insufficient parameters received',
{
userId: req.user.id,
userId: req.user?.id,
credentialId,
},
);
return ResponseHelper.sendErrorResponse(res, errorResponse);
}

const credential = await getCredentialForUser(credentialId, req.user);
const credential = await getCredentialWithoutUser(credentialId);

if (!credential) {
LoggerProxy.error('OAuth1 callback failed because of insufficient user permissions', {
userId: req.user.id,
userId: req.user?.id,
credentialId,
});
const errorResponse = new ResponseHelper.ResponseError(
Expand Down Expand Up @@ -1883,7 +1884,7 @@ class App {
oauthToken = await requestPromise(options);
} catch (error) {
LoggerProxy.error('Unable to fetch tokens for OAuth1 callback', {
userId: req.user.id,
userId: req.user?.id,
credentialId,
});
const errorResponse = new ResponseHelper.ResponseError(
Expand Down Expand Up @@ -1913,13 +1914,13 @@ class App {
await Db.collections.Credentials!.update(credentialId, newCredentialsData);

LoggerProxy.verbose('OAuth1 callback successful for new credential', {
userId: req.user.id,
userId: req.user?.id,
credentialId,
});
res.sendFile(pathResolve(__dirname, '../../templates/oauth-callback.html'));
} catch (error) {
LoggerProxy.error('OAuth1 callback failed because of insufficient user permissions', {
userId: req.user.id,
userId: req.user?.id,
credentialId: req.query.cid,
});
// Error response
Expand Down Expand Up @@ -2084,11 +2085,11 @@ class App {
return ResponseHelper.sendErrorResponse(res, errorResponse);
}

const credential = await getCredentialForUser(state.cid, req.user);
const credential = await getCredentialWithoutUser(state.cid);

if (!credential) {
LoggerProxy.error('OAuth2 callback failed because of insufficient permissions', {
userId: req.user.id,
userId: req.user?.id,
credentialId: state.cid,
});
const errorResponse = new ResponseHelper.ResponseError(
Expand Down Expand Up @@ -2129,7 +2130,7 @@ class App {
!token.verify(decryptedDataOriginal.csrfSecret as string, state.token)
) {
LoggerProxy.debug('OAuth2 callback state is invalid', {
userId: req.user.id,
userId: req.user?.id,
credentialId: state.cid,
});
const errorResponse = new ResponseHelper.ResponseError(
Expand Down Expand Up @@ -2180,7 +2181,7 @@ class App {

if (oauthToken === undefined) {
LoggerProxy.error('OAuth2 callback failed: unable to get access tokens', {
userId: req.user.id,
userId: req.user?.id,
credentialId: state.cid,
});
const errorResponse = new ResponseHelper.ResponseError(
Expand Down Expand Up @@ -2214,7 +2215,7 @@ class App {
// Save the credentials in DB
await Db.collections.Credentials!.update(state.cid, newCredentialsData);
LoggerProxy.verbose('OAuth2 callback successful for new credential', {
userId: req.user.id,
userId: req.user?.id,
credentialId: state.cid,
});

Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/UserManagement/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export function addRoutes(this: N8nApp, ignoredEndpoints: string[], restEndpoint
req.url.startsWith(`/${restEndpoint}/forgot-password`) ||
req.url.startsWith(`/${restEndpoint}/resolve-password-token`) ||
req.url.startsWith(`/${restEndpoint}/change-password`) ||
req.url.startsWith(`/${restEndpoint}/oauth2-credential/callback`) ||
req.url.startsWith(`/${restEndpoint}/oauth1-credential/callback`) ||
Comment on lines +69 to +70

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes total sense to add these as a default non authenticated routes.

isAuthExcluded(req.url, ignoredEndpoints)
) {
return next();
Expand Down
8 changes: 6 additions & 2 deletions packages/cli/src/requests.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,16 @@ export declare namespace OAuthRequest {
{},
{},
{ oauth_verifier: string; oauth_token: string; cid: string }
>;
> & {
user?: User;
};
}

namespace OAuth2Credential {
type Auth = OAuth1Credential.Auth;
type Callback = AuthenticatedRequest<{}, {}, {}, { code: string; state: string }>;
type Callback = AuthenticatedRequest<{}, {}, {}, { code: string; state: string }> & {
user?: User;
};
Comment on lines -240 to +249

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is exactly what I was searching for, but couldn't find it

}
}

Expand Down