summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--certs/app/controllers/certs_controller.rb5
-rw-r--r--certs/app/models/client_certificate.rb28
-rw-r--r--certs/test/functional/certs_controller_test.rb2
-rw-r--r--certs/test/unit/client_certificate_test.rb18
4 files changed, 11 insertions, 42 deletions
diff --git a/certs/app/controllers/certs_controller.rb b/certs/app/controllers/certs_controller.rb
index 3ec2f68..6db270c 100644
--- a/certs/app/controllers/certs_controller.rb
+++ b/certs/app/controllers/certs_controller.rb
@@ -4,11 +4,8 @@ class CertsController < ApplicationController
# GET /cert
def show
- @cert = ClientCertificate.create
+ @cert = ClientCertificate.new
render :text => @cert.key + @cert.cert, :content_type => 'text/plain'
- rescue RECORD_NOT_FOUND
- flash[:error] = t(:cert_pool_empty)
- redirect_to root_path
end
end
diff --git a/certs/app/models/client_certificate.rb b/certs/app/models/client_certificate.rb
index b664ff0..b2b8c0d 100644
--- a/certs/app/models/client_certificate.rb
+++ b/certs/app/models/client_certificate.rb
@@ -9,41 +9,23 @@ require 'openssl'
require 'certificate_authority'
require 'date'
-class ClientCertificate < CouchRest::Model::Base
+class ClientCertificate
- timestamps!
-
- property :key, String # the client private RSA key
- property :cert, String # the client x509 certificate, signed by the CA
- property :valid_until, Time # expiration time of the client certificate
-
- before_validation :generate, :on => :create
-
- validates :key, :presence => true
- validates :cert, :presence => true
-
- design do
- end
-
- class << self
- def valid_attributes_hash
- {:key => "ABCD", :cert => "A123"}
- end
- end
+ attr_accessor :key # the client private RSA key
+ attr_accessor :cert # the client x509 certificate, signed by the CA
#
# generate the private key and client certificate
#
- def generate
+ def initialize
cert = CertificateAuthority::Certificate.new
# set subject
cert.subject.common_name = random_common_name
# set expiration
- self.valid_until = months_from_yesterday(APP_CONFIG[:client_cert_lifespan])
cert.not_before = yesterday
- cert.not_after = self.valid_until
+ cert.not_after = months_from_yesterday(APP_CONFIG[:client_cert_lifespan])
# generate key
cert.serial_number.number = cert_serial_number
diff --git a/certs/test/functional/certs_controller_test.rb b/certs/test/functional/certs_controller_test.rb
index 887d5f0..75256ca 100644
--- a/certs/test/functional/certs_controller_test.rb
+++ b/certs/test/functional/certs_controller_test.rb
@@ -13,7 +13,7 @@ class CertsControllerTest < ActionController::TestCase
test "should send cert" do
login
cert = stub :cert => "adsf", :key => "key"
- ClientCertificate.expects(:create).returns(cert)
+ ClientCertificate.expects(:new).returns(cert)
get :show
assert_response :success
assert_equal cert.key + cert.cert, @response.body
diff --git a/certs/test/unit/client_certificate_test.rb b/certs/test/unit/client_certificate_test.rb
index 7dbb8a9..492a44a 100644
--- a/certs/test/unit/client_certificate_test.rb
+++ b/certs/test/unit/client_certificate_test.rb
@@ -3,22 +3,12 @@ require 'test_helper'
class ClientCertificateTest < ActiveSupport::TestCase
setup do
- @sample = ClientCertificate.new ClientCertificate.valid_attributes_hash
+ @sample = ClientCertificate.new
end
- test "stub cert for testing is valid" do
- assert @sample.valid?
+ test "new cert has all we need" do
+ assert @sample.key
+ assert @sample.cert
end
- test "validates key" do
- @sample.key = nil
- assert @sample.valid?
- assert @sample.key, "Cert should generate key"
- end
-
- test "validates cert" do
- @sample.cert = nil
- assert @sample.valid?
- assert @sample.cert, "Cert should generate cert"
- end
end