Skip to content

Commit

Permalink
Improve login button, fetch assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
cizmarty committed Apr 27, 2024
1 parent 7caa7b8 commit e5eb2e8
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/assets/images/eid-sk.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion app/models/eid_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def decoded_token
end

def sub
decoded_token&.first&.fetch('actor')&.fetch('sub')
decoded_token&.first&.fetch('sub')
end

def name
Expand Down
47 changes: 47 additions & 0 deletions app/models/upvs/assertion.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module Upvs
class Assertion
include ActiveModel::Model
attr_accessor(:raw, :subject_name, :subject_id, :subject_ico)

def self.new_from_xml(raw:)
return unless raw

h = Hash.from_xml(raw)
new(
raw:,
subject_name: h.dig('Assertion', 'AttributeStatement', 'Attribute').detect{|e| e['Name'] == 'Subject.FormattedName' }.dig('AttributeValue'),
subject_id: h.dig('Assertion', 'AttributeStatement', 'Attribute').detect{|e| e['Name'] == 'SubjectID' }.dig('AttributeValue'),
subject_ico: h.dig('Assertion', 'AttributeStatement', 'Attribute').detect{|e| e['Name'] == 'Subject.ICO' }.dig('AttributeValue'),
)
end

def self.assertion(eid_token, client: Faraday, url: "#{ENV.fetch('AUTH_EID_BASE_URL')}/api/upvs/assertion?token=#{eid_token&.api_token}")
new_from_xml(raw: get_from_sk_api(client, url, eid_token))
end

def self.get_from_sk_api(client, url, eid_token)
headers = {
"Accept": "application/samlassertion+xml",
"AUTHORIZATION": "Bearer #{eid_token&.api_token}",
}

response = client.get(url, {}, headers)
error = begin
JSON.parse(response.body)
rescue StandardError
nil
end
if error && error['message']
return nil
end
response.body
rescue StandardError => _e
raise
nil
end


class SkApiError < StandardError
end
end
end
3 changes: 0 additions & 3 deletions app/views/components/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
<a href="#" data-turbolinks="false" class="sdn-header__link sdn-header__dropdown-toggle js-dropdown-toggle" aria-controls="subnav-dropdown">
<span class="sdn-header__fixed-width-text">
<%= current_user.email %>
<% if eid_token&.valid? %>
<sup>EID</sup>
<% end %>
</span>
</a>
<ul class="sdn-header__dropdown" id="subnav-dropdown">
Expand Down
2 changes: 1 addition & 1 deletion app/views/sessions/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<% end %>
<%= form_tag(auth_path(:eid), method: :post, class: 'govuk-body govuk-!-margin-bottom-8') do %>
<%= image_submit_tag 'eid-sk.svg', class: 'govuk-link', title: 'Prihlásiť sa cez eID', alt: 'Prihlásiť sa cez eID', style: 'max-width: 300px' %>
<%= image_submit_tag 'eid-sk.svg', class: 'govuk-link', title: 'Prihlásiť sa cez slovensko.sk', alt: 'Prihlásiť sa cez slovensko.sk', style: 'max-width: 300px' %>
<% end %>
<%= form_tag(auth_path(:magiclink), method: :post, id: 'login-email') do %>
Expand Down

0 comments on commit e5eb2e8

Please sign in to comment.