Regex to obtain Token - Zabbix

Asked

Viewed 53 times

0

I’m trying to collect two sets of characters in one token authentication. The intention is that the regex collect the word bearer and the outcome of access_token.

I’m having a hard time uniting both collections.

It is possible to perform both collect in the same regex?

Token below - Image and text.

inserir a descrição da imagem aqui

{
  "token_type": "bearer",
  "refresh_token": "MjUwMDAwMTJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIzNTMxMzI1MDAwMDE2NiIsImlhdCI6MTYxMjUzMjE2NSwibmJmIjoxNjEyNTMyMTY1LCJleHAiOjE2MTI1MzU3NjUsImNsaWVudF9pZCI6Im1vYmlsZUZpcnN0RGF0YSIsInVzZXJfbmFtZSI6IjM1MzEzMjUwMDAwMTY2IiwiaW5zdGl0dXRpb24iOiIwMDAwMDAwMyIsImVzdGFibGleyJ0eXAiOijpbIjg5NDUwNDIzIl0sImRldmljZSI6IlBvc3RtYW4iLCJ0b2tlbl90eXBlIjoicmVmcmVzaCIsIm9yaWdpbl9wb3MiOmZhbHNlfQ.CEwEPwRXNN0OrcYk7rc2FFzPwMGPhOKnjUUfuR97bN7hfmwEMp_dQQ4mLRod4xuv32xlEFLPvmxtiM-9IiA53w",
  "refresh_expires_in": 3600,
  "expires_in": 900,
  "access_token": "osjjjjsssjJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIzNTMxMzI1MDAwMDE2NiIsImlhdCI6MTYxMjUzMjE2NSwibmJmIjoxNjEyNTMyMTY1LCJleHAiOjE2MTI1MzMwNjUsImNsaWVudF9pZCI6Im1vYmlsZUZpcnN0RGF0YSIsInVzZXJfbmFtZSI6IjM1MzEzMjeyJ0eXAiOiIiwiaW5zdGl0dXRpb24iOiIwMDAwMDAwMyIsImVzdGFibGlzaG1lbnRzIjpbIjg5NDUwNDIzIl0sImRldmljZSI6IlBvc3RtYW4ifQ._gEPrD2KsX1sFUwPmGSL0ZORSDRypwFd3u0Z-YhUowvE3iqDK0I_dpdkf_azCIUqs4JNpmj-zkKtmieRIz6afQ",
  "accepted_contract_term": true,
  "is_origin_pos": false
}
  • Is there a reason not to parse the json, and access the values via get using a programming language?

  • @Danizavtz, thanks for the reply. It has been suggested to use json, however, according to the Zabbix forum, the web scenarios do not support. This way I’m insisting with the regex.

1 answer

0

Following the idea of having the two returns in the same regex, one exit would be this:

/"token_type":.?"(.*?)",|"access_token":.?"(.*?)",/g

Where in Group 1, it returns what is within the first parentheses and in Group 2, the second.

bearer

access_token

In Javascript, this would be a way:

const zabbix = `{
   "token_type": "bearer",
   "refresh_token": "MjUwMDAwMTJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIzNTMxMzI1MDAwMDE2NiIsImlhdCI6MTYxMjUzMjE2NSwibmJmIjoxNjEyNTMyMTY1LCJleHAiOjE2MTI1MzU3NjUsImNsaWVudF9pZCI6Im1vYmlsZUZpcnN0RGF0YSIsInVzZXJfbmFtZSI6IjM1MzEzMjUwMDAwMTY2IiwiaW5zdGl0dXRpb24iOiIwMDAwMDAwMyIsImVzdGFibGleyJ0eXAiOijpbIjg5NDUwNDIzIl0sImRldmljZSI6IlBvc3RtYW4iLCJ0b2tlbl90eXBlIjoicmVmcmVzaCIsIm9yaWdpbl9wb3MiOmZhbHNlfQ.CEwEPwRXNN0OrcYk7rc2FFzPwMGPhOKnjUUfuR97bN7hfmwEMp_dQQ4mLRod4xuv32xlEFLPvmxtiM-9IiA53w",
   "refresh_expires_in": 3600,
   "expires_in": 900,
   "access_token": "osjjjjsssjJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIzNTMxMzI1MDAwMDE2NiIsImlhdCI6MTYxMjUzMjE2NSwibmJmIjoxNjEyNTMyMTY1LCJleHAiOjE2MTI1MzMwNjUsImNsaWVudF9pZCI6Im1vYmlsZUZpcnN0RGF0YSIsInVzZXJfbmFtZSI6IjM1MzEzMjeyJ0eXAiOiIiwiaW5zdGl0dXRpb24iOiIwMDAwMDAwMyIsImVzdGFibGlzaG1lbnRzIjpbIjg5NDUwNDIzIl0sImRldmljZSI6IlBvc3RtYW4ifQ._gEPrD2KsX1sFUwPmGSL0ZORSDRypwFd3u0Z-YhUowvE3iqDK0I_dpdkf_azCIUqs4JNpmj-zkKtmieRIz6afQ",
   "accepted_contract_term": true,
   "is_origin_pos": false
}`;

const regex = /"token_type":.?"(.*?)",|"access_token":.?"(.*?)",/g;

// Test
const token_type = regex.exec(zabbix)[1];
const access_token = regex.exec(zabbix)[2];

console.log(token_type, access_token);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.