oa-yubikey 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile CHANGED
@@ -1,4 +1,3 @@
1
- source "http://rubygems.org"
1
+ source :rubygems
2
2
 
3
- # Specify your gem's dependencies in oa-yubikey.gemspec
4
3
  gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2011 by Steve Hoeksema.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,20 @@
1
+ OmniAuth strategy for authenticating against the Yubico API with a Yubikey OTP.
2
+
3
+ To Do
4
+ -----
5
+
6
+ * More RSpec coverage
7
+ * Verify the hash returned by Yubico
8
+
9
+
10
+ Contains portions of code from:
11
+
12
+ ruby-yubico
13
+ by Jenecai 'Seven' Corvina <seven@ofhearts.org>
14
+ http://code.google.com/p/ruby-yubico/
15
+ BSD license
16
+
17
+ yubikey
18
+ by Jonathan Rudenberg <jon335@gmail.com>
19
+ https://github.com/titanous/yubikey
20
+ MIT license
@@ -0,0 +1,24 @@
1
+ require "rubygems"
2
+ require "sinatra"
3
+ require "oa-yubikey"
4
+
5
+ use Rack::Session::Cookie
6
+
7
+ use OmniAuth::Strategies::Yubikey, "XXXX", "XXXXXXXXXXXXXXXXXXXXXXXXX"
8
+
9
+ enable :sessions
10
+
11
+ get "/" do
12
+ <<-HTML
13
+ <form action="/auth/yubikey" method="post">
14
+ <input type="text" name="otp" />
15
+ <input type="submit" value="Sign in with Yubikey" />
16
+ </form>
17
+ HTML
18
+ end
19
+
20
+ get "/auth/:name/callback" do
21
+ auth = request.env["omniauth.auth"]
22
+
23
+ auth.inspect
24
+ end
data/lib/oa-yubikey.rb CHANGED
@@ -1,7 +1,10 @@
1
- require "oa-yubikey/version"
1
+ require "omniauth/yubikey/version"
2
+ require "omniauth/yubikey/verifier"
3
+ require "omniauth/yubikey/result"
4
+ require "omniauth/yubikey/otp_error"
5
+ require "omniauth/strategies/yubikey"
2
6
 
3
- module Oa
7
+ module OmniAuth
4
8
  module Yubikey
5
- # Your code goes here...
6
9
  end
7
10
  end
@@ -0,0 +1,81 @@
1
+ require "omniauth/strategy"
2
+
3
+ module OmniAuth
4
+ module Strategies
5
+ class Yubikey
6
+
7
+ include OmniAuth::Strategy
8
+
9
+ attr_accessor :options, :api_id, :api_key
10
+
11
+ attr_reader :otp_id
12
+
13
+ def initialize(app, api_id = nil, api_key = nil, options = {}, &block)
14
+ self.api_id = api_id
15
+ self.api_key = api_key
16
+
17
+ super(app, :yubikey, options, &block)
18
+ end
19
+
20
+ def request_phase
21
+ if env["REQUEST_METHOD"] == "GET"
22
+ get_credentials
23
+ else
24
+ perform
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def title
31
+ name.to_s.split("_").map{ |s| s.capitalize }.join(" ")
32
+ end
33
+
34
+ def get_credentials
35
+ OmniAuth::Form.build(:title => title) do
36
+ password_field "One-time password", "otp"
37
+ end.to_response
38
+ end
39
+
40
+ def otp
41
+ request["otp"]
42
+ end
43
+
44
+ def api_url
45
+ options[:api_url] if options
46
+ end
47
+
48
+ def perform
49
+ verifier = OmniAuth::Yubikey::Verifier.new(api_id, api_key, api_url)
50
+ result = verifier.verify!(otp)
51
+
52
+ @otp_id = result.id
53
+
54
+ @env["omniauth.auth"] = auth_hash
55
+ @env["omniauth.yubikey"] = result
56
+ @env["REQUEST_METHOD"] = "GET"
57
+ @env["PATH_INFO"] = "#{OmniAuth.config.path_prefix}/#{name.to_s}/callback"
58
+
59
+ call_app!
60
+ rescue OmniAuth::Yubikey::OtpError => e
61
+ fail!(:invalid_credentials, e)
62
+ end
63
+
64
+ def callback_phase
65
+ fail!(:invalid_credentials)
66
+ end
67
+
68
+ def auth_hash
69
+ OmniAuth::Utils.deep_merge(
70
+ super, {
71
+ "uid" => otp_id,
72
+ "user_info" => {
73
+ "name" => otp_id,
74
+ },
75
+ }
76
+ )
77
+ end
78
+
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,6 @@
1
+ module OmniAuth
2
+ module Yubikey
3
+ class OtpError < StandardError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,34 @@
1
+ require "time"
2
+
3
+ module OmniAuth
4
+ module Yubikey
5
+ class Result
6
+
7
+ attr_reader :otp, :id, :hash, :timestamp, :status, :info
8
+
9
+ def initialize(otp, response)
10
+ @otp = otp
11
+ @id = otp[0, otp.length-32] if otp.length > 32
12
+
13
+ response.gsub!("\r\n", "\n")
14
+
15
+ hashes = response.scan(/^h=([\w_\=\/\+]+)$/)[0]
16
+ statuses = response.scan(/^status=([a-zA-Z0-9_]+)$/)[0]
17
+ timestamps = response.scan(/^t=(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z(\d{4})$/)[0]
18
+
19
+ @hash = hashes[0] if hashes && hashes[0]
20
+ @status = statuses[0] if statuses && statuses[0]
21
+
22
+ if timestamps
23
+ parts = timestamps.map(&:to_i)
24
+ @timestamp = Time.utc(*parts)
25
+ end
26
+ end
27
+
28
+ def valid?
29
+ status == "OK"
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,47 @@
1
+ require "net/http"
2
+ require "openssl"
3
+
4
+ module OmniAuth
5
+ module Yubikey
6
+ class Verifier
7
+
8
+ DEFAULT_API_URL = "https://api.yubico.com/wsapi/"
9
+
10
+ def initialize(api_id, api_key, api_url = DEFAULT_API_URL)
11
+ @api_id, @api_key = api_id, api_key
12
+ @api_url = api_url || DEFAULT_API_URL
13
+ end
14
+
15
+ def verify(otp)
16
+ response = get_response(otp)
17
+ Result.new(otp, response)
18
+ end
19
+
20
+ def verify!(otp)
21
+ result = verify(otp)
22
+ raise OtpError, "Received error: #{result.status}" unless result.valid?
23
+ result
24
+ end
25
+
26
+ private
27
+
28
+ attr_reader :api_id, :api_key, :api_url
29
+
30
+ def get_response(otp)
31
+ uri = URI.parse(api_url) + "verify"
32
+ uri.query = "id=#{api_id}&otp=#{otp}"
33
+
34
+ http = Net::HTTP.new(uri.host, uri.port)
35
+ http.use_ssl = true
36
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
37
+
38
+ request = Net::HTTP::Get.new(uri.request_uri)
39
+
40
+ response = http.request(request).body
41
+
42
+ response
43
+ end
44
+
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,5 @@
1
+ module OmniAuth
2
+ module Yubikey
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
data/oa-yubikey.gemspec CHANGED
@@ -1,10 +1,10 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  $:.push File.expand_path("../lib", __FILE__)
3
- require "oa-yubikey/version"
3
+ require "omniauth/yubikey/version"
4
4
 
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "oa-yubikey"
7
- s.version = Oa::Yubikey::VERSION
7
+ s.version = OmniAuth::Yubikey::VERSION
8
8
  s.authors = ["Steve Hoeksema"]
9
9
  s.email = ["steve@seven.net.nz"]
10
10
  s.homepage = "https://github.com/steveh/oa-yubikey"
@@ -18,7 +18,8 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
- # specify any dependencies here; for example:
22
- # s.add_development_dependency "rspec"
23
- # s.add_runtime_dependency "rest-client"
21
+ s.add_development_dependency "rack"
22
+ s.add_development_dependency "rspec"
23
+
24
+ s.add_runtime_dependency "oa-core"
24
25
  end
@@ -0,0 +1,32 @@
1
+ require "spec_helper"
2
+
3
+ describe OmniAuth::Yubikey::Result do
4
+
5
+ let(:otp) { "ccccccbcifnhfkunerrbtefthtnidhdjfdctblnfihnh" }
6
+ let(:response) { "h=71poL6Z/yjDBF6twhigu777F+7M=\r\nt=2011-09-28T22:05:58Z0964\r\nstatus=OK\r\n\r\n" }
7
+
8
+ before(:all) do
9
+ @result = OmniAuth::Yubikey::Result.new(otp, response)
10
+ end
11
+
12
+ it "should return the OTP" do
13
+ @result.otp.should == "ccccccbcifnhfkunerrbtefthtnidhdjfdctblnfihnh"
14
+ end
15
+
16
+ it "should extract the Yubikey ID from the OTP" do
17
+ @result.id.should == "ccccccbcifnh"
18
+ end
19
+
20
+ it "should extract the Yubikey hash from the response" do
21
+ @result.hash.should == "71poL6Z/yjDBF6twhigu777F+7M="
22
+ end
23
+
24
+ it "should extract the Yubikey timestamp from the response" do
25
+ @result.timestamp.utc.should == Time.utc(2011, 9, 28, 22, 5, 58, 964)
26
+ end
27
+
28
+ it "should extract the Yubikey status from the response" do
29
+ @result.status.should == "OK"
30
+ end
31
+
32
+ end
@@ -0,0 +1 @@
1
+ require "oa-yubikey"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oa-yubikey
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,41 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-13 00:00:00.000000000Z
13
- dependencies: []
12
+ date: 2011-09-28 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: &70224202034200 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70224202034200
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70224202033780 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70224202033780
36
+ - !ruby/object:Gem::Dependency
37
+ name: oa-core
38
+ requirement: &70224202033360 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70224202033360
14
47
  description: OmniAuth strategy for authenticating against the Yubico API with a Yubikey
15
48
  OTP
16
49
  email:
@@ -21,10 +54,19 @@ extra_rdoc_files: []
21
54
  files:
22
55
  - .gitignore
23
56
  - Gemfile
57
+ - LICENSE
58
+ - README.md
24
59
  - Rakefile
60
+ - examples/sinatra.rb
25
61
  - lib/oa-yubikey.rb
26
- - lib/oa-yubikey/version.rb
62
+ - lib/omniauth/strategies/yubikey.rb
63
+ - lib/omniauth/yubikey/otp_error.rb
64
+ - lib/omniauth/yubikey/result.rb
65
+ - lib/omniauth/yubikey/verifier.rb
66
+ - lib/omniauth/yubikey/version.rb
27
67
  - oa-yubikey.gemspec
68
+ - spec/omniauth/yubikey/result_spec.rb
69
+ - spec/spec_helper.rb
28
70
  homepage: https://github.com/steveh/oa-yubikey
29
71
  licenses: []
30
72
  post_install_message:
@@ -50,4 +92,6 @@ signing_key:
50
92
  specification_version: 3
51
93
  summary: OmniAuth strategy for authenticating against the Yubico API with a Yubikey
52
94
  OTP
53
- test_files: []
95
+ test_files:
96
+ - spec/omniauth/yubikey/result_spec.rb
97
+ - spec/spec_helper.rb
@@ -1,5 +0,0 @@
1
- module Oa
2
- module Yubikey
3
- VERSION = "0.0.1"
4
- end
5
- end