Compare commits

...

2 Commits

Author SHA1 Message Date
Saumya Pandey 988f9d5ec1 Check for access token validity in auth.info 2021-07-24 18:44:41 +05:30
Saumya Pandey 50029772fe Enable google offline access 2021-07-24 18:44:22 +05:30
4 changed files with 81 additions and 5 deletions
+44 -1
View File
@@ -3,9 +3,11 @@ import Router from "koa-router";
import { find } from "lodash";
import { parseDomain, isCustomSubdomain } from "../../shared/utils/domains";
import providers from "../auth/providers";
import { AuthenticationError } from "../errors";
import auth from "../middlewares/authentication";
import { Team } from "../models";
import { AuthenticationProvider, Team, UserAuthentication } from "../models";
import { presentUser, presentTeam, presentPolicies } from "../presenters";
import * as Slack from "../slack";
import { isCustomDomain } from "../utils/domains";
const router = new Router();
@@ -105,6 +107,47 @@ router.post("auth.info", auth(), async (ctx) => {
const user = ctx.state.user;
const team = await Team.findByPk(user.teamId);
const authenticationProvider = await AuthenticationProvider.findOne({
where: {
teamId: team.id,
},
});
const userAuthentication = await UserAuthentication.findOne({
where: {
userId: user.id,
authenticationProviderId: authenticationProvider.id,
},
});
if (!userAuthentication.accessToken || !userAuthentication.refreshToken) {
throw new AuthenticationError("Tokens are not provided");
}
if (authenticationProvider.name === "slack") {
const data = await Slack.post("auth.test", {
token: userAuthentication.accessToken,
});
if (!data.ok) {
const data = await Slack.postUrlEncodedForm("oauth.v2.access", {
client_id: process.env.SLACK_KEY,
client_secret: process.env.SLACK_SECRET,
grant_type: "refresh_token",
refresh_token: userAuthentication.refreshToken,
});
if (!data.ok) {
throw new AuthenticationError(data.error);
}
userAuthentication.accessToken = data.access_token;
userAuthentication.refreshToken = data.refresh_token;
await userAuthentication.save();
}
} else if (authenticationProvider.name === "google") {
}
ctx.body = {
data: {
user: presentUser(user, { includeDetails: true }),
+5 -1
View File
@@ -56,12 +56,16 @@ router.post("hooks.unfurl", async (ctx) => {
};
}
await Slack.post("chat.unfurl", {
const data = await Slack.post("chat.unfurl", {
token: auth.token,
channel: event.channel,
ts: event.message_ts,
unfurls,
});
if (!data.ok) {
throw new InvalidRequestError(data.error);
}
});
// triggered by interactions with actions, dialogs, message buttons in Slack
+7 -1
View File
@@ -87,7 +87,13 @@ if (GOOGLE_CLIENT_ID) {
)
);
router.get("google", passport.authenticate(providerName));
router.get(
"google",
passport.authenticate(providerName, {
accessType: "offline",
prompt: "consent",
})
);
router.get("google.callback", passportMiddleware(providerName));
}
+25 -2
View File
@@ -14,7 +14,7 @@ export async function post(endpoint: string, body: Object) {
method: "POST",
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
"Content-Type": "application/json; charset=utf-8",
},
body: JSON.stringify(body),
});
@@ -22,7 +22,30 @@ export async function post(endpoint: string, body: Object) {
} catch (err) {
throw new InvalidRequestError(err.message);
}
if (!data.ok) throw new InvalidRequestError(data.error);
return data;
}
export async function postUrlEncodedForm(endpoint: string, body: Object) {
let data;
const newBody = Object.keys(body)
.map((key) => {
return encodeURIComponent(key) + "=" + encodeURIComponent(body[key]);
})
.join("&");
try {
const response = await fetch(`${SLACK_API_URL}/${endpoint}`, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
body: newBody,
});
data = await response.json();
} catch (err) {
throw new InvalidRequestError(err.message);
}
return data;
}