Skip to content

Instantly share code, notes, and snippets.

@sobstel
Created February 9, 2017 10:48
Show Gist options
  • Save sobstel/1a701a162a32badcce6e01e4974b313e to your computer and use it in GitHub Desktop.
Save sobstel/1a701a162a32badcce6e01e4974b313e to your computer and use it in GitHub Desktop.
Rails, Auth0, JWT, RS256
module Authenticable
extend ActiveSupport::Concern
# Filter to use with before_action
def authenticate_user
jwt_token
rescue JWT::DecodeError => e
render json: { error: e.message }, status: :unauthorized
end
# @return [User|nil]
def current_user
@current_user ||= User.new jwt_token.try(:first)
rescue JWT::DecodeError
nil
end
private
def jwt_token
bearer = request.env.fetch('HTTP_AUTHORIZATION', '').slice(7..-1)
rsa_public_key = OpenSSL::PKey::RSA.new(
OpenSSL::X509::Certificate.new(
Rails.configuration.auth0['signing_certificate']
).public_key
)
JWT.decode bearer, rsa_public_key, true, algorithm: 'RS256'
end
end
module App
class Application < Rails::Application
# (...)
config.auth0 = config_for(:auth0)
end
end
production: &default
# taken from auth0.com: Clients > AppJobs > Settings > Advanced Settings > Certificates
signing_certificate: |
-----BEGIN CERTIFICATE-----
...
-----END CERTIFICATE-----
development:
<<: *default
test:
<<: *default
# generated with `openssl req -x509 -newkey rsa:2048 -keyout privatekey.pem -out certificate.pem`
signing_certificate: |
-----BEGIN CERTIFICATE-----
MIIDtTCCAp2gAwIBAgIJAPunMNM1vt7wMA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV
BAYTAkFVMRMwEQYDVQQIEwpTb21lLVN0YXRlMSEwHwYDVQQKExhJbnRlcm5ldCBX
aWRnaXRzIFB0eSBMdGQwHhcNMTcwMjA5MDk1MTEzWhcNMTcwMzExMDk1MTEzWjBF
MQswCQYDVQQGEwJBVTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50
ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB
CgKCAQEAnEUQtIH4cmK9j41CC5tyk0v8CJNC803ie4vJPa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7Lc18S/IilWLI0a0pJEeWXJDlsICkFfYMRmm2WG/4NfHFH9GiEVN71AVQt
+r1j68kDiZdgSFI/tlw9G+fN3jcJVQ4grg4w6RnP5xSWdp7fJFH1pft6WfZy4n6f
xiFId8Cl81lJ4ptMh+kib0usR0i2zwDK8fSYIr3RmOOw3G4monSDrKGNSpVTImDD
pAI0B+AxzGU+p1v0PasQ72wLv5fx21yOZTlS/c2dC6qlfPDqUXT1PY8O4Yz1hhUS
bGujavaIenPmeFVtE/io3M5Ge+Nzv6Rt9zvuXJfrxpwnK5X6gI2mTPv0rCXrGODG
YUfGSiexYkFMSu+gZNbwg7SL715wM5Np7evJcpvALqwvLRuHlWE/7gR6vC0ywg0N
Sg8NRheEdP2LIejRXqQ5l73w3A4wSzEgipdrH4azQBWUh6F7eCLjfOjC80lRosT+
IidiK9THkzZykLlKrfuT2VaSEjmVg6vpamTG/fu6fea+35MnY6Aq1g==
-----END RSA PRIVATE KEY-----
private_key_passphrase: appjobs
module Helpers
def current_user_headers(payload_overwrites = {})
payload = {
email: '[email protected]',
email_verified: true,
nickname: 'Current User',
iss: 'https://YOURAPP.auth0.com/',
sub: 'auth0|123456789',
aud: 'YOUR_AUD',
exp: (Time.now + 1.day).to_i,
iat: Time.now.to_i
}.merge(payload_overwrites)
@private_key ||= OpenSSL::PKey::RSA.new(
Rails.configuration.auth0['private_key'],
Rails.configuration.auth0['private_key_passphrase']
)
id_token = JWT.encode payload, @private_key, 'RS256'
{ 'Authorization' => "Bearer #{id_token}" }
end
end
require 'rails_helper'
describe 'User' do
specify 'current user info', do
get '/user', headers: current_user_headers
expect(response).to be_success
expect(response.body).to have_json_size(8)
expect(response.body).to be_json_eql('Current User'.to_json).at_path('nickname')
expect(response.body).to be_json_eql('[email protected]'.to_json).at_path('email')
end
specify 'jwt: signature has expired' do
get '/user', headers: current_user_headers(exp: Time.now - 1.day)
expect(response).not_to be_success
expect(response.body).to have_json_size(1)
expect(response.body).to be_json_eql('Signature has expired'.to_json).at_path('error')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment