Skip to content

Commit

Permalink
[gh-NangoHQ#610] bring in updates
Browse files Browse the repository at this point in the history
  • Loading branch information
khaliqgant committed May 4, 2023
2 parents 66976cb + dbc41b6 commit a9f70f9
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 63 deletions.
99 changes: 57 additions & 42 deletions packages/server/lib/controllers/oauth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ class OAuthController {
authMode: '',
url: callbackUrl,
connectionId,
wsClientId
params: {
...connectionConfig,
hmacEnabled: hmacService.isEnabled() === true
}
});

const config = await configService.getProviderConfig(providerConfigKey, accountId);
Expand All @@ -117,8 +120,7 @@ class OAuthController {
timestamp: Date.now(),
authMode: '',
url: callbackUrl,
connectionId,
wsClientId
connectionId
});
fileLogger.error('', log);
return wsClient.notifyErr(res, wsClientId, providerConfigKey, connectionId, WSErrBuilder.UnknownProviderConfigKey(providerConfigKey));
Expand All @@ -133,8 +135,7 @@ class OAuthController {
timestamp: Date.now(),
authMode: '',
url: callbackUrl,
connectionId,
wsClientId
connectionId
});
fileLogger.error('', log);
return wsClient.notifyErr(res, wsClientId, providerConfigKey, connectionId, WSErrBuilder.UnkownProviderTemplate(config.provider));
Expand All @@ -161,8 +162,7 @@ class OAuthController {
timestamp: Date.now(),
authMode: template.auth_mode,
url: callbackUrl,
connectionId,
wsClientId
connectionId
});
fileLogger.error('', log);
return wsClient.notifyErr(res, wsClientId, providerConfigKey, connectionId, WSErrBuilder.InvalidProviderConfig(providerConfigKey));
Expand All @@ -178,7 +178,11 @@ class OAuthController {
authMode: template.auth_mode,
url: callbackUrl,
connectionId,
wsClientId
params: {
...template.authorization_params,
scopes: config.oauth_scopes,
default_scopes: template.default_scopes?.join(',') as string
}
});

if (template.auth_mode === ProviderAuthModes.OAuth2) {
Expand All @@ -192,8 +196,7 @@ class OAuthController {
timestamp: Date.now(),
authMode: '',
url: callbackUrl,
connectionId,
wsClientId
connectionId
});
fileLogger.error('', log);

Expand All @@ -204,8 +207,7 @@ class OAuthController {
timestamp: Date.now(),
authMode: '',
url: '',
connectionId,
wsClientId
connectionId
});
fileLogger.error('', log);
errorManager.report(e, { accountId: accountId });
Expand Down Expand Up @@ -298,26 +300,8 @@ class OAuthController {

await oAuthSessionService.create(session);

log.messages.push({
content: `Initiating token request for ${providerConfig.provider} using ${providerConfigKey} for the connection ${connectionId}`,
timestamp: Date.now(),
providerConfigKey,
url: callbackUrl,
connectionId,
wsClientId: Number(wsClientId)
});

const simpleOAuthClient = new simpleOauth2.AuthorizationCode(getSimpleOAuth2ClientConfig(providerConfig, template, connectionConfig));

log.messages.push({
content: `Token response was received for ${providerConfig.provider} using ${providerConfigKey} for the connection ${connectionId}`,
timestamp: Date.now(),
providerConfigKey,
url: callbackUrl,
connectionId,
wsClientId: Number(wsClientId)
});

const authorizationUri = simpleOAuthClient.authorizeURL({
redirect_uri: callbackUrl,
scope: providerConfig.oauth_scopes.split(',').join(oauth2Template.scope_separator || ' '),
Expand All @@ -333,7 +317,12 @@ class OAuthController {
providerConfigKey,
url: callbackUrl,
connectionId,
wsClientId: Number(wsClientId)
params: {
...additionalAuthParams,
scope: providerConfig.oauth_scopes.split(',').join(oauth2Template.scope_separator || ' '),
external_api_url: authorizationUri,
basic_auth_enabled: template.token_request_auth_method === 'basic'
}
});

log.end = Date.now();
Expand Down Expand Up @@ -394,8 +383,7 @@ class OAuthController {
authMode: template.auth_mode,
providerConfigKey,
url: oAuth1CallbackURL,
connectionId,
wsClientId: Number(wsClientId)
connectionId
});

const oAuth1Client = new OAuth1Client(config, template, oAuth1CallbackURL);
Expand Down Expand Up @@ -433,8 +421,7 @@ class OAuthController {
authMode: template.auth_mode,
providerConfigKey,
url: oAuth1CallbackURL,
connectionId,
wsClientId: Number(wsClientId)
connectionId
});

fileLogger.info('', log);
Expand Down Expand Up @@ -520,8 +507,7 @@ class OAuthController {
timestamp: Date.now(),
authMode: session.authMode,
url: req.originalUrl,
connectionId,
wsClientId
connectionId
});
log.sessionId = session.id;

Expand Down Expand Up @@ -606,6 +592,21 @@ class OAuthController {

try {
let rawCredentials: object;

log.messages.push({
content: `Initiating token request for ${session.provider} using ${providerConfigKey} for the connection ${connectionId}`,
timestamp: Date.now(),
providerConfigKey,
connectionId,
params: {
...additionalTokenParams,
code: code as string,
scope: config.oauth_scopes,
basic_auth_enabled: template.token_request_auth_method === 'basic',
token_params: template?.token_params as string
}
});

if (providerClientManager.shouldUseProviderClient(session.provider)) {
rawCredentials = await providerClientManager.getToken(config, template.token_url, code as string, session.callbackUrl);
} else {
Expand All @@ -622,6 +623,20 @@ class OAuthController {
rawCredentials = accessToken.token;
}

log.messages.push({
content: `Token response was received for ${session.provider} using ${providerConfigKey} for the connection ${connectionId}`,
timestamp: Date.now(),
providerConfigKey,
connectionId,
params: {
...additionalTokenParams,
code: code as string,
scope: config.oauth_scopes,
basic_auth_enabled: template.token_request_auth_method === 'basic',
token_params: template?.token_params as string
}
});

logger.debug(`OAuth 2 for ${providerConfigKey} (connection ${connectionId}) successful.`);

const tokenMetadata = getConnectionMetadataFromTokenResponse(rawCredentials, template);
Expand All @@ -645,13 +660,14 @@ class OAuthController {

log.messages.push({
content: `OAuth connection for ${providerConfigKey} was successful`,
code: code as string,
params: additionalTokenParams,
timestamp: Date.now(),
authMode: template.auth_mode,
url: session.callbackUrl,
connectionId,
wsClientId
params: {
...additionalTokenParams,
code: code as string
}
});

fileLogger.info('', log);
Expand Down Expand Up @@ -710,8 +726,7 @@ class OAuthController {
timestamp: Date.now(),
authMode: template.auth_mode,
url: session.callbackUrl,
connectionId,
wsClientId
connectionId
});

fileLogger.info('', log);
Expand Down
27 changes: 14 additions & 13 deletions packages/server/lib/services/connection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,19 +196,20 @@ class ConnectionService {
return !(value.providerConfigKey === providerConfigKey && value.connectionId === connectionId);
});

log.action = 'token';
log.level = 'error';
log.end = Date.now();
log.success = false;
log.timestamp = Date.now();
log.messages = [
{
content: `Refresh oauth2 token call failed`,
timestamp: Date.now()
}
];
fileLogger.error('', log);

if (log) {
log.action = 'token';
log.level = 'error';
log.end = Date.now();
log.success = false;
log.timestamp = Date.now();
log.messages = [
{
content: `Refresh oauth2 token call failed`,
timestamp: Date.now()
}
];
fileLogger.error('', log);
}
reject(e);
}
});
Expand Down
2 changes: 1 addition & 1 deletion packages/server/lib/utils/file-logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { HTTP_VERB } from '../models.js';
export type LogLevel = 'info' | 'debug' | 'error';
export type LogAction = 'oauth' | 'proxy' | 'token';
interface Message {
[index: string]: undefined | string | number | Record<string, string>;
[index: string]: undefined | string | number | Record<string, string | boolean>;
}

export interface LogData {
Expand Down
14 changes: 7 additions & 7 deletions packages/webapp/src/pages/Activity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default function Activity() {
return Object.entries(params).map(([key, value]) => (
<div key={key}>
<span>{key}: </span>
<span>{value}</span>
<span>{value.toString()}</span>
</div>
));
};
Expand Down Expand Up @@ -79,7 +79,7 @@ export default function Activity() {
<AlertCircle className="stroke-red-500" size="32" />
)
}
<div className="ml-24 w-36 mr-48">
<div className="ml-10 w-36 mr-48">
{activity.action === 'oauth' && (
<div className="inline-flex justify-center items-center rounded-full py-1 px-4 bg-pink-500 bg-opacity-20">
<LinkIcon className="stroke-pink-500 mr-2" size="16" />
Expand Down Expand Up @@ -109,22 +109,22 @@ export default function Activity() {
)}
</div>
<Tooltip text={activity.connectionId} type="dark">
<div className="ml-30 w-32 mr-12 text-[#5AC2B3] font-mono overflow-hidden truncate">`{activity.connectionId}`</div>
<div className="ml-30 w-48 mr-12 text-[#5AC2B3] font-mono overflow-hidden truncate">`{activity.connectionId}`</div>
</Tooltip>
<div className="w-36 mr-12">
{activity?.provider ? (
<div className="w-80 flex">
<img src={`images/template-logos/${activity.provider}.svg`} alt="" className="h-7 mt-0.5 mr-0.5" />
<p className="mt-1.5 ml-2">{activity.provider}</p>
<img src={`images/template-logos/${activity.provider}.svg`} alt="" className="h-7 mt-0.5" />
<p className="mt-1.5 ml-2">{activity.providerConfigKey}</p>
</div>
) : (
<div className="mr-12">{activity.providerConfigKey}</div>
)}
</div>
<p className="text-gray-500 w-48 mr-4">{formatTimestamp(Number(activity.timestamp))}</p>
<p className="text-gray-500 w-40">{formatTimestamp(Number(activity.timestamp))}</p>
{activity.messages && (
<button
className="flex h-8 ml-4 mr-2 rounded-md pl-2 pr-3 pt-1.5 text-sm text-white bg-gray-800 hover:bg-gray-700"
className="flex h-8 mr-2 rounded-md pl-2 pr-3 pt-1.5 text-sm text-white bg-gray-800 hover:bg-gray-700"
onClick={() => setExpandedRow(index === expandedRow ? -1 : index)}
>
<p>{index === expandedRow ? 'Hide Logs' : 'Show Logs'}</p>
Expand Down

0 comments on commit a9f70f9

Please sign in to comment.