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

Print QR code for webauthn verification link #7272

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .codespellrc
Expand Up @@ -2,4 +2,4 @@
check-filenames=
check-hidden=
ignore-words=.codespellignore
skip=*.pem,.git,man,vcr_cassettes,vendor,./lib/rubygems/net*,./lib/rubygems/resolv,./lib/rubygems/tsort,./lib/rubygems/timeout,./lib/rubygems/optparse
skip=*.pem,.git,man,vcr_cassettes,vendor,./lib/rubygems/net*,./lib/rubygems/resolv,./lib/rubygems/tsort,./lib/rubygems/timeout,./lib/rubygems/optparse,./lib/rubygems/rqrcode_core
11 changes: 6 additions & 5 deletions .rubocop.yml
Expand Up @@ -14,6 +14,7 @@ AllCops:
- lib/rubygems/optparse/**/*
- lib/rubygems/net-protocol/**/*
- lib/rubygems/net-http/**/*
- lib/rubygems/rqrcode_core/**/*
- bundler/lib/bundler/vendor/**/*
CacheRootDirectory: tmp/rubocop
MaxFilesInCache: 5000
Expand Down Expand Up @@ -241,8 +242,8 @@ Lint/SafeNavigationConsistency:
Lint/ScriptPermission:
Enabled: true
Exclude:
- 'bundler/lib/bundler/templates/Executable'
- 'bundler/lib/bundler/templates/Executable.*'
- "bundler/lib/bundler/templates/Executable"
- "bundler/lib/bundler/templates/Executable.*"

Lint/ShadowedArgument:
Enabled: true
Expand Down Expand Up @@ -406,8 +407,8 @@ Layout/SpaceInsideBlockBraces:
Enabled: true
SpaceBeforeBlockParameters: false
Exclude:
- bundler/lib/bundler/templates/Gemfile
- bundler/lib/bundler/templates/gems.rb
- bundler/lib/bundler/templates/Gemfile
- bundler/lib/bundler/templates/gems.rb

Layout/SpaceInsideHashLiteralBraces:
Enabled: true
Expand Down Expand Up @@ -494,7 +495,7 @@ Naming/ClassAndModuleCamelCase:
Naming/FileName:
Enabled: true
Exclude:
- 'bundler/spec/realworld/fixtures/warbler/bin/warbler-example.rb'
- "bundler/spec/realworld/fixtures/warbler/bin/warbler-example.rb"

Naming/MemoizedInstanceVariableName:
Enabled: true
Expand Down
16 changes: 16 additions & 0 deletions Manifest.txt
Expand Up @@ -542,6 +542,22 @@ lib/rubygems/resolver/specification.rb
lib/rubygems/resolver/stats.rb
lib/rubygems/resolver/vendor_set.rb
lib/rubygems/resolver/vendor_specification.rb
lib/rubygems/rqrcode_core/.document
lib/rubygems/rqrcode_core/LICENSE.txt
lib/rubygems/rqrcode_core/lib/rqrcode_core.rb
lib/rubygems/rqrcode_core/lib/rqrcode_core/qrcode.rb
lib/rubygems/rqrcode_core/lib/rqrcode_core/qrcode/qr_8bit_byte.rb
lib/rubygems/rqrcode_core/lib/rqrcode_core/qrcode/qr_alphanumeric.rb
lib/rubygems/rqrcode_core/lib/rqrcode_core/qrcode/qr_bit_buffer.rb
lib/rubygems/rqrcode_core/lib/rqrcode_core/qrcode/qr_code.rb
lib/rubygems/rqrcode_core/lib/rqrcode_core/qrcode/qr_math.rb
lib/rubygems/rqrcode_core/lib/rqrcode_core/qrcode/qr_multi.rb
lib/rubygems/rqrcode_core/lib/rqrcode_core/qrcode/qr_numeric.rb
lib/rubygems/rqrcode_core/lib/rqrcode_core/qrcode/qr_polynomial.rb
lib/rubygems/rqrcode_core/lib/rqrcode_core/qrcode/qr_rs_block.rb
lib/rubygems/rqrcode_core/lib/rqrcode_core/qrcode/qr_segment.rb
lib/rubygems/rqrcode_core/lib/rqrcode_core/qrcode/qr_util.rb
lib/rubygems/rqrcode_core/lib/rqrcode_core/version.rb
lib/rubygems/s3_uri_signer.rb
lib/rubygems/safe_marshal.rb
lib/rubygems/safe_marshal/elements.rb
Expand Down
2 changes: 2 additions & 0 deletions lib/rubygems/gemcutter_utilities.rb
Expand Up @@ -2,6 +2,7 @@

require_relative "remote_fetcher"
require_relative "text"
require_relative "rqrcode_core/lib/rqrcode_core"
require_relative "gemcutter_utilities/webauthn_listener"
require_relative "gemcutter_utilities/webauthn_poller"

Expand Down Expand Up @@ -259,6 +260,7 @@ def fetch_otp(credentials)

url_with_port = "#{webauthn_url}?port=#{port}"
say "You have enabled multi-factor authentication. Please visit #{url_with_port} to authenticate via security device. If you can't verify using WebAuthn but have OTP enabled, you can re-run the gem signin command with the `--otp [your_code]` option."
qrcode Gem::RQRCodeCore::QRCode.new(url_with_port)

threads = [WebauthnListener.listener_thread(host, server), WebauthnPoller.poll_thread(options, host, webauthn_url, credentials)]
otp_thread = wait_for_otp_thread(*threads)
Expand Down
1 change: 1 addition & 0 deletions lib/rubygems/rqrcode_core/.document
@@ -0,0 +1 @@
# Vendored files do not need to be documented
21 changes: 21 additions & 0 deletions lib/rubygems/rqrcode_core/LICENSE.txt
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2008 Duncan Robertson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
6 changes: 6 additions & 0 deletions lib/rubygems/rqrcode_core/lib/rqrcode_core.rb
@@ -0,0 +1,6 @@
# frozen_string_literal: true

module Gem::RQRCodeCore
require "rubygems/rqrcode_core/lib/rqrcode_core/qrcode"
require "rubygems/rqrcode_core/lib/rqrcode_core/version"
end
13 changes: 13 additions & 0 deletions lib/rubygems/rqrcode_core/lib/rqrcode_core/qrcode.rb
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require_relative "./qrcode/qr_8bit_byte"
require_relative "./qrcode/qr_alphanumeric"
require_relative "./qrcode/qr_bit_buffer"
require_relative "./qrcode/qr_code"
require_relative "./qrcode/qr_math"
require_relative "./qrcode/qr_multi"
require_relative "./qrcode/qr_numeric"
require_relative "./qrcode/qr_polynomial"
require_relative "./qrcode/qr_rs_block"
require_relative "./qrcode/qr_segment"
require_relative "./qrcode/qr_util"
17 changes: 17 additions & 0 deletions lib/rubygems/rqrcode_core/lib/rqrcode_core/qrcode/qr_8bit_byte.rb
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module Gem::RQRCodeCore
class QR8bitByte
def initialize(data)
@data = data
end

def write(buffer)
buffer.byte_encoding_start(@data.bytesize)

@data.each_byte do |b|
buffer.put(b, 8)
end
end
end
end
@@ -0,0 +1,39 @@
# frozen_string_literal: true

module Gem::RQRCodeCore
ALPHANUMERIC = [
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
"J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", " ", "$",
"%", "*", "+", "-", ".", "/", ":"
].freeze

class QRAlphanumeric
def initialize(data)
unless QRAlphanumeric.valid_data?(data)
raise QRCodeArgumentError, "Not a alpha numeric uppercase string `#{data}`"
end

@data = data
end

def self.valid_data?(data)
(data.chars - ALPHANUMERIC).empty?
end

def write(buffer)
buffer.alphanumeric_encoding_start(@data.size)

@data.size.times do |i|
if i % 2 == 0
if i == (@data.size - 1)
value = ALPHANUMERIC.index(@data[i])
buffer.put(value, 6)
else
value = (ALPHANUMERIC.index(@data[i]) * 45) + ALPHANUMERIC.index(@data[i + 1])
buffer.put(value, 11)
end
end
end
end
end
end
76 changes: 76 additions & 0 deletions lib/rubygems/rqrcode_core/lib/rqrcode_core/qrcode/qr_bit_buffer.rb
@@ -0,0 +1,76 @@
# frozen_string_literal: true

module Gem::RQRCodeCore
class QRBitBuffer
attr_reader :buffer

PAD0 = 0xEC
PAD1 = 0x11

def initialize(version)
@version = version
@buffer = []
@length = 0
end

def get(index)
buf_index = (index / 8).floor
((QRUtil.rszf(@buffer[buf_index], 7 - index % 8)) & 1) == 1
end

def put(num, length)
(0...length).each do |i|
put_bit(((QRUtil.rszf(num, length - i - 1)) & 1) == 1)
end
end

def get_length_in_bits
@length
end

def put_bit(bit)
buf_index = (@length / 8).floor
if @buffer.size <= buf_index
@buffer << 0
end

if bit
@buffer[buf_index] |= QRUtil.rszf(0x80, @length % 8)
end

@length += 1
end

def byte_encoding_start(length)
put(QRMODE[:mode_8bit_byte], 4)
put(length, QRUtil.get_length_in_bits(QRMODE[:mode_8bit_byte], @version))
end

def alphanumeric_encoding_start(length)
put(QRMODE[:mode_alpha_numk], 4)
put(length, QRUtil.get_length_in_bits(QRMODE[:mode_alpha_numk], @version))
end

def numeric_encoding_start(length)
put(QRMODE[:mode_number], 4)
put(length, QRUtil.get_length_in_bits(QRMODE[:mode_number], @version))
end

def pad_until(prefered_size)
# Align on byte
while get_length_in_bits % 8 != 0
put_bit(false)
end

# Pad with padding code words
while get_length_in_bits < prefered_size
put(PAD0, 8)
put(PAD1, 8) if get_length_in_bits < prefered_size
end
end

def end_of_message(max_data_bits)
put(0, 4) unless get_length_in_bits + 4 > max_data_bits
end
end
end