Skip to content

Commit

Permalink
Merge pull request #221 from friggframework/feature/lef-789-save-user…
Browse files Browse the repository at this point in the history
…-token-if-it-exists-instead-of-bot-token

Add the team entity to Slack if it doesn't exists and return it in pr…
  • Loading branch information
leofmds authored Sep 21, 2023
2 parents 247b585 + 3ee830b commit 03cdb23
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 12 deletions.
22 changes: 22 additions & 0 deletions api-module-library/slack/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const qs = require('qs');
const { OAuth2Requester } = require('@friggframework/module-plugin');
const { get } = require('@friggframework/assertions');
const { FetchError } = require('@friggframework/errors');
const moment = require("moment/moment");

class Api extends OAuth2Requester {
constructor(params) {
Expand Down Expand Up @@ -79,6 +80,27 @@ class Api extends OAuth2Requester {
this.tokenUri = this.baseUrl + this.URLs.access_token;
}

async setTokens(params) {
this.access_token = get(params, 'access_token');
this.refresh_token = get(params, 'refresh_token', null);
const authedUser = get(params, 'authed_user', null);
const accessExpiresIn = get(params, 'expires_in', null);
const refreshExpiresIn = get(
params,
'x_refresh_token_expires_in',
null
);

if (authedUser && authedUser.access_token) {
this.access_token = authedUser.access_token;
}

this.accessTokenExpire = moment().add(accessExpiresIn, 'seconds');
this.refreshTokenExpire = moment().add(refreshExpiresIn, 'seconds');

await this.notify(this.DLGT_TOKEN_UPDATE);
}

async _request(url, options, i = 0) {
let encodedUrl = encodeURI(url);
if (options.query) {
Expand Down
70 changes: 58 additions & 12 deletions api-module-library/slack/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const { Credential } = require('./models/credential');
const { ConfigFields } = require('./authFields');
const Config = require('./defaultConfig.json');
const { IntegrationMapping } = require('./models/integrationMapping');
const moment = require("moment/moment");

class Manager extends ModuleManager {
static Entity = Entity;
Expand Down Expand Up @@ -64,37 +65,81 @@ class Manager extends ModuleManager {
const code = get(params.data, 'code', null);

// For OAuth2, generate the token and store in this.credential and the DB

let authInfo;
try {
await this.api.getTokenFromCode(code);
authInfo = await this.api.getTokenFromCode(code);
} catch (e) {
flushDebugLog(e);
throw new Error('Auth Error');
}
const authRes = await this.testAuth();
if (!authRes) throw new Error('Auth Error');

// get entity identifying information from the api. You'll need to format this.

const workspaceInfo = await this.api.authTest();
const isUserScopeAuthorized = authInfo.authed_user && authInfo.authed_user.access_token;
const teamId = authInfo.team.id;
// get entity identifying information from the api. If we have the user access token,
// it means we should store it along with their id
const externalId = isUserScopeAuthorized ?
authInfo.authed_user.id : teamId;

await this.findOrCreateEntity({
externalId: workspaceInfo.team_id,
name: workspaceInfo.team,
await this.findOrCreateUserEntity({
externalId: externalId,
name: authInfo.team.name,
});
return {

const returnObj = {
type: Manager.getName(),
credential_id: this.credential.id,
entity_id: this.entity.id,
type: Manager.getName(),
team_entity_id: null,
auth_info: authInfo,
};

if (isUserScopeAuthorized) {
const teamEntity = await this.createAndReturnTeamEntity({
authInfo,
teamId,
});

returnObj.team_entity_id = teamEntity.id;
}

return returnObj;
}

async createAndReturnTeamEntity({
authInfo,
teamId,
}) {
let teamEntity = await Entity.findOne({
externalId: teamId
});
if (!teamEntity) {
const credential = await Credential.create({
access_token: authInfo.access_token,
refresh_token: authInfo.refresh_token || null,
externalId: teamId,
auth_is_valid: true,
});

// create team entity
const createObj = {
credential: credential.id,
user: 0,
name: authInfo.team.name,
externalId: teamId,
};
teamEntity = await Entity.create(createObj);
}
return teamEntity;
}

async getEntityOptions() {
// No entity options to get. Probably won't even hit this
return [];
}

async findOrCreateEntity(params) {
async findOrCreateUserEntity(params) {
const externalId = get(params, 'externalId', null);
const name = get(params, 'name', null);

Expand Down Expand Up @@ -150,7 +195,8 @@ class Manager extends ModuleManager {

async updateOrCreateCredential() {
const workspaceInfo = await this.api.authTest();
const externalId = get(workspaceInfo, 'team_id');
const isTeamUser = workspaceInfo['bot_user_id'];
const externalId = isTeamUser ? get(workspaceInfo, 'team_id') : get(workspaceInfo, 'user_id');
const updatedToken = {
access_token: this.api.access_token,
refresh_token: this.api.refresh_token,
Expand Down

0 comments on commit 03cdb23

Please sign in to comment.