Conversation
| * console.log(identity.email, identity.name); | ||
| * ``` | ||
| */ | ||
| public getTokenIdentity(): TokenIdentity { |
There was a problem hiding this comment.
getTokenIdentity seems very technical name. IMO it should be getUserDetails
There was a problem hiding this comment.
getUserDetails might seem like we are querying our backend for user info. Since all we are doing is decoding a token, used getTokenIdentity
There was a problem hiding this comment.
then by this logic, even getToken method can also be interpreted by someone in the same manner
There was a problem hiding this comment.
Yes, even this method is on top of getToken. Just a helper for easier SDK usage, nothing more.
There was a problem hiding this comment.
from a user's POV, I dont like this name
There was a problem hiding this comment.
I'm open to changing. Maybe getTokenUserDetails ?
| } | ||
|
|
||
| const token = this.getToken(); | ||
| if (!token) { |
There was a problem hiding this comment.
Is there any scenario possible where this.isAuthenticated() is true but token is undefined/null? If yes, then we should handle that scenario.
There was a problem hiding this comment.
The if condition is checking the exact scenario you are saying isnt it?
There was a problem hiding this comment.
I meant to ask how will this scenario even occur? It should never be the case where this.isAuthenticated() is true but token is undefined/null
e483d3d to
fd9fc52
Compare
|



Prompt used
This is the Typescript SDK repo. We want to add a util method to into src/core/uipath.ts named getTokenIdentity()The util method should be able to retrieve details of current logged in user from the jwt token stored. We already expose isAuthenticated method. Use that to validate if user has already been authenticated. If not authenticated throw an error. We already expose getToken() method to get the token. Use the same method internally to get the token, decode the jwt token. Response should be consisting of below 5 fields.
email
first_name
last_name
preferred_username
name
Follow coding and naming conventions mentioned in repo.
Generated claude plan
Plan: Add
getTokenIdentity()to UiPath core classContext
Consumers of the SDK currently have no first-class way to read identity claims (email, name, etc.) for the authenticated user. The only hook is
getToken(), which returns the raw JWT string — forcing every app to pull in a JWT library and implement base64url decoding themselves. This util exposes the identity claims directly from the token already held in memory, so coded-apps (and any other SDK consumer) can render the current user without an extra round-trip or dependency.The method is a local, read-only utility — no API call, just decode the JWT payload we already have.
Design
Method signature
Synchronous, no parameters. Mirrors the style of the existing
isAuthenticated()/getToken()methods onsrc/core/uipath.ts— plain public method, JSDoc block, no@trackdecorator (consistent with other UiPath core class methods — decorators are only used on service methods).Return type
New interface
TokenIdentityadded to src/core/auth/types.ts (existing home forTokenInfo,AuthToken,OAuthContext— no need for a new file):All fields optional — JWT claims are not guaranteed to be present, and marking them required would cause runtime
undefinedaccess. Raw JWT claims use snake_case (first_name,last_name,preferred_username) and are mapped to camelCase to match the rest of the SDK's public surface.Behavior
!this.isAuthenticated()→ throwAuthenticationErrorwith message'User is not authenticated. Call initialize() before getTokenIdentity().'this.getToken(). If undefined (defensive — shouldn't happen whenisAuthenticated()is true, but possible with secret-auth empty-token path atsrc/core/uipath.ts:120) → throwAuthenticationError..— must be 3 segments. Otherwise → throwValidationErrorwith message'Invalid JWT token format.'-→+,_→/, pad with=to multiple of 4), thendecodeBase64()from src/utils/encoding/base64.ts andJSON.parse(). Any failure in this chain → throwValidationErrorwith message'Failed to decode JWT token payload.'Error type choices
AuthenticationErrorfrom src/core/errors/authentication.ts. The error type is designed for "missing authentication" (see class JSDoc).ValidationErrorfrom src/core/errors/validation.ts. Peragent_docs/conventions.mderror-types section:ValidationErroris for invalid data format (user-supplied input),ServerErroris for failed parsing of API response data. A malformed JWT sitting in auth state is closer to invalid input than a server response issue. This is a judgment call; usingServerErrorhere would also be defensible, butValidationErrormatches the "invalid data format" description more directly.Files to modify
TokenIdentityinterfacegetTokenIdentity()method aftergetToken()(line 239); add imports forTokenIdentity,AuthenticationError,ValidationError,decodeBase64export type { TokenIdentity }alongside existingTokenInfoexport (line 48)No changes needed to src/index.ts — it doesn't explicitly re-export
TokenInfoeither, soTokenIdentityfollows the same pattern (available via@uipath/uipath-typescript/core).No changes to
package.json,rollup.config.js,mkdocs.yml— this is a method on an existing class exported through an existing path, not a new service.No
docs/oauth-scopes.mdentry — this method makes no API call, so no OAuth scope is required.No
docs/pagination.mdentry — not a paginated method.Reused utilities
decodeBase64()— src/utils/encoding/base64.ts:33. Cross-platform (browseratob/ NodeBuffer).AuthenticationError— src/core/errors/authentication.ts.ValidationError— src/core/errors/validation.ts.isAuthenticated()/getToken()— existing methods onUiPathclass.JSDoc
Example to include in the method's JSDoc:
Tests
Add a new
describe('getTokenIdentity')block to tests/unit/core/uipath.test.ts. The existing test file already mocksAuthServicewith configurablehasValidTokenandgetToken— extend the mock so individual tests can override the returned token.Cases to cover:
{ email, first_name, last_name, preferred_username, name }).undefinedwhen claims are missing from the payload.AuthenticationErrorwhenisAuthenticated()is false.ValidationErrorwhen token is not a valid JWT structure (e.g.,'not.a.jwt'has 3 segments but the middle is not valid base64url JSON; also test 2-segment and 1-segment strings).ValidationErrorwhen payload base64 decodes but is not valid JSON.Use
TEST_CONSTANTSstyle; build the fixture JWT inline with a small helper in the test file (no new test util needed for a single use).Verification
Manual end-to-end check (optional, relies on a live auth setup):
Confirm the console output shows the 5 camelCase fields populated from the real token claims.
Local Testing