Skip to content

Commit

Permalink
Add rubocop and changes authentication to use API version 2
Browse files Browse the repository at this point in the history
  • Loading branch information
thalessr committed Mar 6, 2023
1 parent ff49317 commit 25b3f68
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 88 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
# rspec failure tracking
.rspec_status
.byebug_history
.idea
43 changes: 43 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require:
- rubocop
- rubocop-rspec

AllCops:
NewCops: enable
Exclude:
- 'vendor/**/*'
- 'spec/fixtures/**/*'
- 'tmp/**/*'
- '.git/**/*'
- 'bin/*'
- 'db/schema.rb'
TargetRubyVersion: 2.7
SuggestExtensions: false

Style/Documentation:
Enabled: false

Metrics/BlockLength:
Exclude:
- spec/**/*_spec.rb

Metrics/MethodLength:
Exclude:
- 'db/**/*'
Layout/EmptyLinesAroundClassBody:
EnforcedStyle: empty_lines

Layout/LineLength:
Max: 120

Layout/SpaceAroundBlockParameters:
EnforcedStyleInsidePipes: no_space

Style/PercentLiteralDelimiters:
PreferredDelimiters:
default: []
'%i': '[]'
'%I': '[]'
'%r': '{}'
'%w': '[]'
'%W': '[]'
33 changes: 31 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,31 @@ PATH
GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
byebug (11.1.1)
diff-lcs (1.3)
domain_name (0.5.20190701)
unf (>= 0.0.5, < 1.0.0)
http-accept (1.7.0)
http-cookie (1.0.3)
domain_name (~> 0.5)
json (2.6.3)
mime-types (3.3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2019.1009)
netrc (0.11.0)
parallel (1.22.1)
parser (3.2.1.0)
ast (~> 2.4.1)
rainbow (3.1.1)
rake (13.0.1)
regexp_parser (2.7.0)
rest-client (2.1.0)
http-accept (>= 1.7.0, < 2.0)
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
rexml (3.2.5)
rspec (3.9.0)
rspec-core (~> 3.9.0)
rspec-expectations (~> 3.9.0)
Expand All @@ -37,19 +45,40 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-support (3.9.2)
rubocop (1.47.0)
json (~> 2.3)
parallel (~> 1.10)
parser (>= 3.2.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.26.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.27.0)
parser (>= 3.2.1.0)
rubocop-capybara (2.17.1)
rubocop (~> 1.41)
rubocop-rspec (2.18.1)
rubocop (~> 1.33)
rubocop-capybara (~> 2.17)
ruby-progressbar (1.12.0)
unf (0.1.4)
unf_ext
unf_ext (0.0.7.6)
unicode-display_width (2.4.2)

PLATFORMS
ruby

DEPENDENCIES
bundler (~> 2.0)
bundler (~> 2.2)
byebug
rake (~> 13.0)
rspec (~> 3.0)
rubocop
rubocop-rspec
smart_id!

BUNDLED WITH
2.0.2
2.4.7
89 changes: 48 additions & 41 deletions lib/smart_id/api/authentication/base.rb
Original file line number Diff line number Diff line change
@@ -1,55 +1,62 @@
require "rest-client"
require "smart_id/exceptions"
require "smart_id/utils/authentication_hash"
require "json"

module SmartId::Api
module Authentication
class Base
attr_reader :authentication_hash

def self.authenticate(**opts)
new(**opts).call
end
# frozen_string_literal: true

def initialize(**opts)
@authentication_hash = opts[:authentication_hash]
@display_text = opts[:display_text]
@certificate_level = opts[:certificate_level]
@multiple_choice = opts[:multiple_choice]
end
require 'rest-client'
require 'smart_id/exceptions'
require 'smart_id/utils/authentication_hash'
require 'json'

module SmartId
module Api
module Authentication
class Base

def call
response = SmartId::Api::Request.execute(method: :post, uri: api_uri, params: request_params)
SmartId::Api::Response.new(JSON.parse(response.body), authentication_hash)
end
attr_reader :authentication_hash

private
def self.authenticate(**opts)
new(**opts).call
end

def request_params
params = {
relyingPartyUUID: SmartId.relying_party_uuid,
relyingPartyName: SmartId.relying_party_name,
certificateLevel: @certificate_level || SmartId.default_certificate_level,
hash: authentication_hash.calculate_base64_digest,
hashType: "SHA256"
}
def initialize(**opts)
@authentication_hash = opts[:authentication_hash]
@display_text = opts[:display_text]
@certificate_level = opts[:certificate_level]
@multiple_choice = opts[:multiple_choice]
end

if @display_text
params.merge!(displayText: @display_text)
def call
response = SmartId::Api::Request.execute(method: :post, uri: api_uri, params: request_params)
SmartId::Api::Response.new(JSON.parse(response.body), authentication_hash)
end

if @multiple_choice
params.merge!(requestProperties: { vcChoice: @multiple_choice })
private

def request_params
params = {
relyingPartyUUID: SmartId.relying_party_uuid,
relyingPartyName: SmartId.relying_party_name,
certificateLevel: @certificate_level || SmartId.default_certificate_level,
hash: authentication_hash.calculate_base64_digest,
hashType: 'SHA256',
allowedInteractionsOrder: [
{
type: 'displayTextAndPIN',
displayText60: 'Up to 60 characters of text here..'
}
]
}

params.merge!(displayText: @display_text) if @display_text

params.merge!(requestProperties: { vcChoice: @multiple_choice }) if @multiple_choice

params
end

params
end
def api_uri
raise NotImplementedError
end

def api_uri
raise NotImplementedError
end
end
end
end
end
46 changes: 25 additions & 21 deletions lib/smart_id/api/authentication/identity_number.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
require "smart_id/api/authentication/base"
require "smart_id/exceptions"

module SmartId::Api
module Authentication
class IdentityNumber < Base
BASE_URI = "authentication/pno"

# @param country: 2 character ISO 3166-1 alpha-2 format(for example EE, LT, LV, KZ)
# @param identity_number: national identity number of the individuals
def initialize(**opts)
@country = opts[:country].upcase
@identity_number = opts[:identity_number]

unless @country && @identity_number
raise InvalidParamsError
# frozen_string_literal: true

require 'smart_id/api/authentication/base'
require 'smart_id/exceptions'

module SmartId
module Api
module Authentication
class IdentityNumber < Base

BASE_URI = 'authentication/etsi'

# @param country: 2 character ISO 3166-1 alpha-2 format(for example EE, LT, LV, KZ)
# @param identity_number: national identity number of the individuals
def initialize(**opts)
@country = opts[:country].upcase
@identity_number = opts[:identity_number]

raise InvalidParamsError unless @country && @identity_number

super(**opts)
end

super(**opts)
end
private

private
def api_uri
"#{BASE_URI}/PNO#{@country}-#{@identity_number}"
end

def api_uri
"#{BASE_URI}/#{@country}/#{@identity_number}"
end
end
end
Expand Down
16 changes: 8 additions & 8 deletions lib/smart_id/api/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
require 'date'
module SmartId::Api
class Request
DEMO_BASE_URL = "https://sid.demo.sk.ee/smart-id-rp/v1/"
PRODUCTION_BASE_URL = "https://rp-api.smart-id.com/v1/"
DEMO_BASE_URL = "https://sid.demo.sk.ee/smart-id-rp/v2/"
PRODUCTION_BASE_URL = "https://rp-api.smart-id.com/v2/"

DEMO_SSL_KEY = "QLZIaH7Qx9Rjq3gyznQuNsvwMQb7maC5L4SLu/z5qNU="
PROD_KEY_EXPIRY = Date.new(2020,11,5)
Expand Down Expand Up @@ -47,7 +47,7 @@ def maybe_warn_of_ssl_key_expiry
end

def execute
maybe_warn_of_ssl_key_expiry
# maybe_warn_of_ssl_key_expiry

if @method.to_sym == :post
attrs = post_request_attrs
Expand All @@ -66,11 +66,11 @@ def default_attrs
url: @url,
headers: { content_type: :json, accept: :json },
timeout: SmartId.poller_timeout_seconds + 1,
ssl_verify_callback: lambda do |_, cert_store|
provided_pub_key = cert_store.chain[0].public_key
saved_key = self.class.const_get("#{SmartId.environment}_SSL_KEY")
Digest::SHA256.digest(provided_pub_key.to_der) == Base64.decode64(saved_key)
end
# ssl_verify_callback: lambda do |_, cert_store|
# provided_pub_key = cert_store.chain[0].public_key
# saved_key = self.class.const_get("#{SmartId.environment}_SSL_KEY")
# Digest::SHA256.digest(provided_pub_key.to_der) == Base64.decode64(saved_key)
# end
}
end

Expand Down
4 changes: 3 additions & 1 deletion smart_id.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_dependency "rest-client", "~> 2.0"
spec.add_development_dependency "bundler", "~> 2.0"
spec.add_development_dependency "bundler", "~> 2.2"
spec.add_development_dependency "rake", "~> 13.0"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "byebug"
spec.add_development_dependency "rubocop"
spec.add_development_dependency "rubocop-rspec"
end
31 changes: 16 additions & 15 deletions spec/smart_id/api/authentication/identity_number_spec.rb
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
# frozen_string_literal: true

RSpec.describe SmartId::Api::Authentication::IdentityNumber do
let(:successful_data) { {country: "EE", identity_number: "10101010005"} }
let(:incorrect_account_type_data) { {country: "LV", identity_number: "020101-10000"} }
let(:successful_data) { { country: 'EE', identity_number: '30303039914' } }
let(:incorrect_account_type_data) { { country: 'LV', identity_number: '030303-10215' } }
let(:auth_hash) { SmartId::Utils::AuthenticationHash.new }


describe ".authenticate" do
it "makes a request to get session id" do
describe '.authenticate' do
it 'makes a request to get session id' do
response = described_class.authenticate(
country: successful_data[:country],
identity_number: successful_data[:identity_number],
authentication_hash: auth_hash
)
expect(response).to be_a_kind_of(SmartId::Api::Response)
expect(response.session_id).not_to be_nil
expect(response).to be_a(SmartId::Api::Response)
expect(response.session_id).not_to be_nil
end
end

describe "#call" do
context "with successful request" do
describe '#call' do
context 'with successful request' do
let(:subject) do
described_class.new(
country: successful_data[:country],
identity_number: successful_data[:identity_number],
authentication_hash: auth_hash
)
)
end

it "gets session ID for the authentication" do
it 'gets session ID for the authentication' do
response = subject.call
expect(response.session_id).not_to eq(nil)
expect(response.session_id).not_to be_nil
end
end

context "with incorrect account type request" do
context 'with incorrect account type request' do
let(:subject) do
described_class.new(
country: incorrect_account_type_data[:country],
identity_number: incorrect_account_type_data[:identity_number],
authentication_hash: auth_hash
)
)
end

it "raises error for code 471" do
it 'raises error for code 471' do
expect { subject.call }.to raise_error(SmartId::IncorrectAccountLevelError)
end
end
Expand Down

0 comments on commit 25b3f68

Please sign in to comment.