Skip to content

Instantly share code, notes, and snippets.

@stulentsev
Last active August 31, 2018 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stulentsev/890f730ab611a606be1a2905f8d48af7 to your computer and use it in GitHub Desktop.
Save stulentsev/890f730ab611a606be1a2905f8d48af7 to your computer and use it in GitHub Desktop.
require 'openssl'
require 'jwt'
require 'awesome_print'
rsa_private = OpenSSL::PKey::RSA.generate 2048
rsa_public = rsa_private.public_key
payload = {
'data' => 'test',
'projects.quote.request' => true,
'projects.quote.print' => true,
'permissions' => [ {
type: "owner",
owner_id: 1234,
permissions: ["quote:read", "quote:create", "quote:accept", ], # All Permissions
},
{
type: "organisation",
organisation_id: 1234,
permissions: ["quote:read", "quote:create", "quote:accept", ], # All Permissions
}]
}
token = JWT.encode payload, rsa_private, 'RS256'
# => "eyJhbGciOiJSUzI1NiJ9.eyJkYXRhIjoidGVzdCIsInByb2plY3RzLnF1b3RlLnJlcXVlc3QiOnRydWUsInByb2plY3RzLnF1b3RlLnByaW50Ijp0cnVlLCJwZXJtaXNzaW9ucyI6W3sidHlwZSI6Im93bmVyIiwib3duZXJfaWQiOjEyMzQsInBlcm1pc3Npb25zIjpbInF1b3RlOnJlYWQiLCJxdW90ZTpjcmVhdGUiLCJxdW90ZTphY2NlcHQiXX0seyJ0eXBlIjoib3JnYW5pc2F0aW9uIiwib3JnYW5pc2F0aW9uX2lkIjoxMjM0LCJwZXJtaXNzaW9ucyI6WyJxdW90ZTpyZWFkIiwicXVvdGU6Y3JlYXRlIiwicXVvdGU6YWNjZXB0Il19XX0.K66Z1UyWc15PxOnRqe6xAXgOce-vJygB_Xmmd4P37DwlBVAMXGQPkZ_5ga1G1Nl4X_zZrTJFeP5VvBVEkOZDob570AkXgKmttMrjiHqvkxJJp9u9g85sp40YgOUSDm5zRR5QjlG2xNkPTGRuB7SJuikrNAC-tjvSGSbg0C2ehYANXK96zeioEOZ1XTRSXJxldyKbJg7vwUp1R49vO3nUI2a0VPBF0EydIv5YYV5oadmqcH_Pt2ft9I8vFBTxpL0O6yP3IvH6bxIhIYvZ7aJzYvGCQEWCPqH526cf3Ab18kRZfAip5mQqxtEHp2hH5CJYWASKr1fbn8DtoOhfsHrzSA"
payload, headers = JWT.decode token, rsa_public, true, { algorithm: 'RS256' }
# decoded_token = JWT.decode token, OpenSSL::PKey.read(rsa_public.to_s), true, { algorithm: 'RS256' }
ap headers
ap payload
# >> {
# >> "alg" => "RS256"
# >> }
# >> {
# >> "data" => "test",
# >> "projects.quote.request" => true,
# >> "projects.quote.print" => true,
# >> "permissions" => [
# >> [0] {
# >> "type" => "owner",
# >> "owner_id" => 1234,
# >> "permissions" => [
# >> [0] "quote:read",
# >> [1] "quote:create",
# >> [2] "quote:accept"
# >> ]
# >> },
# >> [1] {
# >> "type" => "organisation",
# >> "organisation_id" => 1234,
# >> "permissions" => [
# >> [0] "quote:read",
# >> [1] "quote:create",
# >> [2] "quote:accept"
# >> ]
# >> }
# >> ]
# >> }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment