authful 0.5.12 → 0.5.13

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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZDAzNmNmNjdlNDkyMTYzYmEyOTNmNjIxZTYwOWJkNTU2N2E1ZTIxMw==
4
+ ODhlYTdkYWM5ZGE0NmVhMWUyNjI3NDc2ZDBlNzhhYzRiMjcwZDVkMg==
5
5
  data.tar.gz: !binary |-
6
- MTg4ZWRjODI0NWYxYjExN2VhYjk4MjBlZjgwM2UwYWNjY2E3YTYzNw==
6
+ NWVhMTkxNWFkZjQ5N2FlMzlkOTI3ODhhY2I2OWFhODNkY2YxMDQ4OA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MWY5ZTE1YTJiZTE0YzE0MzI5ZGUwMjdiYTA1NDgxODZiMWNhOTRjYjdkZGQw
10
- ZTQ2ZWUyZWM2NDA0Zjc0ZDA5OWQ0MWFkNWI0YWJkNmM5NGUxMTU3N2YxMGY4
11
- NTYyZTM1YjM4NGU4NjU3OGQ2MmY4ZTYxNDkxZGM2NTkxNGUxM2U=
9
+ NDQ0MDVhYTM2ZjcxYzlmMzBjZTljY2RmZGY1M2I0ZDExZWQyMzY5NmRhMzVm
10
+ OWFjZDBiNGRkNTY4ZDZlZTViOGQ2ODQ5ZTU2OGFkY2IzNDIxNWI3NzllNTIz
11
+ ZTE0M2Y4NWIzZWUwYTBmMDVkOWNiZjEyNTJkMDQyNzQ1OGJkMjg=
12
12
  data.tar.gz: !binary |-
13
- YmUzMWNkYzk0NjRiZmI0NWQ4YjdlY2UwMWY5MWQ4MDlmOGJlZDVkNGE3MDVm
14
- Yjc3ZGJkZTY2ZjJiNjAxNDBhY2RmNTBhMTk2ZTUwZDIxMGZlYmQzNjlkYTc0
15
- YjcwNzRiYjU2Yjc4NmJkOTcxMTcxOGYwZDg2MGIyYTFlYzYyYmI=
13
+ YjlhN2M1MGI4ZmM3Nzk3ZDk3ZWQzNzQ3OGI4ZWI1YWY4ZmUyZmE3MDkxMmM0
14
+ NGJmZDUyMzQ2ZjlmZDk4MWFhNmQ4M2ZlODdiNzg2MTZlNTIzNzUxZGU2MDUw
15
+ MTc0NWE0MmI5MTdhOTQ2YTRiY2NjNmVmNjA1MGQ5OWUzZWU4NWY=
@@ -6,10 +6,10 @@ require 'authful/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "authful"
8
8
  spec.version = Authful::Client::VERSION
9
- spec.authors = ["Chris Winslett"]
10
- spec.email = ["chris@mongohq.com"]
9
+ spec.authors = ["Ben Wyrosdick", "Chris Winslett"]
10
+ spec.email = ["ben@mongohq.com", "chris@mongohq.com"]
11
11
  spec.description = %q{A wrapper for the Authful service}
12
- spec.summary = %q{Both authful is an open source tool that enables rapid implementation of 2fa for your application. This is the client for the service.}
12
+ spec.summary = %q{Authful is an open source tool that enables rapid implementation of 2fa for your application. This is the client for the service.}
13
13
  spec.homepage = ""
14
14
  spec.license = "MIT"
15
15
 
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_dependency 'rest-client'
22
+ spec.add_dependency 'active_support'
22
23
 
23
24
  spec.add_development_dependency "bundler", "~> 1.3"
24
25
  spec.add_development_dependency "rake"
@@ -1,41 +1,19 @@
1
- require 'json'
1
+ require 'active_support/concern'
2
2
  require 'rest-client'
3
+ require 'json'
3
4
 
4
- module Authful
5
- @config = Struct.new(:endpoint, :api_key).new
6
-
7
- def self.configure(&block)
8
- block.call(@config)
9
- end
10
-
11
- def self.config
12
- @config
13
- end
14
-
15
- ## API Calls
16
-
17
- def self.enroll(email, phone)
18
- u = Authful::User.new(email: email, phone: phone)
19
- u.save
20
- return Authful::Response.new(token: u.token, qr_code: u.qr_code, error: u.error)
21
- end
22
-
23
- def self.validate(token, otp)
24
- Authful::User[token].valid?(otp)
25
- end
26
-
27
- def self.send_sms(token)
28
- Authful::User[token].send_sms
29
- end
5
+ require_relative './authful/endpoint'
6
+ require_relative './authful/api'
7
+ require_relative './authful/response'
30
8
 
31
- def self.unenroll(token)
32
- Authful::User[token].destroy
33
- end
9
+ module Authful
10
+ include Authful::Endpoint
11
+ include Authful::Api
34
12
 
35
13
  module Errors
36
14
  class IncorrectApiToken < RuntimeError; end
15
+ class InvalidUserToken < RuntimeError; end
16
+ class InvallidOtpToken < RuntimeError; end
17
+ class AlreadyEnrolled < RuntimeError; end
37
18
  end
38
19
  end
39
-
40
- require_relative './authful/user'
41
- require_relative './authful/response'
@@ -0,0 +1,33 @@
1
+ module Authful
2
+ module Api
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def enroll(email)
7
+ res = Authful.send_request(:post, "/api/users", email: email)
8
+ return Authful::Response.new(token: res["token"], qr_code: res["qr_code"], error: res["error"])
9
+ end
10
+
11
+ def validate(token, otp)
12
+ Authful.send_request(:get, "/api/users/#{token}/validate?token=#{otp}")["ok"] == 1
13
+ end
14
+
15
+ def send_sms(token)
16
+ Authful.send_request(:get, "/api/users/#{token}/send_sms")["ok"] == 1
17
+ end
18
+
19
+ def set_phone(token, phone)
20
+ Authful.send_request(:patch, "/api/users/#{token}", send_sms: true, phone: phone)["error"] == nil
21
+ end
22
+
23
+ def reset(token, send_sms = false)
24
+ res = Authful.send_request(:patch, "/api/users/#{token}", send_sms: send_sms, reset: true)
25
+ return Authful::Response.new(token: res["token"], qr_code: res["qr_code"], error: res["error"])
26
+ end
27
+
28
+ def unenroll(token)
29
+ Authful.send_request(:delete, "/api/users/#{token}")["ok"] == 1
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,52 @@
1
+ module Authful
2
+ module Endpoint
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ @config = Struct.new(:endpoint, :api_key).new
7
+ end
8
+
9
+ module ClassMethods
10
+ def configure(&block)
11
+ block.call(@config)
12
+ end
13
+
14
+ def config
15
+ @config
16
+ end
17
+
18
+ def send_request(method, path, data = nil)
19
+ if data
20
+ r = RestClient.send(method, url_for(path), data, headers)
21
+ else
22
+ r = RestClient.send(method, url_for(path), headers)
23
+ end
24
+ JSON.parse(r)
25
+ rescue
26
+ capture_common_errors($!)
27
+ end
28
+
29
+ def capture_common_errors(error)
30
+ if error.http_code == 400
31
+ JSON.parse($!.response)
32
+ elsif error.http_code == 401
33
+ raise Authful::Errors::IncorrectApiToken
34
+ elsif error.http_code == 403
35
+ JSON.parse($!.response)
36
+ elsif error.http_code == 409
37
+ JSON.parse($!.response)
38
+ else
39
+ raise error
40
+ end
41
+ end
42
+
43
+ def url_for(path)
44
+ [Authful.config.endpoint, path].join("")
45
+ end
46
+
47
+ def headers
48
+ {"Api-Token" => Authful.config.api_key}
49
+ end
50
+ end
51
+ end
52
+ end
@@ -66,44 +66,5 @@ module Authful
66
66
  raise $!
67
67
  end
68
68
  end
69
-
70
- private
71
- def get(path)
72
- r = RestClient.get url_for(path), headers
73
- JSON.parse(r)
74
- rescue
75
- capture_common_errors($!)
76
- end
77
-
78
- def delete(path)
79
- r = RestClient.delete url_for(path), headers
80
- JSON.parse(r)
81
- rescue
82
- capture_common_errors($!)
83
- end
84
-
85
-
86
- def post(path, data)
87
- r = RestClient.post url_for(path), data, headers
88
- JSON.parse(r)
89
- rescue
90
- capture_common_errors($!)
91
- end
92
-
93
- def capture_common_errors(error)
94
- if error.http_code == 401
95
- raise Authful::Errors::IncorrectApiToken
96
- else
97
- raise error
98
- end
99
- end
100
-
101
- def url_for(path)
102
- [Authful.config.endpoint, path].join("")
103
- end
104
-
105
- def headers
106
- {"Api-Token" => Authful.config.api_key}
107
- end
108
69
  end
109
70
  end
@@ -5,7 +5,7 @@ module Authful
5
5
  if ENV['CIRCLE_BUILD_NUM']
6
6
  VERSION = "#{VERSION}.#{ENV['CIRCLE_BUILD_NUM']}"
7
7
  else
8
- VERSION = "#{VERSION}-head"
8
+ VERSION = "#{VERSION}.head"
9
9
  end
10
10
  end
11
11
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Authful::User do
3
+ describe Authful do
4
4
 
5
5
  before :each do
6
6
  Authful.configure do |c|
@@ -12,98 +12,75 @@ describe Authful::User do
12
12
  it "handles bad api key errors" do
13
13
  FakeWeb.register_uri :post, "https://my-endpoint.dev/authful/api/users", {body: {error: "Incrorrect API token"}.to_json, status: ["401", "Unauthorized"]}
14
14
 
15
- -> { Authful::User.create!(email: "john.doe@gmail.com", phone: "12055555434") }.should raise_error(Authful::Errors::IncorrectApiToken)
15
+ -> { Authful.enroll "john.doe@gmail.com" }.should raise_error(Authful::Errors::IncorrectApiToken)
16
16
  end
17
17
 
18
18
  it "creates a user" do
19
19
  FakeWeb.register_uri :post, "https://my-endpoint.dev/authful/api/users", {body: {token: "user-authful-token", email: "john.doe@gmail.com", qr_code: "qr-code"}.to_json}
20
20
 
21
- u = Authful::User.new(email: "john.doe@gmail.com", phone: "12055555434")
21
+ res = Authful.enroll "john.doe@gmail.com"
22
22
 
23
- u.save.should eq('user-authful-token')
24
- u.token.should eq('user-authful-token')
25
- u.qr_code.should eq('qr-code')
26
- u.error.should be_nil
23
+ res.token.should eq('user-authful-token')
24
+ res.qr_code.should eq('qr-code')
25
+ res.error.should be_nil
27
26
  end
28
27
 
29
28
  it "accepts error on duplicate user, and finds user" do
30
29
  FakeWeb.register_uri :post, "https://my-endpoint.dev/authful/api/users", {body: {error: "already enrolled", token: "user-authful-token"}.to_json, status: ["409", "Something"]}
31
30
 
32
- u = Authful::User.new(email: "john.doe@gmail.com", phone: "12055555434")
31
+ res = Authful.enroll "john.doe@gmail.com"
33
32
 
34
- u.save.should eq('user-authful-token')
35
- u.token.should eq('user-authful-token')
36
- u.error.should be_nil
33
+ res.token.should eq('user-authful-token')
34
+ res.error.should eq("already enrolled")
37
35
  end
38
36
 
39
37
  it "errors on invalid user" do
40
38
  FakeWeb.register_uri :post, "https://my-endpoint.dev/authful/api/users", {body: {error: "Invalid user, email is required."}.to_json, status: ["400", "Bad Request"]}
41
39
 
42
- u = Authful::User.new(email: "", phone: "")
40
+ res = Authful.enroll ""
43
41
 
44
- u.save.should eq(false)
45
- u.error.should eq("Invalid user, email is required.")
42
+ res.error.should eq("Invalid user, email is required.")
46
43
  end
47
44
 
48
45
  it "validates the users token" do
49
46
  FakeWeb.register_uri :get, "https://my-endpoint.dev/authful/api/users/user-authful-token/validate?token=000000", {body: {ok: 1}.to_json}
50
47
 
51
- u = Authful::User["user-authful-token"]
52
- u.valid?("000000").should eq(true)
48
+ Authful.validate("user-authful-token", "000000").should eq(true)
53
49
  end
54
50
 
55
51
  it "does not validate invalid tokens" do
56
52
  FakeWeb.register_uri :get, "https://my-endpoint.dev/authful/api/users/user-authful-token/validate?token=000000", {body: {error: "Invalid token"}.to_json, status: [403, "Forbidden"]}
57
53
 
58
- u = Authful::User["user-authful-token"]
59
- u.valid?("000000").should eq(false)
54
+ Authful.validate("user-authful-token", "000000").should eq(false)
60
55
  end
61
56
 
62
57
  it "sends sms to users" do
63
58
  FakeWeb.register_uri :get, "https://my-endpoint.dev/authful/api/users/user-authful-token/send_sms", {body: {ok: 1}.to_json}
64
59
 
65
- u = Authful::User["user-authful-token"]
66
- u.send_sms.should eq(true)
60
+ Authful.send_sms("user-authful-token").should eq(true)
67
61
  end
68
62
 
69
63
  it "catches failed sms" do
70
64
  FakeWeb.register_uri :get, "https://my-endpoint.dev/authful/api/users/user-authful-token/send_sms", {body: {error: "It just didn't send"}.to_json, status: [400, "Bad Request"]}
71
65
 
72
- u = Authful::User["user-authful-token"]
73
- u.send_sms.should eq(false)
74
- u.error = "It just didn't send"
66
+ Authful.send_sms("user-authful-token").should eq(false)
75
67
  end
76
68
 
77
- it "deletes users" do
78
- FakeWeb.register_uri :delete, "https://my-endpoint.dev/authful/api/users/user-authful-token", {body: {ok: 1}.to_json}
69
+ it "resets secret for user" do
70
+ FakeWeb.register_uri :patch, "https://my-endpoint.dev/authful/api/users/user-authful-token", {body: {token: "user-authful-token", email: "john.doe@gmail.com", qr_code: "qr-code"}.to_json}
79
71
 
80
- u = Authful::User["user-authful-token"]
81
- u.destroy.should eq(true)
72
+ res = Authful.reset("user-authful-token")
73
+ res.token.should eq('user-authful-token')
74
+ res.qr_code.should eq('qr-code')
82
75
  end
83
76
 
84
- it "accepts the wrapper function for enroll" do
85
- FakeWeb.register_uri :post, "https://my-endpoint.dev/authful/api/users", {body: {token: "user-authful-token", email: "john.doe@gmail.com", qr_code: "qr-code"}.to_json}
77
+ it "sets phone number for user" do
78
+ FakeWeb.register_uri :patch, "https://my-endpoint.dev/authful/api/users/user-authful-token", {body: {token: "user-authful-token", email: "john.doe@gmail.com", qr_code: "qr-code"}.to_json}
86
79
 
87
- r = Authful.enroll("john.doe@gmail.com", "12055555434")
88
-
89
- r.token.should eq('user-authful-token')
90
- r.qr_code.should eq('qr-code')
91
- r.error.should be_nil
80
+ res = Authful.set_phone("user-authful-token", "12005551212").should eq(true)
92
81
  end
93
82
 
94
- it "accepts the wrapper function for validating the users token" do
95
- FakeWeb.register_uri :get, "https://my-endpoint.dev/authful/api/users/user-authful-token/validate?token=000000", {body: {ok: 1}.to_json}
96
-
97
- Authful.validate("user-authful-token", "000000").should eq(true)
98
- end
99
-
100
- it "accepts the wrapper function for send sms" do
101
- FakeWeb.register_uri :get, "https://my-endpoint.dev/authful/api/users/user-authful-token/send_sms", {body: {ok: 1}.to_json}
102
-
103
- Authful.send_sms("user-authful-token").should eq(true)
104
- end
105
-
106
- it "accepts the wrapper function for unenroll" do
83
+ it "deletes users" do
107
84
  FakeWeb.register_uri :delete, "https://my-endpoint.dev/authful/api/users/user-authful-token", {body: {ok: 1}.to_json}
108
85
 
109
86
  Authful.unenroll("user-authful-token").should eq(true)
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: authful
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.12
4
+ version: 0.5.13
5
5
  platform: ruby
6
6
  authors:
7
+ - Ben Wyrosdick
7
8
  - Chris Winslett
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2014-01-03 00:00:00.000000000 Z
12
+ date: 2014-01-12 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: rest-client
@@ -24,6 +25,20 @@ dependencies:
24
25
  - - ! '>='
25
26
  - !ruby/object:Gem::Version
26
27
  version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: active_support
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
27
42
  - !ruby/object:Gem::Dependency
28
43
  name: bundler
29
44
  requirement: !ruby/object:Gem::Requirement
@@ -110,6 +125,7 @@ dependencies:
110
125
  version: '0'
111
126
  description: A wrapper for the Authful service
112
127
  email:
128
+ - ben@mongohq.com
113
129
  - chris@mongohq.com
114
130
  executables: []
115
131
  extensions: []
@@ -124,10 +140,12 @@ files:
124
140
  - authful.gemspec
125
141
  - circle.yml
126
142
  - lib/authful.rb
143
+ - lib/authful/api.rb
144
+ - lib/authful/endpoint.rb
127
145
  - lib/authful/response.rb
128
146
  - lib/authful/user.rb
129
147
  - lib/authful/version.rb
130
- - spec/lib/authful/user_spec.rb
148
+ - spec/lib/authful/api_spec.rb
131
149
  - spec/spec_helper.rb
132
150
  homepage: ''
133
151
  licenses:
@@ -152,8 +170,8 @@ rubyforge_project:
152
170
  rubygems_version: 2.2.0
153
171
  signing_key:
154
172
  specification_version: 4
155
- summary: Both authful is an open source tool that enables rapid implementation of
156
- 2fa for your application. This is the client for the service.
173
+ summary: Authful is an open source tool that enables rapid implementation of 2fa for
174
+ your application. This is the client for the service.
157
175
  test_files:
158
- - spec/lib/authful/user_spec.rb
176
+ - spec/lib/authful/api_spec.rb
159
177
  - spec/spec_helper.rb