| 1 |
package jwt |
| 2 |
|
| 3 |
import ( |
| 4 |
"github.com/urfave/cli" |
| 5 |
) |
| 6 |
|
| 7 |
|
| 8 |
func Command() cli.Command { |
| 9 |
return cli.Command{ |
| 10 |
Name: "jwt", |
| 11 |
Usage: "sign and verify data using JSON Web Tokens (JWT)", |
| 12 |
UsageText: "step crypto jwt <subcommand> [arguments] [global-flags] [subcommand-flags]", |
| 13 |
Description: `A JSON Web Token or JWT (pronounced "jot") is a compact data structure used |
| 14 |
to represent some JSON encoded "claims" that are passed as the payload of a |
| 15 |
JWS or JWE structure, enabling the claims to be digitally signed and/or |
| 16 |
encrypted. The "claims" (or "claim set") are represented as an ordinary JSON |
| 17 |
object. JWTs are represented using a compact format that's URL safe and can be |
| 18 |
used in space-constrained environments. JWTs can be passed in HTTP |
| 19 |
Authorization headers and as URI query parameters. |
| 20 |
|
| 21 |
A "claim" is a piece of information asserted about a subject, represented as a |
| 22 |
key/value pair. Logically a verified JWT can be interpreted as "<issuer> says to |
| 23 |
<audience> that <subject>'s <claim-name> is <claim-value>" for each claim. |
| 24 |
|
| 25 |
A JWT signed using JWS has three parts: |
| 26 |
|
| 27 |
1. A base64 encoded JSON object representing the JOSE (JSON Object |
| 28 |
Signing and Encryption) header that describes the cryptographic |
| 29 |
operations applied to the JWT Claims Set |
| 30 |
2. A base64 encoded JSON object representing the JWT Claims Set |
| 31 |
3. A base64 encoded digital signature of message authentication code |
| 32 |
|
| 33 |
## EXAMPLES |
| 34 |
|
| 35 |
Create a signed JWT using a JWK (with line breaks for display purposes only): |
| 36 |
''' |
| 37 |
$ step crypto jwt sign --key p256.priv.json --iss "[email protected]" \ |
| 38 |
--aud "https://example.com" --sub auth --exp $(date -v+1M +"%s") |
| 39 |
eyJhbGciOiJFUzI1NiIsImtpZCI6IlpqR1g5N0xtY2ZsUG9sV3Zzb0FXekM1V1BXa05GRkgzUWRLTFVXOTc4aGsiLCJ0eXAiOiJKV1QifQ |
| 40 |
. |
| 41 |
eyJhdWQiOiJodHRwczovL2V4YW1wbGUuY29tIiwiZXhwIjoxNTM1MjM2MTUyLCJpYXQiOjE1MzI1NTc3NTQsImlzcyI6ImpvZUBleGFtcGxlLmNvbSIsIm5iZiI6MTUzMjU1Nzc1NCwic3ViIjoiYXV0aCJ9 |
| 42 |
. |
| 43 |
Z4veKtRmZLoqHNlTrcYo2W1ikLkDcSNfrT52zAGS9cF90Zi3aTXt_75pkikREvMrkC4mhGDdqxCf9ZHq4VnSvg |
| 44 |
''' |
| 45 |
|
| 46 |
Create a signed JWT using a JWK and a custom payload: |
| 47 |
''' |
| 48 |
$ echo '{"srv":"https://srv.example.com"}' | step crypto jwt sign \ |
| 49 |
--key p256.priv.json --iss "[email protected]" \ |
| 50 |
--aud "https://example.com" --sub auth --exp $(date -v+1M +"%s") |
| 51 |
eyJhbGciOiJFUzI1NiIsImtpZCI6IlpqR1g5N0xtY2ZsUG9sV3Zzb0FXekM1V1BXa05GRkgzUWRLTFVXOTc4aGsiLCJ0eXAiOiJKV1QifQ |
| 52 |
. |
| 53 |
eyJhdWQiOiJodHRwczovL2V4YW1wbGUuY29tIiwiZXhwIjoxNTM1MjQyNDcyLCJpYXQiOjE1MzI1NjQwNzMsImlzcyI6ImpvZUBleGFtcGxlLmNvbSIsIm5iZiI6MTUzMjU2NDA3Mywic3J2IjoiaHR0cHM6Ly9zcnYuZXhhbXBsZS5jb20iLCJzdWIiOiJhdXRoIn0 |
| 54 |
. |
| 55 |
DlSkxICjk2h1LarwJgXPbXQe7DwpLMOCvWp3I4GMcBP_5_QYPhVNBPQEeTKAUuQjYwlxZ5zVQnyp8ujvyf1Lqw |
| 56 |
''' |
| 57 |
|
| 58 |
Verify the the previous token: |
| 59 |
''' |
| 60 |
$ echo $TOKEN | step crypto jwt verify --key p256.pub.json --iss "[email protected]" --aud "https://example.com" |
| 61 |
{ |
| 62 |
"header": { |
| 63 |
"alg": "ES256", |
| 64 |
"kid": "ZjGX97LmcflPolWvsoAWzC5WPWkNFFH3QdKLUW978hk", |
| 65 |
"typ": "JWT" |
| 66 |
}, |
| 67 |
"payload": { |
| 68 |
"aud": "https://example.com", |
| 69 |
"exp": 1535242472, |
| 70 |
"iat": 1532564073, |
| 71 |
"iss": "[email protected]", |
| 72 |
"nbf": 1532564073, |
| 73 |
"srv": "https://srv.example.com", |
| 74 |
"sub": "auth" |
| 75 |
}, |
| 76 |
"signature": "DlSkxICjk2h1LarwJgXPbXQe7DwpLMOCvWp3I4GMcBP_5_QYPhVNBPQEeTKAUuQjYwlxZ5zVQnyp8ujvyf1Lqw" |
| 77 |
} |
| 78 |
''' |
| 79 |
|
| 80 |
Read the information in the previous token without verifying it: |
| 81 |
''' |
| 82 |
$ echo $TOKEN | step crypto jwt inspect --insecure |
| 83 |
{ |
| 84 |
"header": { |
| 85 |
"alg": "ES256", |
| 86 |
"kid": "ZjGX97LmcflPolWvsoAWzC5WPWkNFFH3QdKLUW978hk", |
| 87 |
"typ": "JWT" |
| 88 |
}, |
| 89 |
"payload": { |
| 90 |
"aud": "https://example.com", |
| 91 |
"exp": 1535242472, |
| 92 |
"iat": 1532564073, |
| 93 |
"iss": "[email protected]", |
| 94 |
"nbf": 1532564073, |
| 95 |
"srv": "https://srv.example.com", |
| 96 |
"sub": "auth" |
| 97 |
}, |
| 98 |
"signature": "DlSkxICjk2h1LarwJgXPbXQe7DwpLMOCvWp3I4GMcBP_5_QYPhVNBPQEeTKAUuQjYwlxZ5zVQnyp8ujvyf1Lqw" |
| 99 |
} |
| 100 |
'''`, |
| 101 |
Subcommands: cli.Commands{ |
| 102 |
signCommand(), |
| 103 |
verifyCommand(), |
| 104 |
inspectCommand(), |
| 105 |
}, |
| 106 |
} |
| 107 |
} |