Client side SSL Certificate Authentication with Rails and Nginx
Recently, I worked on one application which required SSL client certificate based authentication. So just wanted to share it with you all about how it can be integrated in Rails application.
This article is about using SSL certificates installed into a web browser to authenticate against a Ruby on Rails application with Nginx.
Steps for creating certificates,
1) First thing you will need is to configure openssl.cnf, check following gist for configuring your openssl.cnf , as its big file so cant embed here. https://gist.github.com/pandurang90/dbe6a67339747ef5bacf In this configuration important thing is setting path to CA dir,
this is the path where you are going create your CA.
2) Then you will need to create your own CA(Certificate Authority) that issues Digital certificates. For that we will use CA.pl script is a perl script that supplies the relevant command line arguments to the openssl command for some common certificate operations. It is intended to simplify the process of certificate creation and management by the use of some simple options.
Make sure here you enter domain name in common name field when asked in this.
3) Now generate web server CSR
then self sign web server Certificate
that gives you web server certificates
4) Now its time to configure nginx server, https://gist.github.com/pandurang90/8f0c11819db4c866d985
5) Generating Client certificate,
Here,
is a subject for certificate which can be unique for each user or same based on settings in your openssl.cnf.
6) Install your certificate on web browser(p12 file), then hit url of website and it will ask to submit client certificate , just select required certificate from list and submit. Then in your controller you can get certificate using,
as we have initialized variable in nginx configuration.
You can check more options on http://nginx.org/en/docs/http/ngx_http_ssl_module.html
7) You can verify whether certificate submitted by user is valid or not , using
returns the result of client certificate verification: “SUCCESS”, “FAILED”, and “NONE” if a certificate was not present.