Skip to content

Commit

Permalink
Added auth dialog but forbidden by google auth iframe policy
Browse files Browse the repository at this point in the history
  • Loading branch information
DaKingKong committed Apr 28, 2022
1 parent 18b44ca commit 28068ff
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 9 deletions.
9 changes: 7 additions & 2 deletions src/server/adaptiveCardPayloads/authCard.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@
{
"type": "Image",
"selectAction": {
"type": "Action.OpenUrl",
"url": "${link}"
"type": "Action.Submit",
"data": {
"actionType": "auth",
"returnDialog": "${returnDialog}",
"botId": "${botId}",
"authLink": "${authLink}"
}
},
"url": "https://raw.githubusercontent.com/ringcentral/ringcentral-google-drive-notification-add-in/master/staticResources/google-login-button.png"
},
Expand Down
2 changes: 1 addition & 1 deletion src/server/handlers/authorizationHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ async function oauthCallback(req, res) {
res.send('Internal error.');
}
res.status(200);
res.send('<!doctype><html><body>Successfully authorized. You can close this page now.<script>window.close()</script></body></html>')
res.send('<!doctype><html><body>Successfully authorized. Please close this page.</body></html>')
};

async function checkUserFileAccess(googleUser, fileId) {
Expand Down
4 changes: 2 additions & 2 deletions src/server/handlers/botHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const botHandler = async event => {
const authLink = `${oauthApp.code.getUri({
state: `botId=${botForMessage.id}&rcUserId=${userId}`
})}&access_type=offline`;
const authCard = cardBuilder.authCard(authLink);
const authCard = cardBuilder.authCard(authLink, botForMessage.id);
await botForMessage.sendAdaptiveCard(createGroupResponse.id, authCard);
}
break;
Expand Down Expand Up @@ -228,7 +228,7 @@ async function checkMembersGoogleAccountAuth(bot, groupId) {
const authLink = `${oauthApp.code.getUri({
state: `botId=${bot.id}&rcUserId=${userId}`
})}&access_type=offline`;
const authCard = cardBuilder.authCard(authLink, 'Someone shared a file that requires logging in Google Account to access. Please log in with button below.');
const authCard = cardBuilder.authCard(authLink, bot.id, 'Someone shared a file that requires logging in Google Account to access. Please log in with button below.');
if (rcUser) {
if (moment(nowDate).unix() > moment(rcUser.authReminderExpiryDateTime).unix()) {
await bot.sendAdaptiveCard(dmGroupId, authCard);
Expand Down
24 changes: 23 additions & 1 deletion src/server/handlers/interactiveMessageHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const { GoogleUser } = require('../models/googleUserModel');
const Bot = require('ringcentral-chatbot-core/dist/models/Bot').default;
const { revokeToken, checkAndRefreshAccessToken } = require('../lib/oauth');
const cardBuilder = require('../lib/cardBuilder');
const dialogBuilder = require('../lib/dialogBuilder');
const rcAPI = require('../lib/rcAPI');

const subscriptionHandler = require('./subscriptionHandler');
Expand Down Expand Up @@ -51,13 +52,34 @@ async function interactiveMessages(req, res) {
// Create/Find DM conversation to the RC user
const createGroupResponse = await rcAPI.createConversation([rcUserId], bot.token.access_token);

if (!googleUser) {
if (!googleUser && body.data.actionType !== 'auth') {
await bot.sendMessage(groupId, { text: `![:Person](${rcUserId}) Google Drive account not found. Please message me with \`login\` to login.` });
res.status(200);
res.json('OK')
return;
}

if (body.data.returnDialog) {
let dialogResponse = {
type: "dialog",
dialog: null
};
switch (body.data.actionType) {
case 'auth':
const authDialogData = {
title: 'Google Drive Account Login',
iframeURL: body.data.authLink
}
const authDialog = dialogBuilder.getIframeDialog(authDialogData);
dialogResponse.dialog = authDialog;
break;
}

res.status(200);
res.send(dialogResponse);
return;
}

switch (body.data.actionType) {
case 'unAuthCard':
const unAuthCard = cardBuilder.unAuthCard(googleUser.email, rcUserId, bot.id);
Expand Down
8 changes: 5 additions & 3 deletions src/server/lib/cardBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,14 @@ function fileInfoCard(botId, googleFile) {
return card;
}

function authCard(authLink, additionalInfoText) {
function authCard(authLink, botId, additionalInfoText) {
const template = new Template(authCardTemplateJson);
const cardData = {
link: authLink,
authLink,
additionalInfoText,
showAdditionalInfo: additionalInfoText != null
showAdditionalInfo: additionalInfoText != null,
returnDialog: true,
botId
}
const card = template.expand({
$root: cardData
Expand Down
11 changes: 11 additions & 0 deletions src/server/lib/dialogBuilder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function getIframeDialog({ title, size, iconURL, iframeURL }) {
const dialog = {
...(size !== null && { size }),
...(iconURL !== null && { iconURL }),
...title, iframeURL
};

return dialog;
}

exports.getIframeDialog = getIframeDialog;

0 comments on commit 28068ff

Please sign in to comment.