Skip to content

Commit

Permalink
Generare QR Codes as SVG (#97)
Browse files Browse the repository at this point in the history
* generate provisioning URL via rqrcode, and remove old JS and Google QR Code generation methods;

* remove QRCode.js from assets, and remove assets/javascripts folder in general (no longer needed);

* use path option for SVG to reduce size (will be default in newer versions of rqrcode);

* update README for switch to SVG QR Codes via rqrcode, and remove references to Sprockets and JavaScript (no longer needed);

* update SVG to use "viewbox" to allow setting max-width via CSS; remove additional containing element (no longer needed);

* add default styles for Devise OTP to apply max width and centering to QR Code; update README and test app for change;
  • Loading branch information
strouptl authored Oct 20, 2024
1 parent 180b190 commit 1dd2876
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 648 deletions.
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ Don't forget to migrate:

rake db:migrate

Add the gem's JavaScript to you `application.js`:
### Default CSS

//= require devise-otp
To use the default CSS for devise-otp, just require the devise-otp.css file as usual in your application.css file (or equivalent):

*= require devise-otp

### Custom views

Expand All @@ -77,9 +78,7 @@ The install generator also installs an english copy of a Devise OTP i18n file. T

### QR codes

By default, Devise OTP assumes that you use [Sprockets](https://github.com/rails/sprockets) to render assets and so will use the ([qrcode.js](/app/assets/javascripts/qrcode.js)) embeded library to render the QR code.

If you need something more, have a look at [QR codes](/docs/QR_CODES.md) documentation file.
Devise OTP generates QR Codes directly as SVG's via the [rqrcode](https://github.com/whomwah/rqrcode), so there are no JavaScript (or Sprockets) dependencies.

## Configuration

Expand Down
1 change: 0 additions & 1 deletion app/assets/javascripts/devise-otp.js

This file was deleted.

609 changes: 0 additions & 609 deletions app/assets/javascripts/qrcode.js

This file was deleted.

4 changes: 4 additions & 0 deletions app/assets/stylesheets/devise-otp.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.qrcode-container {
max-width: 300px;
margin: 0 auto;
}
1 change: 1 addition & 0 deletions devise-otp.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ Gem::Specification.new do |gem|
gem.add_dependency "rails", ">= 7.0", "< 8.0"
gem.add_dependency "devise", ">= 4.8.0", "< 5.0"
gem.add_dependency "rotp", ">= 2.0.0"
gem.add_dependency "rqrcode", "~> 2.0"
end
9 changes: 0 additions & 9 deletions docs/QR_CODES.md

This file was deleted.

26 changes: 3 additions & 23 deletions lib/devise_otp_authenticatable/controllers/helpers.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "rqrcode"

module DeviseOtpAuthenticatable
module Controllers
module Helpers
Expand Down Expand Up @@ -119,33 +121,11 @@ def otp_reset_persistence_for(resource)
# returns the URL for the QR Code to initialize the Authenticator device
#
def otp_authenticator_token_image(resource)
otp_authenticator_token_image_js(resource.otp_provisioning_uri)
end

private

def otp_authenticator_token_image_js(otp_url)
content_tag(:div, class: "qrcode-container") do
content_tag(:div, id: "qrcode", class: "qrcode") do
javascript_tag(%[
new QRCode("qrcode", {
text: "#{otp_url}",
width: 256,
height: 256,
colorDark : "#000000",
colorLight : "#ffffff",
correctLevel : QRCode.CorrectLevel.H
});
])
end
raw RQRCode::QRCode.new(resource.otp_provisioning_uri).as_svg(:module_size => 5, :viewbox => true, :use_path => true)
end
end

def otp_authenticator_token_image_google(otp_url)
otp_url = Rack::Utils.escape(otp_url)
url = "https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl=#{otp_url}"
image_tag(url, alt: "OTP Url QRCode")
end
end
end
end
1 change: 0 additions & 1 deletion test/dummy/app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@
// GO AFTER THE REQUIRES BELOW.
//
//= require_tree .
//= require devise-otp
1 change: 1 addition & 0 deletions test/dummy/app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* You're free to add application-wide styles to this file and they'll appear at the top of the
* compiled file, but it's generally better to create a new file per style scope.
*
*= require devise-otp
*= require_self
*= require_tree .
*/

0 comments on commit 1dd2876

Please sign in to comment.