-
Notifications
You must be signed in to change notification settings - Fork 166
Fix downloading personal keys outside of IDV (LG-5882) #6161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ const formEl = document.getElementById('confirm-key'); | |
| const input = formEl.querySelector('input[type="text"]'); | ||
| const modalTrigger = document.querySelector('[data-toggle="modal"]'); | ||
| const modalDismiss = document.querySelector('[data-dismiss="personal-key-confirm"]'); | ||
| const downloadLink = document.querySelector('a[download]'); | ||
|
|
||
| let isInvalidForm = false; | ||
|
|
||
|
|
@@ -114,6 +115,20 @@ function hide() { | |
| modal.hide(); | ||
| } | ||
|
|
||
| function downloadForIE(event) { | ||
| event.preventDefault(); | ||
|
|
||
| const filename = downloadLink.getAttribute('download'); | ||
| const data = scrapePersonalKey(); | ||
| const blob = new Blob([data], { type: 'text/plain' }); | ||
|
|
||
| window.navigator.msSaveBlob(blob, filename); | ||
| } | ||
|
|
||
| modalTrigger.addEventListener('click', show); | ||
| modalDismiss.addEventListener('click', hide); | ||
| formEl.addEventListener('submit', handleSubmit); | ||
|
|
||
| if (window.navigator.msSaveBlob) { | ||
| downloadLink.addEventListener('click', downloadForIE); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was not able to test this myself, but @mitchellhenke said in slack the download worked |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,12 @@ | |
| </div> | ||
| <%= render ButtonComponent.new( | ||
| action: ->(**tag_options, &block) do | ||
| link_to(idv_download_personal_key_path, **tag_options, &block) | ||
| link_to( | ||
| "data:text/plain;charset=utf-8,#{CGI.escape(code)}", | ||
| download: 'personal_key.txt', | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. one thought is maybe we can translate this filename? not that urgent IMO
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think fine for a follow-up, yeah |
||
| **tag_options, | ||
| &block | ||
| ) | ||
| end, | ||
| icon: :file_download, | ||
| outline: true, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -276,6 +276,7 @@ | |
| get '/come_back_later' => 'come_back_later#show' | ||
| get '/personal_key' => 'personal_key#show' | ||
| post '/personal_key' => 'personal_key#update' | ||
| # Remove this after the next deploy | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. another thought --- I think we can convert the backup codes download functionality that does something similar
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't quite follow?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do the same "download" stuff in another controller and I think if we link this client-only approach, we should consider it there, too: https://github.com/18F/identity-idp/blob/main/app/controllers/users/backup_code_setup_controller.rb#L31-L34
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ohhh got it 👍🏼 |
||
| get '/download_personal_key' => 'personal_key#download' | ||
| get '/forgot_password' => 'forgot_password#new' | ||
| post '/forgot_password' => 'forgot_password#update' | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| require 'rails_helper' | ||
| require 'data_uri' | ||
|
|
||
| RSpec.describe 'shared/_personal_key.html.erb' do | ||
| let(:personal_key) { RandomPhrase.new(num_words: 4).to_s } | ||
|
|
||
| describe 'download link' do | ||
| around do |ex| | ||
| # data_uri depends on URI.decode which was removed in Ruby 3.0 :sob: | ||
| module URI | ||
| def self.decode(value) | ||
| CGI.unescape(value) | ||
| end | ||
| end | ||
|
|
||
| ex.run | ||
|
|
||
| URI.singleton_class.undef_method(:decode) | ||
| end | ||
|
|
||
| it 'has the download attribute and a data: url for the personal key' do | ||
| render 'shared/personal_key', code: personal_key, update_path: '/test' | ||
|
|
||
| doc = Nokogiri::HTML(rendered) | ||
| download_link = doc.at_css('a[download]') | ||
| data_uri = URI::Data.new(download_link[:href]) | ||
|
|
||
| expect(data_uri.content_type).to eq('text/plain') | ||
| expect(data_uri.data).to eq(personal_key) | ||
| end | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍🏼