bithavoc-identity 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 67cb3fed971b300e5e8f0339dfb606adce130d88
4
+ data.tar.gz: 5e6bdc8430cdd3df5210af2e0a83370f7d3147a4
5
+ SHA512:
6
+ metadata.gz: 02ddadb09c85bb5b683ea26bde5a9a3a5e8109dd09a1fbe3afd2991cca41db803d25e73a5b873eaa3f8d8bf22c416feea0747de222afb158c38cff6a7f4c36d1
7
+ data.tar.gz: 469ebf5611a90168af985edb1faaa42045790da8c9b9147a5c021cf4d4a81d45915c597adbd5042aba01088e1b0fb7d1d0f5677fea87ac619ac4be342371df74
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Bithavoc - http://bithavoc.io
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,40 @@
1
+ # Bithavoc::Identity
2
+
3
+ Ruby client library for Bithavoc Identity Hub
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'bithavoc-identity'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install bithavoc-identity
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Test
26
+
27
+ rake test
28
+
29
+ ## Contributing
30
+
31
+ 1. Fork it ( https://github.com/bithavoc/id-ruby-client/fork )
32
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
33
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
34
+ 4. Push to the branch (`git push origin my-new-feature`)
35
+ 5. Create a new Pull Request
36
+
37
+ ## License(MIT)
38
+
39
+ Copyright (c) 2014 Bithavoc - http://bithavoc.io
40
+
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.pattern = "test/**/*_test.rb"
6
+ t.options = '-v'
7
+ end
@@ -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 'bithavoc/identity/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bithavoc-identity"
8
+ spec.version = Bithavoc::Identity::VERSION
9
+ spec.authors = ["bithavoc"]
10
+ spec.email = ["im@bithavoc.io"]
11
+ spec.summary = "Client for Bithavoc Identity Hub"
12
+ spec.description = "Full-featured client library for id.bithavoc.io"
13
+ spec.homepage = "http://bithavoc.io/id-ruby-client"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "httparty", "~> 0.13"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "minitest", "~> 5.4"
26
+ spec.add_development_dependency "webmock", "~> 1.18"
27
+ end
@@ -0,0 +1,3 @@
1
+ require "bithavoc/identity/error"
2
+ require "bithavoc/identity/version"
3
+ require "bithavoc/identity/client"
@@ -0,0 +1,23 @@
1
+ require "bithavoc/identity/connectivity"
2
+ Dir.glob( File.join( File.dirname(__FILE__), 'operations', '*.rb' ), &method(:require) )
3
+
4
+ module Bithavoc
5
+ module Identity
6
+ class Client
7
+ include Connectivity
8
+
9
+ include Negotiate
10
+ include SignIn
11
+ include SignUp
12
+ include Confirm
13
+ include Validate
14
+
15
+ attr_reader :app_id
16
+
17
+ def initialize(app_id)
18
+ @app_id = app_id
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ require "httparty"
2
+
3
+ module Bithavoc::Identity::Connectivity
4
+
5
+ def self.included(kclass)
6
+ kclass.class_eval do
7
+ include HTTParty
8
+ case ENV['BH_ENV']
9
+ when '1'
10
+ kclass.base_uri "http://127.0.0.1:4000"
11
+ else
12
+ kclass.base_uri "https://id.bithavoc.io"
13
+ end
14
+ end
15
+ end
16
+
17
+ end
@@ -0,0 +1,6 @@
1
+ module Bithavoc
2
+ module Identity
3
+ class IdentityError < StandardError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,16 @@
1
+
2
+ module Bithavoc::Identity::Confirm
3
+
4
+ def confirm(email_code)
5
+ res = self.class.post("/apps/#{app_id}/confirm", body: {code: email_code})
6
+ case res.code
7
+ when 200
8
+ res['auth_code']
9
+ when 422
10
+ raise Bithavoc::Identity::IdentityError.new, res['message']
11
+ else
12
+ raise Bithavoc::Identity::IdentityError.new, "Something went wrong while confirming your email"
13
+ end
14
+ end
15
+
16
+ end
@@ -0,0 +1,16 @@
1
+
2
+ module Bithavoc::Identity::Negotiate
3
+
4
+ def negotiate(auth_code)
5
+ res = self.class.post("/apps/#{app_id}/tokens", body: {code: auth_code})
6
+ case res.code
7
+ when 201
8
+ return res['token'], res['user']
9
+ when 422
10
+ raise Bithavoc::Identity::IdentityError.new, res['message']
11
+ else
12
+ raise Bithavoc::Identity::IdentityError.new, "Something went wrong while validating your credentials"
13
+ end
14
+ end
15
+
16
+ end
@@ -0,0 +1,13 @@
1
+ module Bithavoc::Identity::SignIn
2
+ def sign_in(email, password)
3
+ res = self.class.post("/apps/#{app_id}/sign-in", body: {email: email, password: password, password_confirmation: password})
4
+ case res.code
5
+ when 200
6
+ res['auth_code']
7
+ when 422
8
+ raise Bithavoc::Identity::IdentityError.new, res['message']
9
+ else
10
+ raise Bithavoc::Identity::IdentityError.new, "Something went wrong while validating your credentials"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ module Bithavoc::Identity::SignUp
2
+ SIGNUP_PASSTHROUGH=['email', 'fullname', 'password']
3
+ def sign_up(options)
4
+ options = Hash[(options.select {|k,v| SIGNUP_PASSTHROUGH.include?(k.to_s) }.merge!({:password_confirmation=> options['password'] || options[:password]})).sort]
5
+ res = self.class.post("/apps/#{app_id}/sign-up", body: options)
6
+ case res.code
7
+ when 200
8
+ nil
9
+ when 422
10
+ raise Bithavoc::Identity::IdentityError.new, res['message']
11
+ else
12
+ raise Bithavoc::Identity::IdentityError.new, "Something went wrong while validating your credentials"
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+
2
+ module Bithavoc::Identity::Validate
3
+
4
+ def validate(token)
5
+ res = self.class.get("/apps/#{app_id}/tokens/#{token}")
6
+ case res.code
7
+ when 200
8
+ res['user']
9
+ when 403
10
+ raise Bithavoc::Identity::IdentityError.new, res['message']
11
+ else
12
+ raise Bithavoc::Identity::IdentityError.new, "Something went wrong while confirming your email"
13
+ end
14
+ end
15
+
16
+ end
@@ -0,0 +1,5 @@
1
+ module Bithavoc
2
+ module Identity
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/unit'
3
+ require 'bithavoc/identity'
4
+ require 'webmock/minitest'
@@ -0,0 +1,27 @@
1
+ require_relative '../test_helper'
2
+
3
+ class ConfirmTest < MiniTest::Test
4
+
5
+ def setup
6
+ @client = create_client
7
+ end
8
+
9
+ def create_client
10
+ Bithavoc::Identity::Client.new('59abb3124156a6e47e39108e36f9f380')
11
+ end
12
+
13
+ def test_confirm_success
14
+ stub_request(:post, "https://id.bithavoc.io/apps/59abb3124156a6e47e39108e36f9f380/confirm").with(:body => "code=email_code").to_return(:status=> 200, :body=> '{"auth_code": "some_auth_code"}', :headers=>{"Content-Type"=>"application/json"})
15
+ code = @client.confirm('email_code')
16
+ assert_equal "some_auth_code", code
17
+ end
18
+
19
+ def test_invalid_access_code
20
+ stub_request(:post, "https://id.bithavoc.io/apps/59abb3124156a6e47e39108e36f9f380/confirm").with(:body => "code=invalid_code").to_return(:status=> 422, :body=> '{"message": "Invalid confirmation code"}', :headers=>{"Content-Type"=>"application/json"})
21
+ err = assert_raises(Bithavoc::Identity::IdentityError) do
22
+ @client.confirm('invalid_code')
23
+ end
24
+ assert_equal "Invalid confirmation code", err.message
25
+ end
26
+ end
27
+
@@ -0,0 +1,28 @@
1
+ require_relative '../test_helper'
2
+
3
+ class NegotiateTest < MiniTest::Test
4
+
5
+ def setup
6
+ @client = create_client
7
+ end
8
+
9
+ def create_client
10
+ Bithavoc::Identity::Client.new('59abb3124156a6e47e39108e36f9f380')
11
+ end
12
+
13
+ def test_create
14
+ stub_request(:post, "https://id.bithavoc.io/apps/59abb3124156a6e47e39108e36f9f380/tokens").with(:body => "code=some_auth_code").to_return(:status=> 201, :body=> '{"token": "some_auth_token", "user": {"email": "johan@johan.com", "fullname": "Johan Johanson"}}', :headers=>{"Content-Type"=>"application/json"})
15
+ token, user = @client.negotiate('some_auth_code')
16
+ assert_equal "some_auth_token", token
17
+ assert_equal "johan@johan.com", user['email']
18
+ end
19
+
20
+ def test_invalid_access_code
21
+ stub_request(:post, "https://id.bithavoc.io/apps/59abb3124156a6e47e39108e36f9f380/tokens").with(:body => "code=invalid_code").to_return(:status=> 422, :body=> '{"message": "Invalid authorization code"}', :headers=>{"Content-Type"=>"application/json"})
22
+ err = assert_raises(Bithavoc::Identity::IdentityError) do
23
+ @client.negotiate('invalid_code')
24
+ end
25
+ assert_equal "Invalid authorization code", err.message
26
+ end
27
+ end
28
+
@@ -0,0 +1,28 @@
1
+ require_relative '../test_helper'
2
+
3
+ class SignInTest < MiniTest::Test
4
+
5
+ def setup
6
+ @client = create_client
7
+ end
8
+
9
+ def create_client
10
+ Bithavoc::Identity::Client.new('59abb3124156a6e47e39108e36f9f380')
11
+ end
12
+
13
+ def test_success_signin
14
+ stub_request(:post, "https://id.bithavoc.io/apps/59abb3124156a6e47e39108e36f9f380/sign-in").with(:body => "email=johan%40johan.com&password=pass1&password_confirmation=pass1").to_return(:status=> 200, :body=> '{"auth_code": "some_auth_code"}', :headers=>{"Content-Type"=>"application/json"})
15
+ auth_code = @client.sign_in('johan@johan.com', 'pass1')
16
+ assert_equal "some_auth_code", auth_code
17
+ end
18
+
19
+ def test_invalid_access_code
20
+ stub_request(:post, "https://id.bithavoc.io/apps/59abb3124156a6e47e39108e36f9f380/sign-in").with(:body => "email=wrong%40example.com&password=wrong&password_confirmation=wrong").to_return(:status=> 422, :body=> '{"message": "wrong password"}', :headers=>{"Content-Type"=>"application/json"})
21
+ err = assert_raises(Bithavoc::Identity::IdentityError) do
22
+ @client.sign_in('wrong@example.com', 'wrong')
23
+ end
24
+ assert_equal "wrong password", err.message
25
+ end
26
+
27
+ end
28
+
@@ -0,0 +1,27 @@
1
+ require_relative '../test_helper'
2
+
3
+ class SignUpTest < MiniTest::Test
4
+
5
+ def setup
6
+ @client = create_client
7
+ end
8
+
9
+ def create_client
10
+ Bithavoc::Identity::Client.new('59abb3124156a6e47e39108e36f9f380')
11
+ end
12
+
13
+ def test_success_signup
14
+ stub_request(:post, "https://id.bithavoc.io/apps/59abb3124156a6e47e39108e36f9f380/sign-up").with(:body => "email=johan%40johan.com&fullname=chill%20bithavoc&password=pass1&password_confirmation=pass1").to_return(:status=> 200, :body=> '{}', :headers=>{"Content-Type"=>"application/json"})
15
+ @client.sign_up(email: 'johan@johan.com', password: 'pass1', fullname: 'chill bithavoc')
16
+ end
17
+
18
+ def test_error_signup
19
+ stub_request(:post, "https://id.bithavoc.io/apps/59abb3124156a6e47e39108e36f9f380/sign-up").with(:body => "email=johan%40johan.com&fullname=chill%20bithavoc&password=pass1&password_confirmation=pass1").to_return(:status=> 422, :body=> '{"message": "Email already taken"}', :headers=>{"Content-Type"=>"application/json"})
20
+ err = assert_raises(Bithavoc::Identity::IdentityError) do
21
+ @client.sign_up(email: 'johan@johan.com', password: 'pass1', fullname: 'chill bithavoc')
22
+ end
23
+ assert_equal "Email already taken", err.message
24
+ end
25
+
26
+ end
27
+
@@ -0,0 +1,27 @@
1
+ require_relative '../test_helper'
2
+
3
+ class ValidateTest < MiniTest::Test
4
+
5
+ def setup
6
+ @client = create_client
7
+ end
8
+
9
+ def create_client
10
+ Bithavoc::Identity::Client.new('59abb3124156a6e47e39108e36f9f380')
11
+ end
12
+
13
+ def test_validate_success
14
+ stub_request(:get, "https://id.bithavoc.io/apps/59abb3124156a6e47e39108e36f9f380/tokens/valid_token").to_return(:status=> 200, :body=> '{"token": "valid_token", "user": {"email": "johan@example.com"}}', :headers=>{"Content-Type"=>"application/json"})
15
+ user = @client.validate('valid_token')
16
+ assert_equal({"email" => "johan@example.com"}, user)
17
+ end
18
+
19
+ def test_validate_failed
20
+ stub_request(:get, "https://id.bithavoc.io/apps/59abb3124156a6e47e39108e36f9f380/tokens/invalid_token").to_return(:status=> 403, :body=> '{"message": "Invalid confirmation code"}', :headers=>{"Content-Type"=>"application/json"})
21
+ err = assert_raises(Bithavoc::Identity::IdentityError) do
22
+ @client.validate('invalid_token')
23
+ end
24
+ assert_equal "Invalid confirmation code", err.message
25
+ end
26
+ end
27
+
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bithavoc-identity
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - bithavoc
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.13'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.4'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.18'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.18'
83
+ description: Full-featured client library for id.bithavoc.io
84
+ email:
85
+ - im@bithavoc.io
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE
93
+ - README.md
94
+ - Rakefile
95
+ - bithavoc-identity.gemspec
96
+ - lib/bithavoc/identity.rb
97
+ - lib/bithavoc/identity/client.rb
98
+ - lib/bithavoc/identity/connectivity.rb
99
+ - lib/bithavoc/identity/error.rb
100
+ - lib/bithavoc/identity/operations/confirm.rb
101
+ - lib/bithavoc/identity/operations/negotiate.rb
102
+ - lib/bithavoc/identity/operations/signin.rb
103
+ - lib/bithavoc/identity/operations/signup.rb
104
+ - lib/bithavoc/identity/operations/validate.rb
105
+ - lib/bithavoc/identity/version.rb
106
+ - test/test_helper.rb
107
+ - test/tests/confirm_test.rb
108
+ - test/tests/negotiate_test.rb
109
+ - test/tests/signin_test.rb
110
+ - test/tests/signup_test.rb
111
+ - test/tests/validate_test.rb
112
+ homepage: http://bithavoc.io/id-ruby-client
113
+ licenses:
114
+ - MIT
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 2.2.2
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: Client for Bithavoc Identity Hub
136
+ test_files:
137
+ - test/test_helper.rb
138
+ - test/tests/confirm_test.rb
139
+ - test/tests/negotiate_test.rb
140
+ - test/tests/signin_test.rb
141
+ - test/tests/signup_test.rb
142
+ - test/tests/validate_test.rb