Skip to content

Commit

Permalink
fix Graph API refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanrmoore committed Aug 29, 2024
1 parent 3fb5cc6 commit f810f00
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
22 changes: 22 additions & 0 deletions api-module-library/microsoft-teams/api/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,28 @@ class graphApi extends OAuth2Requester {
}
}

async refreshAccessToken(refreshTokenObject) {
this.access_token = undefined;
const params = new URLSearchParams();
params.append('grant_type', 'refresh_token');
params.append('client_id', this.client_id);
params.append('client_secret', this.client_secret);
params.append('refresh_token', refreshTokenObject.refresh_token);
params.append('redirect_uri', this.redirect_uri);
params.append('scope', this.scope);

const options = {
body: params,
url: this.tokenUri,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
};
const response = await this._post(options, false);
await this.setTokens(response);
return response;
}

setTenantId(tenantId) {
this.tenant_id = tenantId;
this.generateUrls();
Expand Down
19 changes: 13 additions & 6 deletions api-module-library/microsoft-teams/manager.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const { debug, flushDebugLog } = require('@friggframework/logs');
const { get } = require('@friggframework/assertions');
const { ModuleManager, ModuleConstants} = require('@friggframework/module-plugin');
const {
ModuleManager,
ModuleConstants,
} = require('@friggframework/module-plugin');
const { Api } = require('./api/api');
const { graphApi } = require('./api/graph');
const { botFrameworkApi } = require('./api/botFramework');
Expand All @@ -15,7 +18,11 @@ class Manager extends ModuleManager {
constructor(params) {
super(params);
this.tenant_id = get(params, 'tenant_id', null);
this.redirect_uri= get(params, 'redirect_uri', `${process.env.REDIRECT_URI}/microsoft-teams`)
this.redirect_uri = get(
params,
'redirect_uri',
`${process.env.REDIRECT_URI}/microsoft-teams`
);
}

//------------------------------------------------------------
Expand All @@ -39,13 +46,14 @@ class Manager extends ModuleManager {

if (params.entityId) {
instance.entity = await Entity.findById(params.entityId);
instance.tenant_id = instance.entity.externalId;
instance.credential = await Credential.findById(
instance.entity.credential
);
teamsParams = {
...teamsParams,
...instance.credential.toObject(),
tenant_id: instance.entity.externalId
tenant_id: instance.entity.externalId,
};
}
instance.api = new Api(teamsParams);
Expand Down Expand Up @@ -83,15 +91,14 @@ class Manager extends ModuleManager {
}
const authRes = await this.testAuth();
if (!authRes) throw new Error('Auth Error');
}
else {
} else {
await this.api.getTokenFromClientCredentials();
const authCheck = await this.testAuth();
if (!authCheck) throw new Error('Auth Error');
}

const orgDetails = await this.api.graphApi.getOrganization();
this.tenant_id = orgDetails.id;
this.tenant_id = orgDetails.id;
await this.findAndUpsertCredential({
tenant_id: this.tenant_id,
graph_access_token: this.api.graphApi.access_token,
Expand Down

0 comments on commit f810f00

Please sign in to comment.