Skip to content
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

Generare QR Codes as SVG #97

Merged
merged 6 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 .
*/