Class: Wsse::UsernameToken

Inherits:
Object
  • Object
show all
Defined in:
lib/wsse/username_token.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, digest, nonce, created) ⇒ UsernameToken

Returns a new instance of UsernameToken.



8
9
10
11
12
13
# File 'lib/wsse/username_token.rb', line 8

def initialize(username, digest, nonce, created)
  @username = username
  @digest   = digest  # binary
  @nonce    = nonce   # binary
  @created  = created # Time object
end

Instance Attribute Details

#createdObject (readonly)

Returns the value of attribute created.



15
16
17
# File 'lib/wsse/username_token.rb', line 15

def created
  @created
end

#digestObject (readonly)

Returns the value of attribute digest.



15
16
17
# File 'lib/wsse/username_token.rb', line 15

def digest
  @digest
end

#nonceObject (readonly)

Returns the value of attribute nonce.



15
16
17
# File 'lib/wsse/username_token.rb', line 15

def nonce
  @nonce
end

#usernameObject (readonly)

Returns the value of attribute username.



15
16
17
# File 'lib/wsse/username_token.rb', line 15

def username
  @username
end

Class Method Details

.build(username, password, nonce = nil, created = nil) ⇒ Object



25
26
27
28
29
30
# File 'lib/wsse/username_token.rb', line 25

def self.build(username, password, nonce = nil, created = nil)
  nonce   ||= self.create_random_binary(20)
  created ||= Time.now.utc
  digest = self.create_password_digest(password, nonce, created)
  return self.new(username, digest, nonce, created)
end

.create_password_digest(password, nonce, created) ⇒ Object



21
22
23
# File 'lib/wsse/username_token.rb', line 21

def self.create_password_digest(password, nonce, created)
  return Digest::SHA1.digest(nonce + created.utc.iso8601 + password)
end

.create_random_binary(size) ⇒ Object



17
18
19
# File 'lib/wsse/username_token.rb', line 17

def self.create_random_binary(size)
  return size.times.map { rand(256) }.pack("C*")
end

.format_token_values(username, digest, nonce, created) ⇒ Object



51
52
53
54
55
# File 'lib/wsse/username_token.rb', line 51

def self.format_token_values(username, digest, nonce, created)
  return format(
    %|UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"|,
    username, digest, nonce, created)
end

.parse(token) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/wsse/username_token.rb', line 57

def self.parse(token)
  parsed_token = self.parse_token(token)
  return nil unless parsed_token

  username = parsed_token["Username"]
  digest   = parsed_token["PasswordDigest"]
  nonce    = parsed_token["Nonce"]
  created  = parsed_token["Created"]
  return nil if [username, digest, nonce, created].include?(nil)

  return self.new(
    username,
    digest.unpack("m")[0],
    nonce.unpack("m")[0],
    self.parse_time(created))
end

.parse_time(value) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/wsse/username_token.rb', line 43

def self.parse_time(value)
  if /\A(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)Z\z/ =~ value.to_s
    return Time.utc(*($~.captures.map { |s| s.to_i }))
  else
    return nil
  end
end

.parse_token(token) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/wsse/username_token.rb', line 32

def self.parse_token(token)
  if /\AUsernameToken (.+?=".+?"(?:, .+?=".+?")*)\z/ =~ token.to_s
    return $1.scan(/(?:\A|, )(.+?)="(.+?)"/).inject({}) { |memo, (key, value)|
      memo[key] = value
      memo
    }
  else
    return nil
  end
end

Instance Method Details

#base64encoded_digestObject



74
75
76
# File 'lib/wsse/username_token.rb', line 74

def base64encoded_digest
  return [@digest].pack("m").chomp
end

#base64encoded_nonceObject



78
79
80
# File 'lib/wsse/username_token.rb', line 78

def base64encoded_nonce
  return [@nonce].pack("m").chomp
end

#formatObject



82
83
84
85
86
87
88
# File 'lib/wsse/username_token.rb', line 82

def format
  return self.class.format_token_values(
    self.username,
    self.base64encoded_digest,
    self.base64encoded_nonce,
    self.created.utc.iso8601)
end