Class: WSS4R::Security::Xml::X509SecurityToken

Inherits:
BinarySecurityToken show all
Defined in:
lib/wss4r/security/xml/tokentypes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from SecurityToken

#add_namespace

Constructor Details

#initialize(x509certificate, private_key = nil) ⇒ X509SecurityToken

Returns a new instance of X509SecurityToken.



18
19
20
21
22
23
24
25
# File 'lib/wss4r/security/xml/tokentypes.rb', line 18

def initialize(x509certificate, private_key = nil)
  if (x509certificate.kind_of?(Certificate))
    @certificate = x509certificate
  elsif x509certificate.instance_of?(String)
    @certificate = Certificate.new(Base64.decode64(x509certificate))
  end
  @private_key = private_key
end

Instance Attribute Details

#certificateObject (readonly)

Returns the value of attribute certificate.



15
16
17
# File 'lib/wss4r/security/xml/tokentypes.rb', line 15

def certificate
  @certificate
end

#private_keyObject

Returns the value of attribute private_key.



16
17
18
# File 'lib/wss4r/security/xml/tokentypes.rb', line 16

def private_key
  @private_key
end

Instance Method Details

#get_idObject



41
42
43
44
45
46
# File 'lib/wss4r/security/xml/tokentypes.rb', line 41

def get_id()
  unless @id
    @id = Crypto::CryptHash.new().digest_b64(@certificate.public_key().to_s()+Time.new().to_s()).to_s().strip()
  end
  @id
end

#get_issuer_nameObject



74
75
76
# File 'lib/wss4r/security/xml/tokentypes.rb', line 74

def get_issuer_name()
  @certificate.issuer()
end

#key_identifierObject



48
49
50
51
52
53
54
55
# File 'lib/wss4r/security/xml/tokentypes.rb', line 48

def key_identifier()
  if (@key_identifier == nil)
    ext = @certificate.extensions()[2]
    return (Base64.encode64(ext.to_der()[11..30]))
  else
    return @key_identifier
  end
end

#key_identifier=(id) ⇒ Object



57
58
59
# File 'lib/wss4r/security/xml/tokentypes.rb', line 57

def key_identifier=(id)
  @key_identifier = id
end

#private_decrypt_b64(text) ⇒ Object



66
67
68
# File 'lib/wss4r/security/xml/tokentypes.rb', line 66

def private_decrypt_b64(text)
  @private_key.private_decrypt(Base64.decode64(text.strip()))
end

#process(document) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/wss4r/security/xml/tokentypes.rb', line 27

def process(document)
  e = Element.new(Names::BINARY_SECURITY_TOKEN)
  e.add_namespace("xmlns:wsu", Namespaces::WSU)
  der_certificate_string = Base64.encode64(@certificate.to_der())
  der_certificate_string.delete!("\n\r")

  e.add_text(der_certificate_string)
  e.add_attribute("wsu:Id", get_id())
		
  e.add_attribute("ValueType", Types::REFERENCE_VALUETYPE_X509)
  e.add_attribute("EncodingType", Types::ENCODING_X509V3)
  return e
end

#public_encrypt_b64(text) ⇒ Object



61
62
63
64
# File 'lib/wss4r/security/xml/tokentypes.rb', line 61

def public_encrypt_b64(text)
  ciphervalue = @certificate.public_key().public_encrypt(text)
  return Base64.encode64(ciphervalue)
end

#public_keyObject



85
86
87
# File 'lib/wss4r/security/xml/tokentypes.rb', line 85

def public_key()
  return @certificate.public_key()
end

#serial_numberObject



70
71
72
# File 'lib/wss4r/security/xml/tokentypes.rb', line 70

def serial_number()
  @certificate.serial()
end

#sign_b64(to_sign) ⇒ Object



78
79
80
81
82
83
# File 'lib/wss4r/security/xml/tokentypes.rb', line 78

def sign_b64(to_sign)
  plain_signature = @private_key.sign(OpenSSL::Digest::SHA1.new(), to_sign)
  signature = Base64.encode64(plain_signature)
  signature.strip!
  signature
end