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

Adding claim names #254

Closed
netgfx opened this issue Mar 1, 2021 · 7 comments
Closed

Adding claim names #254

netgfx opened this issue Mar 1, 2021 · 7 comments

Comments

@netgfx
Copy link

netgfx commented Mar 1, 2021

Hello,

Is it possible to include claim names https://tools.ietf.org/html/rfc7519#section-4.1 like:

iss (issuer): Issuer of the JWT
sub (subject): Subject of the JWT (the user)
aud (audience): Recipient for which the JWT is intended
exp (expiration time): Time after which the JWT expires
nbf (not before time): Time before which the JWT must not be accepted for processing
iat (issued at time): Time at which the JWT was issued; can be used to determine age of the JWT
jti (JWT ID): Unique identifier; can be used to prevent the JWT from being replayed (allows a token to be used only once)

on the payload?
Is there a proposed way to do this? I see that that JWSHeader contains some exposed properties.

Thanks.

@ghost
Copy link

ghost commented Mar 1, 2021

Hi @netgfx, you can use anything that's Data as Payload. Just encode your claims to Data and wrap it as payload.

let payload = Payload("anything you want".data(using: utf8)!)

If you're looking for a purpose built JWT library, it might make sense to check out what's available at jwt.io. JOSESwift is only aiming at providing the underlying JOSE (JWS/JWE/JWT) groundwork.

@ghost ghost closed this as completed Mar 1, 2021
@netgfx
Copy link
Author

netgfx commented Mar 1, 2021

@daniel-mohemian I opened the issue because after following the ReadMe instructions and wiki the JWS payload was indeed identified but I got the following message.

BE3AE83A-9836-4690-968A-E91837F4B93F

@ghost
Copy link

ghost commented Mar 1, 2021

What is your payload? From the jwt.io error message it seems it's not valid JSON.

Keep in mind that signed JWTs are a special subset of valid JWSs. Every signed JWT is a JWS but not every JWS is a JWT. Nothing will stop you from creating JWTs using JSOESwift. But nothing will stop you from creating a JWS which is not valid JWT either.

@netgfx
Copy link
Author

netgfx commented Mar 1, 2021

The payload is this: "{'iss':twig,'exp':1701012559.846624,'sub':Hello Project Twig,'aud':twig users,'iat':1614612559.846685,'is_root':true}" which is a valid JSON (according to JSON validators) but it is still not accepted. At this point I understand it is probably not a JOSESwift issue, and I appreciate any help provided!

@ghost
Copy link

ghost commented Mar 1, 2021

JSON required strings to be in double quotes. If you get your payload to be a valid JSON, I'm pretty sure it'll work.

See: https://www.json.org/json-en.html

@netgfx
Copy link
Author

netgfx commented Mar 1, 2021

I got it working, if it helps anyone I had to convert a dictionarty into a JSON string through the JSONEncoder.

let dic:Dictionary<String,String> = [
      "iss": iss,
      "exp": exp,
      "sub": sub,
      "aud": aud,
      "iat": iat,
      "is_root": "true"
 ]

Then

  private func jsonEncode(data:Dictionary<String,String>) -> String? {
        let encoder = JSONEncoder()
        if let jsonData = try? encoder.encode(data) {
            if let jsonString = String(data: jsonData, encoding: .utf8) {
                print(jsonString)
                return jsonString
            }
            else {
                return nil
            }
        }
        else {
            return nil
        }
    }
    

@ghost
Copy link

ghost commented Mar 1, 2021

Good to hear you got it to work. Just to be clear though, the payload can be any Data. As long as that Data contains valid JSON, the final JWS payload will be valid JSON.

This issue was closed.
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