class MobileId::Auth

Constants

LIVE_URL

API documentation github.com/SK-EID/MID

TEST_NAME
TEST_URL
TEST_UUID

Attributes

doc[RW]
hash[RW]
live[RW]
name[RW]
url[RW]
user_cert[RW]
uuid[RW]

Public Class Methods

new(live:, uuid: nil, name: nil) click to toggle source
# File lib/mobile_id/auth.rb, line 14
def initialize(live:, uuid: nil, name: nil)
  self.url = live == true ? LIVE_URL : TEST_URL
  self.uuid = live == true ? uuid : TEST_UUID
  self.name = live == true ? name : TEST_NAME
  self.live = live
  init_doc(SecureRandom.hex(40))
end

Public Instance Methods

authenticate!(phone_calling_code: nil, phone:, personal_code:, language: nil, display_text: nil) click to toggle source
# File lib/mobile_id/auth.rb, line 27
def authenticate!(phone_calling_code: nil, phone:, personal_code:, language: nil, display_text: nil)
  phone_calling_code ||= '+372'
  full_phone = "#{phone_calling_code}#{phone}"
  language ||= 
    case I18n.locale
    when :et
      display_text ||= 'Autentimine' 
      'EST'
    when :ru
      display_text ||= 'Аутентификация' 
      'RUS'
    else
      display_text ||= 'Authentication' 
      'ENG'
    end
  
  options = {
    headers: {
      "Content-Type": "application/json"
    },
    query: {},
    body: {
      relyingPartyUUID: uuid,
      relyingPartyName: name,
      phoneNumber: full_phone.to_s.strip,
      nationalIdentityNumber: personal_code.to_s.strip,
      hash: Base64.strict_encode64(hash),
      hashType: 'SHA256',
      language: language,
      displayText: display_text,
      displayTextFormat: 'GSM-7' # or "UCS-2”
    }.to_json
  }

  response = HTTParty.post(url + '/authentication', options)
  raise Error, "#{I18n.t('mobile_id.some_error')} #{response}" unless response.code == 200

  ActiveSupport::HashWithIndifferentAccess.new(
    session_id: response['sessionID'],
    phone: phone,
    phone_calling_code: phone_calling_code,
    doc: doc
  )
end
common_name() click to toggle source
# File lib/mobile_id/auth.rb, line 147
def common_name
  user_cert.common_name
end
country() click to toggle source
# File lib/mobile_id/auth.rb, line 143
def country
  user_cert.country
end
first_name()
Alias for: given_name
given_name() click to toggle source
# File lib/mobile_id/auth.rb, line 133
def given_name
  user_cert.given_name
end
Also aliased as: first_name
init_doc(doc) click to toggle source
# File lib/mobile_id/auth.rb, line 22
def init_doc(doc)
  self.doc = doc
  self.hash = Digest::SHA256.digest(doc)
end
last_name()
Alias for: surname
long_poll!(session_id:, doc:) click to toggle source
# File lib/mobile_id/auth.rb, line 91
def long_poll!(session_id:, doc:)
  response = nil

  # Retries until RUNNING state turns to COMPLETE
  30.times do |i|
    response = session_request(session_id)
    break if response['state'] == 'COMPLETE'
    sleep 1
  end
  raise Error, "#{I18n.t('mobile_id.some_error')} #{response.code} #{response}" if response['state'] != 'COMPLETE'

  if response['result'] != 'OK'
    message = 
      case response['result']
      when "TIMEOUT"
        I18n.t('mobile_id.timeout')
      when "NOT_MID_CLIENT"
        I18n.t('mobile_id.user_is_not_mobile_id_client')
      when "USER_CANCELLED"
        I18n.t('mobile_id.user_cancelled')
      when "SIGNATURE_HASH_MISMATCH"
        I18n.t('mobile_id.signature_hash_mismatch')
      when "PHONE_ABSENT"
        I18n.t('mobile_id.phone_absent')
      when "DELIVERY_ERROR"
        I18n.t('mobile_id.delivery_error')
      when "SIM_ERROR"
        I18n.t('mobile_id.sim_error')
      end
      raise Error, message
  end

  @user_cert = MobileId::Cert.new(response['cert'], live: live)
  @user_cert.verify_signature!(response['signature']['value'], doc)
  self.user_cert = @user_cert
end
organizational_unit() click to toggle source
# File lib/mobile_id/auth.rb, line 151
def organizational_unit
  user_cert.organizational_unit
end
personal_code()
Alias for: serial_number
serial_number() click to toggle source
# File lib/mobile_id/auth.rb, line 155
def serial_number
  user_cert.serial_number
end
Also aliased as: personal_code
session_request(session_id) click to toggle source
# File lib/mobile_id/auth.rb, line 85
def session_request(session_id)
  response = HTTParty.get(url + "/authentication/session/#{session_id}")
  raise Error, "#{I18n.t('mobile_id.some_error')} #{response.code} #{response}" if response.code != 200
  response
end
surname() click to toggle source
# File lib/mobile_id/auth.rb, line 138
def surname
  user_cert.surname
end
Also aliased as: last_name
verification_code() click to toggle source
# File lib/mobile_id/auth.rb, line 128
def verification_code
  binary = hash.to_s.unpack('B*').first
  "%04d" % (binary[0...6] + binary[-7..-1]).to_i(2)
end
verify!(auth) click to toggle source
# File lib/mobile_id/auth.rb, line 72
def verify!(auth)
  long_poll!(session_id: auth['session_id'], doc: auth['doc'])

  ActiveSupport::HashWithIndifferentAccess.new(
    personal_code: personal_code,
    first_name: first_name,
    last_name: last_name,
    phone: auth['phone'],
    phone_calling_code: auth['phone_calling_code'],
    auth_provider: 'mobileid' # User::MOBILEID
  )
end