Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Commit

Permalink
Merge branch '111-qr-code-size'
Browse files Browse the repository at this point in the history
  • Loading branch information
pencil committed May 30, 2015
2 parents 8e229be + 9576ca1 commit 83089cc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
17 changes: 15 additions & 2 deletions app/helpers/casino/two_factor_authenticators_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'rqrcode'
require 'rqrcode_png'

module CASino::TwoFactorAuthenticatorsHelper
Expand All @@ -6,7 +7,19 @@ def otp_auth_url(two_factor_authenticator)
end

def otp_qr_code_data_url(two_factor_authenticator)
qr = RQRCode::QRCode.new(otp_auth_url(two_factor_authenticator), size: 5, level: :l)
qr.to_img.resize(250,250).to_data_url
auth_url = otp_auth_url(two_factor_authenticator)
size = otp_qr_code_suggested_size(auth_url)
qr = RQRCode::QRCode.new(auth_url, size: size, level: :l)
qr.to_img.resize(250, 250).to_data_url
end

def otp_qr_code_suggested_size(data)
data_bits = data.length * 8
(3..40).each do |size|
metadata_bits = 4 + RQRCode::QRUtil.get_length_in_bits(RQRCode::QRMODE[:mode_8bit_byte], size)
total_data_bits = metadata_bits + data_bits
max_data_bits = RQRCode::QRCode.count_max_data_bits(RQRCode::QRRSBlock.get_rs_blocks(size, 1))
return size if total_data_bits < max_data_bits
end
end
end
13 changes: 13 additions & 0 deletions spec/controllers/two_factor_authenticators_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@
get :new, request_options
response.should render_template(:new)
end

context 'with a really long service name' do
before(:each) do
CASino.config.frontend[:sso_name] = 'Na' * 200
end

render_views

it 'renders the new template' do
get :new, request_options
response.should render_template(:new)
end
end
end

context 'without a ticket-granting ticket' do
Expand Down

0 comments on commit 83089cc

Please sign in to comment.