tokenizer2fa-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
+ SHA1:
3
+ metadata.gz: 9e7aea321c3dc5462fb08c7d288912f1b42b36fb
4
+ data.tar.gz: a6c3ffb5f853588b2a85953347d4f89c82dfcd6e
5
+ SHA512:
6
+ metadata.gz: 15bb931b7c4ca5eca66b15c7883f81c19c3297116efab67c03260c08f82fa27f745526bc10c08d1aa66b5ec504afc7896fe0ae392285ccb116c6d082897169d3
7
+ data.tar.gz: 063c7b5a05333d185b5568f3d58839ff3ca02bd1f0aa9abf7b4d93ea4d4e1de065305a2756d15d25255fa09854919f52d81b4c4ba13c48a4db3b3942d0e53de4
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tokenizer2fa-client.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Radek Fąfara
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,62 @@
1
+ # Tokenizer2fa::Client
2
+
3
+ This is an integration SDK for [tokenizer.com](https://tokenizer.com) service. The SDK is framework independent and works with pure Ruby 1.9 (or above).
4
+
5
+ Tokenizer is a cloud-based Two-Factor authentication solution developed by [Amsterdam Standard](http://amsterdamstandard.com).
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'tokenizer2fa-client'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install tokenizer2fa-client
20
+
21
+ ## Usage
22
+
23
+ #### Configuration
24
+ In order to connect with Tokenizer, we need the APP ID and APP KEY that you receive via email, when creating a Tokenizer service.
25
+ ```ruby
26
+ tokenizer_config = {
27
+ app_id: 'YOUR-APP-ID',
28
+ app_key: 'YOUR-APP-KEY',
29
+ host: 'https://api.tokenizer.com/'
30
+ }
31
+ tokenizer = Tokenizer2fa::Client.new tokenizer_config
32
+ ```
33
+
34
+ #### Creating a token
35
+ ```ruby
36
+ id = tokenizer.create_auth 'email@example.com'
37
+ ```
38
+
39
+ #### Verifying a token
40
+ ```ruby
41
+ auth_state = tokenizer.verify_auth id
42
+ puts auth_state.to_s
43
+ ```
44
+
45
+ #### Polling token verification
46
+ This gem does not use the Tokenizer provided waiting page, but allows you to poll the API whilst waiting for the user to Tokenize.
47
+ ```ruby
48
+ auth_state = tokenizer.poll_verify_auth id
49
+ if auth_state.is_accepted?
50
+ puts 'Authenticated'
51
+ else
52
+ puts 'Denied'
53
+ end
54
+ ```
55
+
56
+ ## Contributing
57
+
58
+ 1. Fork it ( https://bitbucket.org/tokenizer-integrations/tokenizer-ruby/fork )
59
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
60
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
61
+ 4. Push to the branch (`git push origin my-new-feature`)
62
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,76 @@
1
+ require 'tokenizer2fa/token/state'
2
+ require 'tokenizer2fa/token/adapter/json'
3
+ require 'tokenizer2fa/token/adapter/xml'
4
+
5
+ require 'uri'
6
+ require 'net/http'
7
+
8
+ module Tokenizer2fa
9
+ class Client
10
+ attr_reader :app_id, :app_key, :host, :format
11
+
12
+ DEFAULT_CREATE_AUTH_URL = '%{host}v1/authentications.%{format}'
13
+ DEFAULT_USER_AUTH_URL = '%{host}v1/authentication/%{id}.%{format}?%{params}'
14
+
15
+ def initialize config={}
16
+ @format = 'json'
17
+ config.each { |key,value| instance_variable_set("@#{key}",value) }
18
+ begin
19
+ @adapter = Object.class.const_get("Token::Adapter::#{@format.capitalize}").new
20
+ rescue
21
+ @format = 'json'
22
+ @adapter = Token::Adapter::Json.new
23
+ end
24
+ end
25
+
26
+ # Checks whether all required settings are set
27
+ def validate_config
28
+ @app_id && @app_key && @host
29
+ end
30
+
31
+ # Generates authentication token for an email address
32
+ def create_auth email, url=DEFAULT_CREATE_AUTH_URL
33
+ config_params = {
34
+ app_id: @app_id,
35
+ app_key: @app_key,
36
+ usr_email: email # notice the missing e in usr_email
37
+ }
38
+
39
+ uri = URI(url % { host: @host, format: @format })
40
+ https = Net::HTTP.new(uri.hostname, uri.port)
41
+ https.use_ssl = true
42
+
43
+ request = Net::HTTP::Post.new uri.request_uri
44
+ request.set_form_data config_params
45
+ request.content_type = 'application/x-www-form-urlencoded'
46
+
47
+ response = https.request request
48
+ @adapter.parse(response.body)['id']
49
+ end
50
+
51
+ # Checks whether token ID is valid
52
+ def verify_auth id, url=DEFAULT_USER_AUTH_URL
53
+ config_params = URI.encode_www_form({
54
+ app_id: @app_id,
55
+ app_key: @app_key
56
+ })
57
+
58
+ uri = URI(url % { host: @host, id: id, format: @format, params: config_params })
59
+ https = Net::HTTP.new(uri.hostname, uri.port)
60
+ https.use_ssl = true
61
+
62
+ request = Net::HTTP::Get.new uri.request_uri
63
+
64
+ response = https.request(request)
65
+ Token::State.new @adapter.parse(response.body)['state']
66
+ end
67
+
68
+ def poll_verify_auth id, url=DEFAULT_USER_AUTH_URL
69
+ loop do
70
+ state = verify_auth id, url
71
+ return state unless state.is_pending?
72
+ sleep 1
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,11 @@
1
+ require 'json'
2
+
3
+ module Tokenizer2fa::Token::Adapter
4
+ class Json
5
+
6
+ def parse data
7
+ JSON.parse(data)
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ module Tokenizer2fa::Token::Adapter
2
+ class Xml
3
+
4
+ def parse data
5
+ raise 'Not implemented'
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,21 @@
1
+ module Tokenizer2fa::Token
2
+ class State
3
+
4
+ def initialize state
5
+ @state = state
6
+ end
7
+
8
+ def is_pending?
9
+ @state == 'pending'
10
+ end
11
+
12
+ def is_accepted?
13
+ @state == 'accepted'
14
+ end
15
+
16
+ def to_s
17
+ @state
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module Tokenizer2fa
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ feature 'External request' do
4
+ it 'makes sure external HTTP requests are disabled' do
5
+ uri = URI('https://tokenizer.com')
6
+
7
+ expect {
8
+ response = Net::HTTP.get(uri)
9
+ }.to raise_error(WebMock::NetConnectNotAllowedError)
10
+ end
11
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ tokenizer_config = {
4
+ app_id: 'fake_app',
5
+ app_key: 'fake_key',
6
+ host: 'https://api.tokenizer.com/'
7
+ }
8
+ tokenizer = Tokenizer2fa::Client.new tokenizer_config
9
+
10
+ feature 'Validating configuration' do
11
+ it 'succeeds only for a fully a configured instance of tokenizer connector' do
12
+ expect(tokenizer.validate_config).to be_truthy
13
+ end
14
+
15
+ it 'fails when a user skips any of the params when initializing connector' do
16
+ expect(Tokenizer2fa::Client.new.validate_config).to be_falsey
17
+ end
18
+ end
19
+
20
+ feature 'Creating a token' do
21
+ it 'requests Tokenizer API for a new token ID for e-mail address' do
22
+
23
+ id = tokenizer.create_auth 'valid@example.com'
24
+ expect(id).not_to be_falsey
25
+ end
26
+
27
+ it 'fails when provided invalid e-mail address' do
28
+
29
+ id = tokenizer.create_auth 'not-an-email-address'
30
+ expect(id).to be_nil
31
+ end
32
+ end
33
+
34
+ feature 'Verifying a token' do
35
+ it 'succeeds after at least one trial using valid token' do
36
+ state = nil
37
+ 10.times do
38
+ state = tokenizer.verify_auth '666'
39
+ break unless state.is_pending?
40
+ sleep 1
41
+ end
42
+ expect(state.is_accepted?).to be_truthy
43
+ end
44
+
45
+ it 'fails when using non-existing token_id' do
46
+ state = nil
47
+ 10.times do
48
+ state = tokenizer.verify_auth 'non-existing-token'
49
+ break unless state.is_pending?
50
+ sleep 1
51
+ end
52
+ expect(state.is_accepted?).to be_falsey
53
+ end
54
+
55
+ it 'never gives pending state when polling token verification' do
56
+ state = tokenizer.poll_verify_auth '666'
57
+ expect(state.is_pending?).to be_falsey
58
+
59
+ state = tokenizer.poll_verify_auth 'non-existing-token'
60
+ expect(state.is_pending?).to be_falsey
61
+ end
62
+ end
@@ -0,0 +1,15 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'support/fake_tokenizer_api'
5
+ require 'tokenizer2fa/client'
6
+
7
+ require 'capybara/rspec'
8
+ require 'webmock/rspec'
9
+ WebMock.disable_net_connect!(allow_localhost: true)
10
+
11
+ RSpec.configure do |config|
12
+ config.before(:each) do
13
+ stub_request(:any, /api.tokenizer.com/).to_rack(FakeTokenizerApi)
14
+ end
15
+ end
@@ -0,0 +1,46 @@
1
+ require 'sinatra/base'
2
+
3
+ class FakeTokenizerApi < Sinatra::Base
4
+
5
+ post '/v1/authentications.json' do
6
+ valid_params.each do |k, v|
7
+ params[k].to_s != v and return json_response 400, 'invalid_auth.json'
8
+ end
9
+ json_response 200, 'valid_auth.json'
10
+ end
11
+
12
+ get '/v1/authentication/:token_id.json' do
13
+ valid_user.each do |k, v|
14
+ params[k].to_s != v and return json_response 400, 'invalid_user.json'
15
+ end
16
+ if Time.now.to_i % 5 == 0
17
+ json_response 200, 'valid_user.json'
18
+ else
19
+ json_response 200, 'pending_auth.json'
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def json_response http_code, fixture_filename
26
+ content_type :json
27
+ status http_code
28
+ File.open(File.dirname(__FILE__) + '/fixtures/' + fixture_filename, 'rb').read
29
+ end
30
+
31
+ def valid_params
32
+ {
33
+ app_id: 'fake_app',
34
+ app_key: 'fake_key',
35
+ usr_email: 'valid@example.com'
36
+ }
37
+ end
38
+
39
+ def valid_user
40
+ {
41
+ app_id: 'fake_app',
42
+ app_key: 'fake_key',
43
+ token_id: '666'
44
+ }
45
+ end
46
+ end
@@ -0,0 +1,9 @@
1
+ {
2
+ "status_code": 400,
3
+ "error_name": "ContextError",
4
+ "error_time": 1415970967,
5
+ "error_messages": "Missing request parameters",
6
+ "error_reason": "",
7
+ "error_code": 100,
8
+ "status_messages": "400 ContextError"
9
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "status_code": 400,
3
+ "error_name": "AuthError",
4
+ "error_time": 1415971954,
5
+ "error_messages": "Invalid Token",
6
+ "error_reason": "",
7
+ "error_code": 101,
8
+ "status_messages": "400 AuthError",
9
+ "state": "denied"
10
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "status_code": 200,
3
+ "error_name": "OK",
4
+ "error_time": 1415965000,
5
+ "error_code": 0,
6
+ "state": "pending"
7
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "status_code": 200,
3
+ "error_name": "OK",
4
+ "error_time": 1415971000,
5
+ "error_code": 0,
6
+ "id": 666
7
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "status_code": 200,
3
+ "error_name": "OK",
4
+ "error_time": 1415972000,
5
+ "error_code": 0,
6
+ "state": "accepted"
7
+ }
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tokenizer2fa/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tokenizer2fa-client"
8
+ spec.version = Tokenizer2fa::VERSION
9
+ spec.authors = ["Radosław Fąfara"]
10
+ spec.email = ["radek@amsterdam-standard.pl"]
11
+ spec.summary = 'Tokenizer2fa is an integration client for tokenizer.com service.'
12
+ spec.description = 'The SDK allows framework independent two-factor authentication for your web application. Client handles both token generation and validation.'
13
+ spec.homepage = "https://bitbucket.org/tokenizer-integrations/tokenizer-ruby"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+ spec.required_ruby_version = '>= 1.9'
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "capybara"
25
+ spec.add_development_dependency "sinatra"
26
+ spec.add_development_dependency "webmock"
27
+ end
metadata ADDED
@@ -0,0 +1,158 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tokenizer2fa-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Radosław Fąfara
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: capybara
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sinatra
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: The SDK allows framework independent two-factor authentication for your
98
+ web application. Client handles both token generation and validation.
99
+ email:
100
+ - radek@amsterdam-standard.pl
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - lib/tokenizer2fa/client.rb
111
+ - lib/tokenizer2fa/token/adapter/json.rb
112
+ - lib/tokenizer2fa/token/adapter/xml.rb
113
+ - lib/tokenizer2fa/token/state.rb
114
+ - lib/tokenizer2fa/version.rb
115
+ - spec/features/external_request_spec.rb
116
+ - spec/features/manage_token_spec.rb
117
+ - spec/spec_helper.rb
118
+ - spec/support/fake_tokenizer_api.rb
119
+ - spec/support/fixtures/invalid_auth.json
120
+ - spec/support/fixtures/invalid_user.json
121
+ - spec/support/fixtures/pending_auth.json
122
+ - spec/support/fixtures/valid_auth.json
123
+ - spec/support/fixtures/valid_user.json
124
+ - tokenizer2fa-client.gemspec
125
+ homepage: https://bitbucket.org/tokenizer-integrations/tokenizer-ruby
126
+ licenses:
127
+ - MIT
128
+ metadata: {}
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '1.9'
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubyforge_project:
145
+ rubygems_version: 2.2.2
146
+ signing_key:
147
+ specification_version: 4
148
+ summary: Tokenizer2fa is an integration client for tokenizer.com service.
149
+ test_files:
150
+ - spec/features/external_request_spec.rb
151
+ - spec/features/manage_token_spec.rb
152
+ - spec/spec_helper.rb
153
+ - spec/support/fake_tokenizer_api.rb
154
+ - spec/support/fixtures/invalid_auth.json
155
+ - spec/support/fixtures/invalid_user.json
156
+ - spec/support/fixtures/pending_auth.json
157
+ - spec/support/fixtures/valid_auth.json
158
+ - spec/support/fixtures/valid_user.json