yubico_client 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: dada71b6d0649f3ba3e5f3aca318c03e72f0b96ffbfb1452283b349e12b5d4c3
4
+ data.tar.gz: ea7065608b34786cbb5adffc645a6ff4369814a9a1a26788792256e1b5958c64
5
+ SHA512:
6
+ metadata.gz: 115c7533b00d4a015f5f12195ae83603085e94b7b77d138e28fca8d16f588433e6b336365702c2b3cfd2d8670d474e59e5da68c456cc6513aa8453b329f3b88b
7
+ data.tar.gz: e21510f08078aa11cce328453dcd50fbb76ac025fc71e39f3c3ad6d52e52941feddad0007864ee48a4e266ad4c64790bc2893b1a3a6bb04257b5de5a3a121efb
@@ -0,0 +1,17 @@
1
+ name: RSpec tests
2
+ on: [push]
3
+ jobs:
4
+ test:
5
+ runs-on: ubuntu-latest
6
+ strategy:
7
+ matrix:
8
+ ruby-version: [ 2.6.3, 2.7.2, 3.0.2 ]
9
+ steps:
10
+ - name: Checkout code
11
+ uses: actions/checkout@v2
12
+ - name: Setup Ruby
13
+ uses: ruby/setup-ruby@v1
14
+ with:
15
+ ruby-version: ${{ matrix.ruby-version }}
16
+ - run: bundle install
17
+ - run: bundle exec rspec
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.idea
2
+ /Gemfile.lock
3
+ /.bundle/
4
+ /.yardoc
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+
12
+ # rspec failure tracking
13
+ .rspec_status
data/.gitlab-ci.yml ADDED
@@ -0,0 +1,9 @@
1
+ image: ruby:2.6.3
2
+
3
+ before_script:
4
+ - gem install bundler -v 2.2.16
5
+ - bundle install
6
+
7
+ rspec:
8
+ script:
9
+ - bundle exec rspec
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,16 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ AllCops:
4
+ NewCops: enable
5
+ TargetRubyVersion: 2.6
6
+
7
+ Metrics/BlockLength:
8
+ Enabled: false
9
+
10
+ Style/StringLiterals:
11
+ Enabled: true
12
+ EnforcedStyle: double_quotes
13
+
14
+ Style/StringLiteralsInInterpolation:
15
+ Enabled: true
16
+ EnforcedStyle: double_quotes
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,30 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config --auto-gen-only-exclude`
3
+ # on 2021-09-24 18:09:41 UTC using RuboCop version 1.21.0.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 1
10
+ # Configuration parameters: ExpectMatchingDefinition, CheckDefinitionPathHierarchy, Regex, IgnoreExecutableScripts, AllowedAcronyms.
11
+ # AllowedAcronyms: CLI, DSL, ACL, API, ASCII, CPU, CSS, DNS, EOF, GUID, HTML, HTTP, HTTPS, ID, IP, JSON, LHS, QPS, RAM, RHS, RPC, SLA, SMTP, SQL, SSH, TCP, TLS, TTL, UDP, UI, UID, UUID, URI, URL, UTF8, VM, XML, XMPP, XSRF, XSS
12
+ Naming/FileName:
13
+ Exclude:
14
+ - 'lib/yubico_client/OTP.rb'
15
+
16
+ # Offense count: 1
17
+ # Configuration parameters: AllowedConstants.
18
+ Style/Documentation:
19
+ Exclude:
20
+ - 'spec/**/*'
21
+ - 'test/**/*'
22
+ - 'lib/yubico_client.rb'
23
+
24
+ # Offense count: 1
25
+ # Cop supports --auto-correct.
26
+ # Configuration parameters: Max, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
27
+ # URISchemes: http, https
28
+ Layout/LineLength:
29
+ Exclude:
30
+ - 'lib/yubico_client/OTP.rb'
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-09-24
4
+ ### Added
5
+ - Initial release
6
+ - OTP
7
+ - specs
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at lukas@easy.cz. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in yubico_client.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "rspec", "~> 3.0"
10
+ gem "rubocop", "~> 1.7"
11
+ group :test do
12
+ gem "simplecov", "~> 0.21.2", require: false
13
+ gem "webmock", "~> 3.14"
14
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Lukáš Pokorný
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # YubicoClient
2
+
3
+ Ruby client library for YubiKey.
4
+
5
+ ## OTP
6
+
7
+ Verify Yubico OTPs with YubiCloud. This works as a library for https://developers.yubico.com/OTP/Libraries/Using_a_library.html
8
+
9
+ The most simple way is to use `.valid?` shortcut - it just verify OTP
10
+ ```ruby
11
+ YubicoClient::OTP.valid?("vvvvvvcurikvhjcvnlnbecbkubjvuittbifhndhn", client_id: "12345", secret: "c2VjcmV0Cg=") # => true / false
12
+ ```
13
+
14
+ `client_id` and `secret` is required and should be obtained from https://upgrade.yubico.com/getapikey/
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem 'yubico_client'
22
+ ```
23
+
24
+ And then execute:
25
+
26
+ $ bundle install
27
+
28
+ Or:
29
+
30
+ $ bundle add yubico_client
31
+
32
+ Or install it yourself as:
33
+
34
+ $ gem install yubico_client
35
+
36
+
37
+ ## Development
38
+
39
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
40
+
41
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
42
+
43
+ ## Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/yubico_client. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/luk4s/yubico_client/blob/master/CODE_OF_CONDUCT.md).
46
+
47
+ ## License
48
+
49
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
50
+
51
+ ## Code of Conduct
52
+
53
+ Everyone interacting in the YubicoClient project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/luk4s/yubico_client/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "yubico_client"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "base64"
4
+ require "net/http"
5
+ require "openssl"
6
+
7
+ module YubicoClient
8
+ # Verify OTP against Yubico Cloud
9
+ # @see https://developers.yubico.com/yubikey-val/Getting_Started_Writing_Clients.html
10
+ class OTP
11
+ # @param (see #initialize)
12
+ def self.valid?(*args)
13
+ client = new(*args)
14
+ client.response_data["status"] == "OK" && client.valid?
15
+ end
16
+
17
+ # @param [String] otp
18
+ # @see https://developers.yubico.com/yubikey-val/Getting_Started_Writing_Clients.html#_binding_an_otp_to_an_identity
19
+ # @note ...Since the rest of the OTP is always 32 characters, the method to extract the identity is to remove 32 characters from the end and then use the remaining string, which should be 2-16 characters, as the YubiKey identity
20
+ def self.key_id(otp)
21
+ otp[0, otp.size - 32]
22
+ end
23
+
24
+ # @param [String] otp obtain from Yubikey
25
+ # @param [String] client_id api_key ID for Yubico cloud
26
+ # @param [String] secret see https://upgrade.yubico.com/getapikey/
27
+ def initialize(otp, client_id:, secret:)
28
+ @otp = otp
29
+ @client_id = client_id
30
+ @secret = secret
31
+ @timestamp = Time.now.utc.iso8601
32
+ end
33
+
34
+ def nonce
35
+ @nonce ||= SecureRandom.hex 16
36
+ end
37
+
38
+ def params
39
+ { id: @client_id, otp: @otp, nonce: nonce }
40
+ end
41
+
42
+ def query_params
43
+ @query_params ||= params.map { |k, v| "#{k}=#{v.strip}" }.sort
44
+ end
45
+
46
+ def request_signature
47
+ hmac = OpenSSL::HMAC.digest("sha1", Base64.decode64(@secret), query_params.join("&").strip)
48
+ Base64.encode64(hmac).strip
49
+ end
50
+
51
+ # @see https://developers.yubico.com/yubikey-val/Getting_Started_Writing_Clients.html
52
+ # @return [Hash]
53
+ def response_data
54
+ return @response_data if @response_data
55
+
56
+ uri = URI("https://api.yubico.com/wsapi/2.0/verify")
57
+ uri.query = URI.encode_www_form(params.merge("h" => request_signature))
58
+
59
+ response = Net::HTTP.get_response(uri)
60
+ raise Error, response unless response.is_a?(Net::HTTPSuccess)
61
+
62
+ @response_data = parse_response(response)
63
+ end
64
+
65
+ def valid?
66
+ data = response_data
67
+ signature = data.delete("h")
68
+ response_string = data.map { |k, v| "#{k}=#{v}" }.sort.join("&")
69
+ hmac = OpenSSL::HMAC.digest("sha1", Base64.decode64(@secret.strip), response_string)
70
+ signature == Base64.encode64(hmac).strip
71
+ end
72
+
73
+ private
74
+
75
+ def parse_response(response)
76
+ response.body.split.each_with_object({}) do |i, obj|
77
+ m = i.match(/^(\w+)=(.*)/)
78
+ obj.store(m[1], m[2].strip)
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module YubicoClient
4
+ VERSION = "1.0.0"
5
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+ require_relative "yubico_client/version"
5
+
6
+ module YubicoClient
7
+ autoload :OTP, "yubico_client/OTP"
8
+
9
+ class Error < StandardError; end
10
+ # Your code goes here...
11
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/yubico_client/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "yubico_client"
7
+ spec.version = YubicoClient::VERSION
8
+ spec.authors = ["Lukáš Pokorný"]
9
+ spec.email = ["pokorny@luk4s.cz"]
10
+
11
+ spec.summary = "Ruby client for Yubikey"
12
+ spec.description = "Verify OTP with YubiCloud.\nhttps://developers.yubico.com/OTP/"
13
+ spec.homepage = "https://github.com/luk4s/yubico_client"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = spec.homepage
21
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/CHANGELOG.md"
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
27
+ end
28
+ # spec.bindir = "exe"
29
+ # spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ # For more information and examples about making a new gem, checkout our
33
+ # guide at: https://bundler.io/guides/creating_gem.html
34
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yubico_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Lukáš Pokorný
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-09-24 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |-
14
+ Verify OTP with YubiCloud.
15
+ https://developers.yubico.com/OTP/
16
+ email:
17
+ - pokorny@luk4s.cz
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - ".github/workflows/rspec.yml"
23
+ - ".gitignore"
24
+ - ".gitlab-ci.yml"
25
+ - ".rspec"
26
+ - ".rubocop.yml"
27
+ - ".rubocop_todo.yml"
28
+ - CHANGELOG.md
29
+ - CODE_OF_CONDUCT.md
30
+ - Gemfile
31
+ - LICENSE.txt
32
+ - README.md
33
+ - Rakefile
34
+ - bin/console
35
+ - bin/setup
36
+ - lib/yubico_client.rb
37
+ - lib/yubico_client/OTP.rb
38
+ - lib/yubico_client/version.rb
39
+ - yubico_client.gemspec
40
+ homepage: https://github.com/luk4s/yubico_client
41
+ licenses:
42
+ - MIT
43
+ metadata:
44
+ allowed_push_host: https://rubygems.org
45
+ homepage_uri: https://github.com/luk4s/yubico_client
46
+ source_code_uri: https://github.com/luk4s/yubico_client
47
+ changelog_uri: https://github.com/luk4s/yubico_client/CHANGELOG.md
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 2.6.0
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubygems_version: 3.1.4
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: Ruby client for Yubikey
67
+ test_files: []