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

JWT #12

Closed
dev4jam opened this issue May 18, 2018 · 8 comments
Closed

JWT #12

dev4jam opened this issue May 18, 2018 · 8 comments

Comments

@dev4jam
Copy link

dev4jam commented May 18, 2018

Could you please explain how to generate this N and D values from JWT? I used JWT with NodeJS but never faced with a manual configuration of those parameters.

@proggeramlug
Copy link
Member

You can read all about it here: https://tools.ietf.org/html/rfc7517

What it essentially means is that N is the modulus component of your RSA key and D is your private exponent. The names aren't picked very well, but that's what it is. ;)

This is an interesting issue however, since the creation/conversion from one project to another should be rather smooth. What format is your private key in?

@dev4jam
Copy link
Author

dev4jam commented May 18, 2018

PEM

@calebkleveter
Copy link
Contributor

@dev4jam

First, I want to note that because certificates only contain public keys, you can only read and verify signed JWTs with them, not sign them.

But for using a PEM cert, I just made an update to one of the dependencies for this project (JWTVapor), so if you update your packages (swift package update or vapor update -y) the should be a class CertService exported from it. If you go into your global configure function, you can change the JWTProvider closure to something like this:

let jwtProvider = JWTProvider { cert in
    let headers = JWTHeader(alg: "RS256", crit: ["exp", "aud"], kid: "user_manager_kid")
    return try CertService(certificate: cert, header: headers)
}

The JWT_SECRET environment variable should then be set to the PEM certificate you are using. Another option would be to create an empty environment variable (the provider expects one and will throw an error if one doesn't exist) and then read the cert from disk using FileManager.

@proggeramlug
Copy link
Member

Can you do me a favor? Try this little bash-script with your key-file and see if that works:

#!/bin/bash

data=$(openssl rsa -in my-key.key -noout -modulus)
length=${#data}
modulus=${data:8:length}
modulusMd5="test"
echo "N:"
echo $modulus | xxd -r -p | base64
data=$(openssl rsa -in dummy.key -noout -text)
p1=$(echo $data | python -c 'print(raw_input().index("privateExponent: "))+17')
p2=$(echo $data | python -c 'print(raw_input().index("prime1:"))')
d=$((p2-p1))
p=$(echo $data)
echo "D:"
echo ${p:p1:d} | xxd -r -p | base64

make sure to adjust "my-key.key" to match your file

@dev4jam
Copy link
Author

dev4jam commented May 24, 2018

It printed out:

N:
6vIAUNUo3xGPvlzP+1KxxWQ8rSmJiqAuyDJJbYqlOeqOjIVQNAC8CdKv2hisGnUTxFw+tY0Dm0girCSgePC4ma2t7USzqcoStgNUAvPXBtEY1jiPl83XByZP8qE4FSEcFvP9rLyHWiw+haRKijQl0JJnrnjaU0G4BNepWUVyNPcSVrPKo5PT297OGOjMaolIEUTF0I2pPelY1hCGus7t2stm4Qa2swAZAj6kq6QPgJzrtTYKPSfAUDSP0HCHedBXZBfxDQ7OGuxjv0WRN2O/5wJnBzGayu2DdB2JPotcMzVSi1ThyKx1ACYkbLF2ViWFSkwZNeEZzCiNsmg83narkw==
D:
NAGrtjjpGi+9/Op4k25jsYkVsVMPgiMhF+mhvWDCYEaelVZxQYGkHhyIFqP9fMHLxVyR+Qgk8zuNiUN2Ti8i/cmnRW0+w+faKnYkI58sSvMAJ3NPvc+vuIJvkgrD6lFw8VfDIHh/EaPXlyed5GfUsw2la8aOtX1HqbK/ZuZreHOIBMqP94chN9sA6Gyuv663mzhKoWJlCQGito7cCNtwYuR/mdLV2vLQsX+PENM5qO8InCJCflEjuZgP37jeb1jTiWYdPNW+3oMwo/Us5Tjn3DrYye5jefKiF+wbzapSs2nlnCL+mrA6VG1ZZ0CZ3b+V+8s++vvo3pYqDosFyb4XMQ==

@dev4jam
Copy link
Author

dev4jam commented May 24, 2018

is that exactly what I need to set in the configuration?

@proggeramlug
Copy link
Member

Those should be the values you need, yes.

Note: Since they are now public in GitHub I hope this is not some kind of production environment. :)

@dev4jam
Copy link
Author

dev4jam commented May 24, 2018

sure 😂

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

3 participants