SSL client certificate authentication using ruby (Sinatra)


Tom Crudy

How can I authorize an API in sinatra so that only callers with a known client certificate (or a certificate issued by a trusted CA) can call it?

Currently, I'm using a "thin" webserver, but I'm open to other options if needed.

Uri Agassi

You can nginxuse to take care of client certificates - here is a blog post showing how to set it up:

server {
    listen        443;
    ssl on;
    server_name example.com;

    ssl_certificate      /etc/nginx/certs/server.crt;
    ssl_certificate_key  /etc/nginx/certs/server.key;
    ssl_client_certificate /etc/nginx/certs/ca.crt;
    ssl_verify_client optional;

    location / {
        root           /var/www/example.com/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME /var/www/example.com/lib/Request.class.php;
        fastcgi_param  VERIFIED $ssl_client_verify;
        fastcgi_param  DN $ssl_client_s_dn;
        include        fastcgi_params;
    }
}

We specify the server's certificate (server.crt) and private key (server.key). We specify the CA certificate used to sign the client certificate (ca.crt). Set ssl_verify_client to optional. If provided, this tells nginx to try to verify the SSL certificate. My API allows both authenticated and unauthenticated requests, however, if you only want to allow authenticated requests, you can go ahead and set this value to on.

You can use thinwithnginx , but I believe using passengerwithnginx is more popular in this case and easier to deploy.


ssl_verify_client optionalExplained here :

ssl_verify_client

Syntax:   ssl_verify_client on | off | optional | optional_no_ca
Default:  off
Context:   http | server
Reference:    ssl_verify_client

This directive enables client authentication. The parameter "optional" checks the client's identity using its certificate, in case the client is available to the server.

Related


Java: SSL client authentication with self signed certificate

Chris: I'm trying to secure a connection for a Java client/server application that communicates over the internet. My idea is to use SSL sockets with self signed certificates and client authentication. I did the following: Server: The keystore containing the n

Java: SSL client authentication with self signed certificate

Chris: I'm trying to secure a connection for a Java client/server application that communicates over the internet. My idea is to use SSL sockets with self signed certificates and client authentication. I did the following: Server: The keystore containing the n

Java: SSL client authentication with self signed certificate

Chris: I'm trying to secure a connection for a Java client/server application that communicates over the internet. My idea is to use SSL sockets with self signed certificates and client authentication. I did the following: Server: The keystore containing the n

Java: SSL client authentication with self signed certificate

Chris: I'm trying to secure a connection for a Java client/server application that communicates over the internet. My idea is to use SSL sockets with self signed certificates and client authentication. I did the following: Server: The keystore containing the n

mac authentication fails using Node with SSL certificate

Kelvin As specified in the Node api documentation, I tried the first one using openssl with a self-created and signed certificate. Everything is going fine except the android test client cannot pass because it requires a ca certificate. When I try the second m

mac authentication fails using Node with SSL certificate

Kelvin As specified in the Node api documentation, I tried the first one using openssl with a self-created and signed certificate. Everything is going fine except the android test client cannot pass because it requires a ca certificate. When I try the second m

Get client certificate without authentication using Erlang

Thomas I'm fairly new to Erlang and want to write a small server that uses client certificates to identify clients. Clients should be able to use any public/private key pair without having to make it part of the certificate chain. I looked at the SSL example f

Get client certificate without authentication using Erlang

Thomas I'm fairly new to Erlang and want to write a small server that uses client certificates to identify clients. Clients should be able to use any public/private key pair without having to make it part of the certificate chain. I looked at the SSL example f

Get client certificate without authentication using Erlang

Thomas I'm fairly new to Erlang and want to write a small server that uses client certificates to identify clients. Clients should be able to use any public/private key pair without having to make it part of the certificate chain. I looked at the SSL example f

SSL communication with client certificate using Xamarin Android

Nitha Paul I'm creating an enterprise application where an organization's MDM solution will install a client certificate on that device. I had to use the following code to read the same but no connection to the server was established. if (x is X509Certificate[

SSL communication with client certificate using Xamarin Android

Nitha Paul I'm creating an enterprise application where an organization's MDM solution will install a client certificate on that device. I had to use the following code to read the same but no connection to the server was established. if (x is X509Certificate[

SSL communication with client certificate using Xamarin Android

Nitha Paul I'm creating an enterprise application where an organization's MDM solution will install a client certificate on that device. I had to use the following code to read the same but no connection to the server was established. if (x is X509Certificate[

SSL communication with client certificate using Xamarin Android

Nitha Paul I'm creating an enterprise application where an organization's MDM solution will install a client certificate on that device. I had to use the following code to read the same but no connection to the server was established. if (x is X509Certificate[