Class: NemID::Authentication::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/nemid/authentication/response.rb

Constant Summary collapse

PID_REGEX =
/\APID:([0-9-]+)\Z/.freeze
RID_REGEX =
/\ARID:([0-9-]+)\Z/.freeze

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ Response

Returns a new instance of Response.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/nemid/authentication/response.rb', line 7

def initialize(string)
  if string.match?(/\A[A-Za-z0-9+\/\r\n]+={0,2}\z/)
    decoded_string = Base64.decode64(string)
    if decoded_string.start_with? '<?xml'
      @doc = NemID::XMLDSig::Document.new(decoded_string)
    else
      raise error(decoded_string)
    end
  elsif string.start_with? '<?xml'
    @doc = NemID::XMLDSig::Document.new(string)
  else
    raise NemID::Errors::ResponseError
  end
end

Instance Method Details

#extract_pidObject



22
23
24
25
26
# File 'lib/nemid/authentication/response.rb', line 22

def extract_pid
  if has_pid?
    serial_number.match(PID_REGEX)[1]
  end
end

#extract_pid_or_ridObject



34
35
36
# File 'lib/nemid/authentication/response.rb', line 34

def extract_pid_or_rid
  serial_number
end

#extract_ridObject



28
29
30
31
32
# File 'lib/nemid/authentication/response.rb', line 28

def extract_rid
  if has_rid?
    serial_number.match(RID_REGEX)[1]
  end
end

#has_pid?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/nemid/authentication/response.rb', line 38

def has_pid?
  serial_number.match?(PID_REGEX)
end

#has_rid?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/nemid/authentication/response.rb', line 42

def has_rid?
  serial_number.match?(RID_REGEX)
end

#user_certificate_expired?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/nemid/authentication/response.rb', line 46

def user_certificate_expired?
  @doc.user_certificate_expired?
end

#user_certificate_revoked?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/nemid/authentication/response.rb', line 50

def user_certificate_revoked?
  @doc.user_certificate_revoked?
end

#valid_certificate_chain?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/nemid/authentication/response.rb', line 54

def valid_certificate_chain?
  @doc.validate_certificate_chain
end

#valid_signature?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/nemid/authentication/response.rb', line 67

def valid_signature?
  @doc.validate_signature
end

#validate_responseObject



58
59
60
61
62
63
64
65
# File 'lib/nemid/authentication/response.rb', line 58

def validate_response
  raise NemID::Errors::InvalidSignature if not valid_signature?
  raise NemID::Errors::InvalidCertificateChain if not valid_certificate_chain?
  raise NemID::Errors::UserCertificateExpired if user_certificate_expired?
  raise NemID::Errors::UserCertificateRevoked if user_certificate_revoked?

  true
end