Skip to content

Commit

Permalink
fix: Connector config opt replaceCallbackURIDomain (#79)
Browse files Browse the repository at this point in the history
* fix: Connector config opt replaceCallbackURIDomain

* chore: Debug

* chore: Debug

* Update index.ts

---------

Co-authored-by: Ankur Banerjee <[email protected]>
  • Loading branch information
jay-dee7 and ankurdotb authored Jun 30, 2023
1 parent 8e49758 commit 230665b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ export const defaultMetadata: ConnectorMetadata = {
},
{
key: 'replaceCallbackURIDomain',
type: ConnectorConfigFormItemType.Text,
type: ConnectorConfigFormItemType.Switch,
required: false,
defaultValue: false,
label: 'Replace Callback URI with Bot Origin?',
placeholder: 'false',
},
Expand Down
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const getAuthorizationUri =
// Reconstruct the return_to url
// eslint-disable-next-line
let returnTo = new URL(redirectUri);

if (config.replaceCallbackURIDomain) {
// eslint-disable-next-line
returnTo = new URL(returnTo.pathname, config.origin);
Expand All @@ -57,7 +57,9 @@ const getAuthorizationUri =
};

const authorizationCallbackHandler = async (parameterObject: unknown) => {
console.log('authorizationCallbackHandler-parameterObject:', parameterObject);
const result = authResponseGuard.safeParse(parameterObject);
console.log('authorizationCallbackHandler-result:', result);

if (!result.success) {
throw new ConnectorError(ConnectorErrorCodes.General, JSON.stringify(parameterObject));
Expand All @@ -71,6 +73,7 @@ const performTelegramIntegrityCheck = (
botToken: string
): boolean => {
const fields: string[] = [];
console.log('telegramResponse:', telegramResponse);
for (const key of Object.keys(telegramResponse)) {
if (key === 'hash') {
continue;
Expand All @@ -80,17 +83,21 @@ const performTelegramIntegrityCheck = (
fields.push(field);
}
const data = fields.sort().join('\n');
console.log('telegramResponse-data:', data);

const botSecretKey = createHash('sha256').update(Buffer.from(botToken)).digest();
const dataHash = createHmac('sha256', botSecretKey).update(data).digest('hex');
console.log('telegramResponse-dataHash:', dataHash);
return dataHash === telegramResponse.hash;
};

const getUserInfo =
(getConfig: GetConnectorConfig): GetUserInfo =>
async (data) => {
console.log('getUserInfo:', data);
// Get tgAuthResult from parameterObject
const { tgAuthResult } = await authorizationCallbackHandler(data);
console.log('tgAuthResult:', tgAuthResult);

// Get config
const config = await getConfig(defaultMetadata.id);
Expand Down

0 comments on commit 230665b

Please sign in to comment.