From a314d1265bcf7b0c6dd66d61d03e1d2a7545cfb8 Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 25 Feb 2013 12:35:00 +0100 Subject: enable free certs in the controller --- certs/app/controllers/certs_controller.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'certs/app') diff --git a/certs/app/controllers/certs_controller.rb b/certs/app/controllers/certs_controller.rb index 6db270c..6099ac0 100644 --- a/certs/app/controllers/certs_controller.rb +++ b/certs/app/controllers/certs_controller.rb @@ -1,10 +1,8 @@ class CertsController < ApplicationController - before_filter :authorize - # GET /cert def show - @cert = ClientCertificate.new + @cert = ClientCertificate.new(free: !logged_in?) render :text => @cert.key + @cert.cert, :content_type => 'text/plain' end -- cgit v1.2.3 From d99bcf4b0d0b8716ab0da58ea7320fb33bac78bb Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 25 Feb 2013 13:01:07 +0100 Subject: enable free certs with a common name postfix --- certs/app/controllers/certs_controller.rb | 2 +- certs/app/models/client_certificate.rb | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) (limited to 'certs/app') diff --git a/certs/app/controllers/certs_controller.rb b/certs/app/controllers/certs_controller.rb index 6099ac0..3b7d35d 100644 --- a/certs/app/controllers/certs_controller.rb +++ b/certs/app/controllers/certs_controller.rb @@ -3,7 +3,7 @@ class CertsController < ApplicationController # GET /cert def show @cert = ClientCertificate.new(free: !logged_in?) - render :text => @cert.key + @cert.cert, :content_type => 'text/plain' + render text: @cert.to_s, content_type: 'text/plain' end end diff --git a/certs/app/models/client_certificate.rb b/certs/app/models/client_certificate.rb index be0ac63..3a82d1a 100644 --- a/certs/app/models/client_certificate.rb +++ b/certs/app/models/client_certificate.rb @@ -1,5 +1,5 @@ # -# Model for certificates stored in CouchDB. +# Model for certificates # # This file must be loaded after Config has been loaded. # @@ -17,11 +17,11 @@ class ClientCertificate # # generate the private key and client certificate # - def initialize + def initialize(options = {}) cert = CertificateAuthority::Certificate.new # set subject - cert.subject.common_name = random_common_name + cert.subject.common_name = common_name(options[:free]) # set expiration cert.not_before = yesterday @@ -35,8 +35,12 @@ class ClientCertificate cert.parent = ClientCertificate.root_ca cert.sign! client_signing_profile - self.key = cert.key_material.private_key.to_pem - self.cert = cert.to_pem + self.key = cert.key_material.private_key + self.cert = cert + end + + def to_s + self.key.to_pem + self.cert.to_pem end private @@ -61,6 +65,14 @@ class ClientCertificate Digest::MD5.hexdigest("#{rand(10**10)} -- #{Time.now}").to_i(16) end + def common_name(for_free_cert = false) + if for_free_cert + random_common_name + ' ' + APP_CONFIG[:free_cert_postfix] + else + random_common_name + end + end + # # for the random common name, we need a text string that will be unique across all certs. # ruby 1.8 doesn't have a built-in uuid generator, or we would use SecureRandom.uuid -- cgit v1.2.3 From 494ebdab860a4db792e1c61836f1efcb7593dfe7 Mon Sep 17 00:00:00 2001 From: Azul Date: Mon, 25 Feb 2013 13:15:50 +0100 Subject: added configuration setting for disabling free certs --- certs/app/controllers/certs_controller.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'certs/app') diff --git a/certs/app/controllers/certs_controller.rb b/certs/app/controllers/certs_controller.rb index 3b7d35d..977e03e 100644 --- a/certs/app/controllers/certs_controller.rb +++ b/certs/app/controllers/certs_controller.rb @@ -1,9 +1,16 @@ class CertsController < ApplicationController + before_filter :logged_in_or_free_certs + # GET /cert def show @cert = ClientCertificate.new(free: !logged_in?) render text: @cert.to_s, content_type: 'text/plain' end + protected + + def logged_in_or_free_certs + authorize unless APP_CONFIG[:free_certs_enabled] + end end -- cgit v1.2.3 From 2eafc17ea68e75e6b040b6c6677e5eebd3371f0e Mon Sep 17 00:00:00 2001 From: Azul Date: Tue, 26 Feb 2013 12:01:56 +0100 Subject: minor: using ?: operator for cert postfix --- certs/app/models/client_certificate.rb | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'certs/app') diff --git a/certs/app/models/client_certificate.rb b/certs/app/models/client_certificate.rb index 3a82d1a..1bc34c6 100644 --- a/certs/app/models/client_certificate.rb +++ b/certs/app/models/client_certificate.rb @@ -66,11 +66,8 @@ class ClientCertificate end def common_name(for_free_cert = false) - if for_free_cert - random_common_name + ' ' + APP_CONFIG[:free_cert_postfix] - else - random_common_name - end + random_common_name + + (for_free_cert ? APP_CONFIG[:free_cert_postfix] : '') end # -- cgit v1.2.3