Skip to content

Commit

Permalink
Remove test code + additional improvements to token manager
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-tavares committed Jan 23, 2025
1 parent c1f6591 commit 51ffc1d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ export class MicrosoftDefenderEndpointConnector extends SubActionConnector<
responseSchema:
MicrosoftDefenderEndpointDoNotValidateResponseSchema as unknown as SubActionRequestParams<R>['responseSchema'],
headers: {
Authorization: `Bearer ${
'--invalid-test--' + (await this.oAuthToken.get(connectorUsageCollector))
}`,
Authorization: `Bearer ${await this.oAuthToken.get(connectorUsageCollector)}`,
},
};
let response: AxiosResponse<R>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,42 @@ export class OAuthTokenManager {

this.reGenerateNewTokenPromise = new Promise(async (resolve, reject) => {
try {
const connectorTokenClient = this.params.services.connectorTokenClient;

if (this.generatingNewTokenPromise) {
this.logger.debug(
`Waiting for current new token retrieval/generate to complete before performing proceeding with generating new one`
);
await this.generatingNewTokenPromise;
}

// First check if the token was already updated by another instance of the connector and if so, then do nothing
if (this.connectorToken) {
const currentToken = this.connectorToken.token;
const latestStoredToken = await connectorTokenClient.get({
connectorId: this.params.connector.id,
tokenType: this.tokenType,
});

if (
latestStoredToken.connectorToken &&
latestStoredToken.connectorToken.token !== currentToken
) {
this.logger.debug(
`Token has been updated since it was last read. Using it instead of generating a new one.\n${JSON.stringify(
{
...latestStoredToken.connectorToken,
token: '[redacted]',
},
null,
2
)}`
);
this.connectorToken = latestStoredToken.connectorToken;
return resolve(undefined);
}
}

await this.fetchAndStoreNewToken(connectorUsageCollector);
this.connectorToken = null;

Expand Down

0 comments on commit 51ffc1d

Please sign in to comment.