fix(core.gbapp): #387 adding /setupSecurity multiple tokens. Refresh token fix. @othonlima.

This commit is contained in:
Rodrigo Rodriguez 2023-12-25 17:47:23 -03:00
parent be0abaf6c7
commit b0163b6096
3 changed files with 48 additions and 36 deletions

View file

@ -602,28 +602,22 @@ export class GBVMService extends GBService {
return new Promise(resolve => setTimeout(resolve, ms));
}
const tokenStops = {};
// Setups refresh token mechanism.
const tokens = this.tokens ? this.tokens.split(',') : [];
const interval = 60000 * 60;
for(i in tokens) {
const ensureTokens = async () => {
const tokens = this.tokens ? this.tokens.split(',') : [];
for(i in tokens) {
const token = tokens[i];
tokenStops[token] = false;
const expiresOn = new Date(global[i + "_expiresOn"]);
const waitAndRefreshToken = async (token) => {
await timeout(interval);
global[i] = await sys.getCustomToken({pid, token});
if (!tokenStops[token]) {
await waitAndRefreshToken(token);
}
};
(async (token) => {
await waitAndRefreshToken(token);
})(token);
}
if (expiresOn.getTime() > new Date().getTime()) {
{token, expiresOn} = await sys.getCustomToken({pid, token});
global[i] = token;
global[i + "_expiresOn"]= expiresOn;
}
}
});
try{
${code}
@ -962,10 +956,15 @@ export class GBVMService extends GBService {
const tokens = await min.core['findParam'](min.instance, strFind);
let tokensList = [];
await CollectionUtil.asyncForEach(tokens, async t => {
const token = t.replace(strFind, '');
tokensList.push(token);
const tokenName = t.replace(strFind, '');
tokensList.push(tokenName);
try {
variables[token] = await sys.getCustomToken({pid, token});
let {token, expiresOn} = await sys.getCustomToken({pid, tokenName});
variables[token] = token;
variables[token + '_expiresOn'] = expiresOn;
} catch (error) {
variables[t] = 'ERROR: Configure /setupSecurity before using token variables.';
}

View file

@ -374,7 +374,8 @@ export class KeywordsExpressions {
if (__calls < __totalCalls && __pageMode === "auto") {
// Performs GET request using the constructed URL
await ensureTokens();
__data = await sys.getHttp ({pid: pid, file: __url, addressOrHeaders: headers, httpUsername, httpPs});
// Updates current variable handlers.
@ -669,7 +670,10 @@ export class KeywordsExpressions {
// Handles the GET http version.
else {
return `${$1} = await sys.getHttp ({pid: pid, file: ${$2}, addressOrHeaders: headers, httpUsername, httpPs})`;
return `
await ensureTokens();
${$1} = await sys.getHttp ({pid: pid, file: ${$2}, addressOrHeaders: headers, httpUsername, httpPs})
`;
}
}
];
@ -824,14 +828,20 @@ export class KeywordsExpressions {
const args = $2.split(',');
return `${$1} = await sys.postByHttp ({pid: pid, url:${args[0]}, data:${args[1]}, headers})`;
return `
await ensureTokens();
${$1} = await sys.postByHttp ({pid: pid, url:${args[0]}, data:${args[1]}, headers})
`;
}
];
keywords[i++] = [
/^\s*((?:[a-z]+.?)(?:(?:\w+).)(?:\w+)*)\s*=\s*put\s*(.*),\s*(.*)/gim,
($0, $1, $2, $3) => {
return `${$1} = await sys.putByHttp ({pid: pid, url:${$2}, data:${$3}, headers})`;
return `
await ensureTokens();
${$1} = await sys.putByHttp ({pid: pid, url:${$2}, data:${$3}, headers})
`;
}
];

View file

@ -1670,19 +1670,22 @@ export class SystemKeywords {
return res;
}
public async getCustomToken({ pid, token }): Promise<string> {
public async getCustomToken({ pid, token: tokenName }) {
const { min } = await DialogKeywords.getProcessInfo(pid);
GBLogEx.info(min, `GET TOKEN: ${token}`);
GBLogEx.info(min, `GET TOKEN: ${tokenName}`);
return await (min.adminService as any)['acquireElevatedToken']
const token = await (min.adminService as any)['acquireElevatedToken']
(min.instance.instanceId, false,
token,
min.core.getParam(min.instance, `${token} Client ID`, null),
min.core.getParam(min.instance, `${token} Client Secret`, null),
min.core.getParam(min.instance, `${token} Host`, null),
min.core.getParam(min.instance, `${token} Tenant`, null)
tokenName,
min.core.getParam(min.instance, `${tokenName} Client ID`, null),
min.core.getParam(min.instance, `${tokenName} Client Secret`, null),
min.core.getParam(min.instance, `${tokenName} Host`, null),
min.core.getParam(min.instance, `${tokenName} Tenant`, null)
);
const expiresOn = await min.adminService.getValue(min.instanceId, `${tokenName}expiresOn`);
return { token, expiresOn };
}
@ -2586,7 +2589,7 @@ export class SystemKeywords {
array = [];
}
if (!baseUrl){
if (!baseUrl) {
let obj = await GBDeployer.internalGetDriveClient(min);
baseUrl = obj.baseUrl;
client = obj.client;