Class: R509::Middleware::Validity

Inherits:
Object
  • Object
show all
Includes:
Dependo::Mixin
Defined in:
lib/r509/middleware/validity.rb,
lib/r509/middleware/validity/version.rb

Constant Summary collapse

VERSION =
"0.2"

Instance Method Summary collapse

Constructor Details

#initialize(app, redis = nil) ⇒ Validity

Returns a new instance of Validity.



10
11
12
13
14
15
16
17
# File 'lib/r509/middleware/validity.rb', line 10

def initialize(app,redis=nil)
    @app = app

    if redis.nil?
        redis = Redis.new
    end
    @writer = R509::Validity::Redis::Writer.new(redis)
end

Instance Method Details

#call(env) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/r509/middleware/validity.rb', line 19

def call(env)
    status, headers, response = @app.call(env)

    # we only want to attempt to record validity if status is 200 and it's a call
    # to the "/1/certificate/issue" path
    if not (env["PATH_INFO"] =~ /^\/1\/certificate\/issue\/?$/).nil? and status == 200
        body = ""
        response.each do |part|
            body += part
        end
        begin
            cert = R509::Cert.new(:cert => body)
            log.info "Writing serial: #{cert.serial.to_s}, Issuer: #{cert.issuer.to_s}"
            @writer.issue(cert.issuer.to_s,cert.serial.to_s)
        rescue => e
            log.error "Writing failed"
            log.error e.inspect
        end
    elsif not (env["PATH_INFO"] =~ /^\/1\/certificate\/revoke\/?$/).nil? and status == 200
        begin
            params = parse_params(env)

            issuer = @app.config_pool[params["ca"]].ca_cert.subject.to_s
            serial = params["serial"]
            reason = params["reason"].to_i || 0

            log.info "Revoking serial: #{serial}, reason: #{reason}"

            @writer.revoke(issuer, serial, Time.now.to_i, reason)
        rescue => e
            log.error "Revoking failed: #{serial}"
            log.error e.inspect
        end
    elsif not (env["PATH_INFO"] =~ /^\/1\/certificate\/unrevoke\/?$/).nil? and status == 200
        begin
            params = parse_params(env)

            issuer = @app.config_pool[params["ca"]].ca_cert.subject.to_s
            serial = params["serial"]

            log.info "Unrevoking serial: #{serial}"

            @writer.unrevoke(issuer, serial)
        rescue => e
            log.error "Unrevoking failed: #{serial}"
            log.error e.inspect
        end
    end

    [status, headers, response]
end