-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(ProfileInfo): JWT token expiration detection #2298
Conversation
Signed-off-by: Trae Yelovich <[email protected]>
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #2298 +/- ##
==========================================
+ Coverage 91.07% 91.08% +0.01%
==========================================
Files 628 628
Lines 17874 17896 +22
Branches 3842 3848 +6
==========================================
+ Hits 16278 16300 +22
Misses 1595 1595
Partials 1 1 ☔ View full report in Codecov by Sentry. |
Signed-off-by: Trae Yelovich <[email protected]>
fbf581e
to
f52417e
Compare
Signed-off-by: Trae Yelovich <[email protected]>
Signed-off-by: Trae Yelovich <[email protected]>
Signed-off-by: Trae Yelovich <[email protected]>
I don't have permissions on Zowe CLI to update the new issue reported by SonarCloud, but it is a false positive as |
Signed-off-by: Trae Yelovich <[email protected]>
// Cannot decode LTPA tokens without private key | ||
if (tokenTypeProp?.argValue == "LtpaToken2") { | ||
return false; | ||
} | ||
|
||
const fullToken = tokenValueProp.argValue.toString(); | ||
// JWT format: [header].[payload].[signature] | ||
const tokenParts = fullToken.split("."); | ||
try { | ||
const payloadJson = JSON.parse(Buffer.from(tokenParts[1], "base64url").toString("utf8")); | ||
if ("exp" in payloadJson) { | ||
// The expire time is stored in seconds since UNIX epoch. | ||
// The Date constructor expects a timestamp in milliseconds. | ||
const msPerSec = 1000; | ||
const expireDate = new Date(payloadJson.exp * msPerSec); | ||
return expireDate < new Date(); | ||
} | ||
} catch (err) { | ||
return false; | ||
} | ||
|
||
return false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding this to the ProfileInfo API! As the ProfileInfo API is used only by extenders like ZE, I'm curious if we'd want to move this part of the method to make it usable by CLI itself?
Perhaps we could have a static isTokenExpired
method on the ConfigUtils
or RestStandaloneUtils
classes, that only checks if the token is expired and doesn't handle loading it from config.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call - I added a function to ConfigUtils
called hasTokenExpired
and updated the ProfileInfo
function to use it.
Also added an entry for the new function in the changelog 👍
Signed-off-by: Trae Yelovich <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks @traeok!
Signed-off-by: Trae Yelovich <[email protected]>
Signed-off-by: Trae Yelovich <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! 😋
looking at the code reminded me of this issue:
Quality Gate passedIssues Measures |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requesting some edits to the changelog
Signed-off-by: Trae Yelovich <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fantastic changelog @traeok!
Release succeeded for the The following packages have been published:
Powered by Octorelease 🚀 |
What It Does
Fernando mentioned that extenders would benefit from having access to the JWT token expiration feature from Zowe Explorer, so I figured it made the most sense to put it the logic for it in
ProfileInfo
. That way, anyone using Imperative to access/manage profiles can leverage this functionality.How to Test
Once merged, this can be tested in action after this PR is updated: zowe/zowe-explorer-vscode#3174
Here's a manual test to show the behavior of the function itself:
npm run build && npm pack
from thepackages/imperative
folderzowe-imperative-8.1.0.tgz
file from this folder and place it into a new folder outside of the repocd
to the empty folder and runzowe config init --no-prompt && npm init -y && npm install zowe-imperative-8.1.0.tgz @zowe/secrets-for-zowe-sdk
(you don't need secrets SDK, but its easier than getting around cred manager setup in ProfileInfo)zowe.config.json
:script.mjs
within this test folder and run it usingnode script.mjs
The script should output
token expired: true
.Review Checklist
I certify that I have:
Additional Comments