Class: R509::Subject

Inherits:
Object
  • Object
show all
Defined in:
lib/r509/subject.rb

Overview

The primary subject class. Used for building subject DNs in a sane fashion.

Examples:

subject = R509::Subject.new
subject.CN= "test.test"
subject.organization= "r509 LLC"
subject = R509::Subject.new([['CN','test.test'],['O','r509 LLC']])
subject = R509::Subject.new(:CN => 'test.test', :O => 'r509 LLC')
# you can also use the friendly getter/setters with custom OIDs
R509::OIDMapper.register("1.2.3.4.5.6.7.8","COI","customOID")
subject = R509::Subject.new
subject.COI="test"
# or
subject.customOID="test"
# or
subject.custom_oid="test"

Instance Method Summary (collapse)

Constructor Details

- (Subject) initialize(arg = nil)

A new instance of Subject

Parameters:

  • arg (Array, OpenSSL::X509::Name, R509::Subject, DER, Hash, nil) (defaults to: nil)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/r509/subject.rb', line 24

def initialize(arg=nil)
  if arg.kind_of?(Array)
    @array = arg
  elsif arg.kind_of?(Hash)
    @array = arg.map { |k,v| [k.to_s.upcase,v] }
  elsif arg.kind_of?(OpenSSL::X509::Name)
    sanitizer = R509::NameSanitizer.new
    @array = sanitizer.sanitize(arg)
  elsif arg.kind_of?(R509::Subject)
    @array = arg.to_a
  else
    @array = []
    if not (begin OpenSSL::ASN1.decode(arg) rescue nil end).nil?
      parse_asn1(arg)
    end
  end

  # see if X509 thinks this is okay
  name
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(method_sym, *args, &block) (private)

Try to build methods for getting/setting various subject attributes dynamically. this will also cache methods that get built via instance_eval. This code will also allow you to set subject items for custom oids defined via R509::OIDMapper

Examples:

subject = R509::Subject.new
subject.CN = 'test' # method built via method missing.


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/r509/subject.rb', line 140

def method_missing(method_sym, *args, &block)
  if method_sym.to_s =~ /(.*)=$/
    sn = oid_check($1)
    if not sn.nil?
      define_dynamic_setter(method_sym,sn)
      send(method_sym, args.first)
    else
      return super(method_sym, *args, &block)
    end
  else
    sn = oid_check(method_sym)
    if not sn.nil?
      define_dynamic_getter(method_sym,sn)
      send(method_sym)
    else
      return super(method_sym, *args, &block)
    end
  end
end

Instance Method Details

- (Object) [](key)

get value for key



56
57
58
59
60
61
62
63
# File 'lib/r509/subject.rb', line 56

def [](key)
  @array.each do |item|
    if key == item[0]
      return item[1]
    end
  end
  return nil
end

- (Object) []=(key, value)

set key and value



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/r509/subject.rb', line 66

def []=(key, value)
  added = false
  @array = @array.map{ |item|
    if key == item[0]
      added = true
      [key, value]
    else
      item
    end
  }

  if not added
    @array << [key, value]
  end

  # see if X509 thinks this is okay
  name

  @array
end

- (Object) delete(key)

Parameters:

  • key (String)

    item you want deleted



88
89
90
91
92
# File 'lib/r509/subject.rb', line 88

def delete(key)
  @array = @array.select do |item|
    item[0] != key
  end
end

- (Boolean) empty?

Returns:

  • (Boolean)


51
52
53
# File 'lib/r509/subject.rb', line 51

def empty?
  @array.empty?
end

- (OpenSSL::X509::Name) name

Returns:

  • (OpenSSL::X509::Name)


46
47
48
# File 'lib/r509/subject.rb', line 46

def name
  OpenSSL::X509::Name.new(@array)
end

- (Array) to_a

Array of form [['CN','langui.sh']]

Returns:

  • (Array)

    Array of form [['CN','langui.sh']]



100
101
102
# File 'lib/r509/subject.rb', line 100

def to_a
  @array
end

- (Hash) to_h

Returns:

  • (Hash)


105
106
107
108
109
110
111
# File 'lib/r509/subject.rb', line 105

def to_h
  hash = {}
  @array.each do |el|
    hash[el[0].to_sym] = el[1]
  end
  hash
end

- (String) to_s

String of form /CN=something.com/O=whatever/L=Locality

Returns:

  • (String)

    string of form /CN=something.com/O=whatever/L=Locality



95
96
97
# File 'lib/r509/subject.rb', line 95

def to_s
  name.to_s
end

- (YAML) to_yaml

Returns:

  • (YAML)


114
115
116
# File 'lib/r509/subject.rb', line 114

def to_yaml
  self.to_h.to_yaml
end