Class: WSS4R::Security::Xml::UsernameToken

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

Constant Summary collapse

PLAIN =
"PLAIN"
HASHED =
"HASHED"

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from SecurityToken

#add_namespace

Constructor Details

#initialize(username = nil, password = nil, type = HASHED) ⇒ UsernameToken

Returns a new instance of UsernameToken.



95
96
97
98
99
# File 'lib/wss4r/security/xml/tokentypes.rb', line 95

def initialize(username = nil, password = nil, type = HASHED)
  @username = username
  @password = password
  @type = type
end

Instance Attribute Details

#createdObject

Returns the value of attribute created.



93
94
95
# File 'lib/wss4r/security/xml/tokentypes.rb', line 93

def created
  @created
end

#hashObject

Returns the value of attribute hash.



93
94
95
# File 'lib/wss4r/security/xml/tokentypes.rb', line 93

def hash
  @hash
end

#nonceObject

Returns the value of attribute nonce.



93
94
95
# File 'lib/wss4r/security/xml/tokentypes.rb', line 93

def nonce
  @nonce
end

#passwordObject

Returns the value of attribute password.



93
94
95
# File 'lib/wss4r/security/xml/tokentypes.rb', line 93

def password
  @password
end

#typeObject

Returns the value of attribute type.



93
94
95
# File 'lib/wss4r/security/xml/tokentypes.rb', line 93

def type
  @type
end

#usernameObject

Returns the value of attribute username.



93
94
95
# File 'lib/wss4r/security/xml/tokentypes.rb', line 93

def username
  @username
end

Instance Method Details

#process(document) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/wss4r/security/xml/tokentypes.rb', line 115

def process(document)
  wsse_security = XPath.first(document, "/env:Envelope/env:Header/wsse:Security")
  username_token = wsse_security.add_element("wsse:UsernameToken")
  username_token.add_namespace("xmlns:wsu", Namespaces::WSU)
  username_token.add_attribute("wsu:Id", "SecurityToken-" + username_token.object_id().to_s())
  username = username_token.add_element("wsse:Username")
  username.text=(@username)

  if @password.nil?
    # no password provided
  elsif @type == HASHED
    password = username_token.add_element("wsse:Password")
    #BUG #4400
    #password.add_attribute("Type", Types::PASSWORD_DIGEST)
    #Solution--------------------------------------------------
    created = username_token.add_element("wsu:Created")
    created_time = Time.new.getutc()
    #created_time = (Time.new()-(60*60*1)).getutc.iso8601()	
    #----------------------------------------------------------
    created.text=(created_time)

    password.add_attribute("Type", Types::PASSWORD_DIGEST)
    nonce = username_token.add_element("wsse:Nonce")
    nonce_text = OpenSSL::Random.random_bytes(20).to_s().strip()
    nonce.text=(Base64.encode64(nonce_text))
    stamp = nonce_text.to_s() + created_time.to_s() + @password.to_s()
    hash = CryptHash.new().digest_b64(stamp)
    password.text=(hash.to_s())
  else
    password = username_token.add_element("wsse:Password")
    password.add_attribute("Type", Types::PASSWORD_TEXT)
    password.text=@password
  end
		
  # BUG #5877 -----------------------------------------------
  #created_time = (Time.new()-(60*60*1)).iso8601()
  #created_time = created_time[0..created_time.index("+")]
  #created_time[-1]="Z"
  #----------------------------------------------------------
		
end

#unprocess(usernametoken) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/wss4r/security/xml/tokentypes.rb', line 101

def unprocess(usernametoken)
  @username = XPath.first(usernametoken, "wsse:Username", {"wsse"=>Namespaces::WSSE}).text()
  @password = XPath.first(usernametoken, "wsse:Password", {"wsse"=>Namespaces::WSSE}).text()
  password_type = XPath.first(usernametoken, "wsse:Password", {"wsse"=>Namespaces::WSSE}).attribute("Type").value()
  if password_type == Types::PASSWORD_DIGEST
    @type = HASHED
    @nonce    = XPath.first(usernametoken, "wsse:Nonce", {"wsse"=>Namespaces::WSSE}).text()
    @created  = XPath.first(usernametoken, "wsu:Created", {"wsu"=>Namespaces::WSU}).text()
  else 
    @type = PLAIN
  end
  @hash = @password
end