Skip to content
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

Advice | Accessing a JWT Claim #25

Closed
ghost opened this issue Aug 9, 2020 · 8 comments
Closed

Advice | Accessing a JWT Claim #25

ghost opened this issue Aug 9, 2020 · 8 comments

Comments

@ghost
Copy link

ghost commented Aug 9, 2020

Noob here - what's the best way to access claims on a JWT once validation has passed?

image

@timonson
Copy link
Member

timonson commented Aug 9, 2020

Change line 60 to console.log(email). What is the output?

@ghost
Copy link
Author

ghost commented Aug 9, 2020

Here we are - any advice on how to think about this error?
image

@timonson
Copy link
Member

timonson commented Aug 9, 2020

function isObject(obj: unknown): obj is object {
  return (
    obj !== null && typeof obj === "object" && Array.isArray(obj) === false
  );
}

function has<K extends string>(
  key: K,
  x: object
): x is { [key in K]: unknown } {
  return key in x;
}

if(isObject(jwtpayload) && has("email", jwtpayload)) console.log(jwtpayload.email)

Does this work?

@ghost
Copy link
Author

ghost commented Aug 9, 2020

@timonson that works perfectly! Thank you for being so helpful.

Could the djwt library be improved to help others who might run into this use case?
This developer experience for pulling claims from JWT is just OK - (coming from my limited experience in javascript development).

Is the approach you suggested here normal? Just curious to hear your thoughts on this.

Thank you again.

@ghost ghost closed this as completed Aug 9, 2020
@timonson
Copy link
Member

timonson commented Aug 9, 2020

@PaulFish-Radius the issue is that the payload of a JWS might be any value because the specification says:

The payload can be any content and need not be a representation of a JSON object

Therefore checking if properties actually exist seems to be the right way. But I am open for better solutions of course.

Maybe it helps that the two functions isObject and hasProperty are exported in the validate.ts file now. 76cfb94

@ghost
Copy link
Author

ghost commented Aug 10, 2020

@timonson thank you for the addition - its great!
A best practice/ code example the readme for showing how to use isObject, hasProperty, isExpired would go a long way in helping others - I'm sure this will increase the popularity of the library as I'm sure its a common use case.

@timonson
Copy link
Member

I added some information to the README.md eaeecdc .

@timonson
Copy link
Member

Just for your information: My suggestions in this issue are deprecated now. The function verify returns a Payload object and is not an unknown value anymore. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant