Skip to content

Instantly share code, notes, and snippets.

@alassek
Created May 5, 2014 22:00
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 alassek/54a00db91c0a0717bf3b to your computer and use it in GitHub Desktop.
Save alassek/54a00db91c0a0717bf3b to your computer and use it in GitHub Desktop.
SSL Auth for Services
class ApplicationController < ActionController::Base
before_filter :require_authentication
private
def require_authentication
unless current_certificate.verify(public_key)
head :forbidden
end
end
def public_key
@public_key ||= OpenSSL::PKey::RSA.new(ENV['AUTH_PUBLIC_KEY'])
end
def current_certificate
@current_certificate ||= OpenSSL::X509::Certificate.new(request.headers['X-SSL-Auth'])
end
# Identify the client application for access control
def current_client
current_certificate.issuer.to_a.assoc('OU')[1]
end
end
openssl genrsa -out master.key 1024
openssl req -new -key master.key -out web-client.csr
openssl x509 -req -in web-client.csr -signkey master.key -out web-client.crt
# Servers don't need the private master to authenticate certs
# requests are authenticated via the public key, private key is kept secret
openssl rsa -in master.key -pubout > master.pub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment