Class: AadhaarAuth::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/aadhaar_auth/client.rb

Defined Under Namespace

Classes: ResponseError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(person_data) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/aadhaar_auth/client.rb', line 11

def initialize(person_data)
  @aadhaar_no = person_data[:aadhaar_no].to_s
  @name = person_data[:name]
  @email = person_data[:email]
  @phone = person_data[:phone]
  @gender = person_data[:gender]
  @time = Time.now
  @encrypter = Encrypter.new
  @digital_signer = DigitalSigner.new
  @raw_response = nil
end

Instance Attribute Details

#aadhaar_noObject (readonly)

Returns the value of attribute aadhaar_no.



8
9
10
# File 'lib/aadhaar_auth/client.rb', line 8

def aadhaar_no
  @aadhaar_no
end

#digital_signerObject (readonly)

Returns the value of attribute digital_signer.



8
9
10
# File 'lib/aadhaar_auth/client.rb', line 8

def digital_signer
  @digital_signer
end

#emailObject (readonly)

Returns the value of attribute email.



8
9
10
# File 'lib/aadhaar_auth/client.rb', line 8

def email
  @email
end

#encrypterObject (readonly)

Returns the value of attribute encrypter.



8
9
10
# File 'lib/aadhaar_auth/client.rb', line 8

def encrypter
  @encrypter
end

#error_codeObject (readonly)

Returns the value of attribute error_code.



8
9
10
# File 'lib/aadhaar_auth/client.rb', line 8

def error_code
  @error_code
end

#genderObject (readonly)

Returns the value of attribute gender.



8
9
10
# File 'lib/aadhaar_auth/client.rb', line 8

def gender
  @gender
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/aadhaar_auth/client.rb', line 8

def name
  @name
end

#phoneObject (readonly)

Returns the value of attribute phone.



8
9
10
# File 'lib/aadhaar_auth/client.rb', line 8

def phone
  @phone
end

#raw_responseObject (readonly)

Returns the value of attribute raw_response.



8
9
10
# File 'lib/aadhaar_auth/client.rb', line 8

def raw_response
  @raw_response
end

#timeObject (readonly)

Returns the value of attribute time.



8
9
10
# File 'lib/aadhaar_auth/client.rb', line 8

def time
  @time
end

#verboseObject

Returns the value of attribute verbose.



7
8
9
# File 'lib/aadhaar_auth/client.rb', line 7

def verbose
  @verbose
end

Instance Method Details

#encrypted_dataObject



123
124
125
126
127
# File 'lib/aadhaar_auth/client.rb', line 123

def encrypted_data
  @encrypted_data ||= begin
    Base64.encode64(encrypter.encrypt_using_session_key(pid_block))
  end
end

#pid_blockObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/aadhaar_auth/client.rb', line 105

def pid_block
  @pid_block ||= begin
    xml = Nokogiri::XML('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>')
    Nokogiri::XML::Builder.with(xml) do |x|
      x.Pid('ts' => time.strftime("%Y-%m-%dT%H:%M:%S"), 'xmlns:ns2' => 'http://www.uidai.gov.in/authentication/uid-auth-request-data/1.0') do
        x.parent.namespace = x.parent.namespace_definitions.find{|ns| ns.prefix == "ns2"}
        x.Demo do
          info = {'ms' => "E", 'mv' => "100", 'name' => name}
          info.merge!('gender' => gender) if gender
          info.merge!('phone' => phone) if phone
          info.merge!('email' => email) if email
          x.Pi(info)
        end
      end
    end.to_xml
  end
end

#raw_requestObject



55
56
57
# File 'lib/aadhaar_auth/client.rb', line 55

def raw_request
  @raw_request ||= digital_signer.sign(req_xml)
end

#req_xmlObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/aadhaar_auth/client.rb', line 59

def req_xml
  nok = Nokogiri::XML::Builder.new(:encoding => 'UTF-8') do |x|
    x.Auth(
            'uid' => aadhaar_no,
            'ac' => Config.ac,
            'lk' => Config.lk,
            'sa'=> Config.sa,
            'tid'=> Config.tid,
            'txn'=>"AuthDemoClient:public:#{time.to_i}",
            'ver'=> Config.api_version,
            'xmlns:ds'=>"http://www.w3.org/2000/09/xmldsig#",
            'xmlns'=>"http://www.uidai.gov.in/authentication/uid-auth-request/1.0") do
      x.Uses('bio'=>'n', 'otp'=>"n", 'pa'=>"n", 'pfa'=>"n", 'pi'=>"y", 'pin'=>"n")
      x.Meta('fdc'=>"NA", 'idc'=>"NA", 'lot'=>"P", 'lov' => "560094", 'pip' => "NA", 'udc'=> Config.udc)
      x.Skey('ci'=> skey_ci) do
        x.text(encrypter.encrypted_session_key)
      end
      x.Data('type' => "X") do
        x.text(encrypted_data)
      end
      x.Hmac(encrypter.calculate_hmac(pid_block))
      x['ds'].Signature do
        x['ds'].SignedInfo do
          x['ds'].CanonicalizationMethod('Algorithm' => "http://www.w3.org/2001/10/xml-exc-c14n#")
          x['ds'].SignatureMethod('Algorithm' => "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256")
          x['ds'].Reference('URI' => "") do
            x['ds'].Transforms do
              x['ds'].Transform('Algorithm'=>"http://www.w3.org/2000/09/xmldsig#enveloped-signature")
            end
            x['ds'].DigestMethod('Algorithm'=>"http://www.w3.org/2001/04/xmlenc#sha256")
            x['ds'].DigestValue('')
          end
        end
        x['ds'].SignatureValue('')
        x['ds'].KeyInfo do
          x['ds'].X509Data do
            x['ds'].X509SubjectName(DigitalSigner.private_key_cert.subject.to_s)
            x['ds'].X509Certificate(DigitalSigner.private_key_cert_val)
          end
        end
      end
    end
  end
  nok.to_xml
end

#skey_ciObject



129
130
131
# File 'lib/aadhaar_auth/client.rb', line 129

def skey_ci
  encrypter.public_cert.not_after.strftime('%Y%m%d')
end

#urlObject



51
52
53
# File 'lib/aadhaar_auth/client.rb', line 51

def url
  @url ||= url = "http://auth.uidai.gov.in/#{Config.api_version}/public/#{aadhaar_no[0]}/#{aadhaar_no[1]}/#{Config.asa_licence_key}"
end

#valid?Boolean

Returns:

  • (Boolean)

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/aadhaar_auth/client.rb', line 23

def valid?
  # should be exactly 12 digits
  if aadhaar_no !~ /^\d{12}$/
    return(false)
  end

  @raw_response = Curl::Easy.http_post(url, raw_request).body_str

  if verbose
    puts "URL: \n#{url}"
    puts "PID XML: \n#{pid_block()}"
    puts "Signed request: \n#{raw_request}"
    puts "Response: \n#{@raw_response}"
  end

  digital_signer.verify_signature(@raw_response) if Config.verify_response_signature

  auth_res = Nokogiri::XML(@raw_response).children.find{|c| c.name == 'AuthRes'}
  @error_code = auth_res.attributes['err'] ? auth_res.attributes['err'].value : nil

  ret = auth_res.attributes['ret'] ? auth_res.attributes['ret'].value : nil
  if ret && ret != ''
    return ret == 'y'
  end

  raise ResponseError.new(["Error :#{@error_code}", pid_block, raw_request, @raw_response].join("\n\n"))
end