DataPower

 View Only

 Checking Auth / Bearer Token expiry before requesting new one

Nick's profile image
Nick posted Thu January 16, 2025 04:33 PM

Hi all,

First off Happy New Year!  I hope this will make sense so bear with me.  Is it possible to store the bearer token and then check against it's expiration, and as long as it's under 24hours send the request to a backend url?  If it's over 24hours, then request new bearer and store it for the following requests in the next 24 hour period?

Not sure if that makes sense.  I've created a couple MPGWs that send requests to multiple backends after obtaining bearer tokens but they always get the bearer token on each request.  Is it possible to store it and validate against it before renewing?

Thanks in advance!

Nick

Joseph Morgan's profile image
Joseph Morgan IBM Champion

Not being sure what your possibilities are, you can consider:

  1. A signed and encrypted cookie with the bearer token.
  2. A side-call to a server that can manage sessions for DataPower?
  3. Look into eXtreme Scale?
Nick's profile image
Nick

Thanks for the quick response Joseph.  We don't have eXtreme Scale and I don't think it will be in the picture in the near future.  I'd have to check out the network to see what servers have access to / from DataPower to see if option #2 is viable.

I haven't tried the signed and encrypted cookie option, do you have any docs that explain / have examples

Thanks!

Joseph Morgan's profile image
Joseph Morgan IBM Champion

 Hi Nick,

I'm just now getting back to things after a long weekend.

Typically, you'll just write a small piece of code to encrypt and sign the token and put it into a session cookie.   You'll just use a combination of things, somewhat in this order (xslt as an example):

  1. Create or identify a signing key and an encryption key from a named set of key objects in your system.  The signing key should be the name of a crypto key object, whereas the encryption key should be the name of a shared secret key.
  2. Create a nodeset containing the bearer token and an issuing time (maybe converted to seconds with "date:seconds(date:date-time())".  Let's call that nodeset "bearer_token".  You could also opt for using an expiring time by using "date:add(date:date-time(), whatever)".
  3. Create a hash of the token using something like "dp:c14n-hash($bearer_token, false())".  Let's call this "hashed_token".
  4. Create a signature nodeset, say, "signed_token", using something like "dp:sign("algorithm like rsa-sha1", $hashed_token, concat('name:", name_of_signing_key_object))". 
  5. Then create yet another nodeset containing the token and it's signature in two elements.  Let's call this nodeset "session_data".
  6. Encrypt that nodeset into a variable with the encryption key using "dp:encrypt-data("pick appropriate algorithm - see docs", concat("name:", name_of_encryption_key_object), $session_data)".  Let's call this variable "cookie_value".
  7. Finally, set the cookie into the response header with set or append, such as "<dp:append-response-header name="'Set-Cookie'" value="$cookie_value"/>'.

I could say this is possibly a simplified form, as at some point you'll need to decrypt and validate the cookie and check the expiration date against the current time.   I also don't know for certain if you're using XSLT, so, the main reason for the xslt-ish pseudo-code.

Hermanni Pernaa's profile image
Hermanni Pernaa IBM Champion

Hi Nick, 

have you tried using distributed variables to store the token?

https://www.ibm.com/docs/en/datapower-gateway/10.6.x?topic=administration-distributed-variables

-Hermanni

Nick's profile image
Nick

Hi Hermanni,

I haven't tried distributed variables yet but will give them a look.  Thanks for the reply!