simpleapikeyengine 0.1.0 → 0.1.1

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,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 536016096a163e0a2561c8352de594f35cf25a9f
4
- data.tar.gz: 5624f9f5ca889ac170588bd336c309d60092bf8b
3
+ metadata.gz: 07f5b168e790607fcc0d8b35cbdc24662e615050
4
+ data.tar.gz: 38b118f1e391d69349db8c68b5eddd2d92310d60
5
5
  SHA512:
6
- metadata.gz: 8c84fe3a41311e386485db06438e5e9c3f28c6e13b2151b5f94200a337f41a5472cf15ba00a6d16601d7ae01555439650bd6859e0bdaa7c6222a48e5350189b7
7
- data.tar.gz: eec86b2c43670628520600ef751ee55cb01de78c735b99e941d52bf7406087efbc114911c5c62750a88aa042487ee1589ba5f9ed784955bf880ec160e2542f63
6
+ metadata.gz: 6a2c7b750b0da1e802aea29354ea5252c661f87b02f9e05b45d27c26ed7d02ab15a47ad6dc52d3e16adedf76599ccb64e9c7de4536e692c5869bbf434e966c0b
7
+ data.tar.gz: 408e4efec0d56f57704bccd0dfdec01bdfa8f85228554c3f5c45ff2ae49b5a656153e9291af9b5849a8f5b0808616117054ea10f3d7e64b279c8ea6c4188b37d
data/MIT-LICENSE CHANGED
@@ -1,20 +1,20 @@
1
- Copyright 2014 YOURNAME
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining
4
- a copy of this software and associated documentation files (the
5
- "Software"), to deal in the Software without restriction, including
6
- without limitation the rights to use, copy, modify, merge, publish,
7
- distribute, sublicense, and/or sell copies of the Software, and to
8
- permit persons to whom the Software is furnished to do so, subject to
9
- the following conditions:
10
-
11
- The above copyright notice and this permission notice shall be
12
- included in all copies or substantial portions of the Software.
13
-
14
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1
+ Copyright 2014 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- simpleapikeyengine
2
- =====================
3
-
4
- Simple API Key Engine.
1
+ simpleapikeyengine
2
+ =====================
3
+
4
+ Simple API Key Engine.
data/Rakefile CHANGED
@@ -1,8 +1,8 @@
1
- begin
2
- require 'bundler/setup'
3
- rescue LoadError
4
- puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
- end
6
-
7
- Bundler::GemHelper.install_tasks
8
-
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ Bundler::GemHelper.install_tasks
8
+
@@ -1,37 +1,37 @@
1
- module SimpleApiKeyEngine
2
- class ApiKey < ActiveRecord::Base
3
- before_create :generate_token
4
- before_create :set_expiration
5
-
6
- def self.activate(token)
7
- api_key = where(token: token).first
8
- return api_key && !api_key.expired? ? api_key : nil
9
- end
10
-
11
- def user_class
12
- Class.const_get(user_type)
13
- end
14
-
15
- def user=(obj)
16
- self.user_ident = obj.to_param
17
- self.user_type = obj.class.name
18
- @user = obj
19
- end
20
-
21
- def expired?
22
- Time.current >= expires_at
23
- end
24
-
25
- private
26
- def generate_token
27
- begin
28
- self.token = SecureRandom.hex
29
- end while self.class.exists?(token: token)
30
- end
31
-
32
- def set_expiration
33
- self.expires_at = Time.current + 30.days
34
- end
35
-
36
- end
37
- end
1
+ module SimpleApiKeyEngine
2
+ class ApiKey < ActiveRecord::Base
3
+ before_create :generate_token
4
+ before_create :set_expiration
5
+
6
+ def self.activate(token)
7
+ api_key = where(token: token).first
8
+ return api_key && !api_key.expired? ? api_key : nil
9
+ end
10
+
11
+ def user_class
12
+ Class.const_get(user_type)
13
+ end
14
+
15
+ def user=(obj)
16
+ self.user_ident = obj.to_param
17
+ self.user_type = obj.class.name
18
+ @user = obj
19
+ end
20
+
21
+ def expired?
22
+ Time.current >= expires_at
23
+ end
24
+
25
+ private
26
+ def generate_token
27
+ begin
28
+ self.token = SecureRandom.hex
29
+ end while self.class.exists?(token: token)
30
+ end
31
+
32
+ def set_expiration
33
+ self.expires_at = Time.current + 30.days
34
+ end
35
+
36
+ end
37
+ end
@@ -1,46 +1,47 @@
1
- module SimpleApiKeyEngine
2
- class Authentication < ActiveRecord::Base
3
- store :auth_hash
4
-
5
- class << self
6
- def auth(request, &block)
7
- get_provider(request).auth(&block)
8
- end
9
-
10
- def activate(request, &block)
11
- authentication = auth(request, &block)
12
- return nil unless authentication
13
- SimpleApiKeyEngine::ApiKey.create!(user_type: authentication.user_type,
14
- user_ident: authentication.user_ident)
15
- end
16
-
17
- private
18
- def provider_classes
19
- ObjectSpace.each_object(Class).select{|klass| klass < SimpleApiKeyEngine::Providers::AbstractProvider}.reject { |klass|
20
- klass.priority.nil?
21
- }.sort_by { |klass| klass.priority }
22
- end
23
-
24
- private
25
- def get_provider(request)
26
- provider_class = provider_classes.find do |klass|
27
- klass.acceptable? request
28
- end
29
- raise 'Unknown authentication provider' unless provider_class
30
- provider_class.new request
31
- end
32
- end
33
-
34
- def user_class
35
- Class.const_get(user_type)
36
- end
37
-
38
- def user=(obj)
39
- self.user_ident = obj.to_param
40
- self.user_type = obj.class.name
41
- @user = obj
42
- end
43
-
44
- end
45
- end
46
-
1
+ module SimpleApiKeyEngine
2
+ class Authentication < ActiveRecord::Base
3
+ store :auth_hash
4
+
5
+ class << self
6
+ def auth(request, &block)
7
+ get_provider(request).auth(&block)
8
+ end
9
+
10
+ def activate(request, &block)
11
+ authentication = auth(request, &block)
12
+ return nil unless authentication
13
+ SimpleApiKeyEngine::ApiKey.create!(provider: authentication.provider,
14
+ user_type: authentication.user_type,
15
+ user_ident: authentication.user_ident)
16
+ end
17
+
18
+ private
19
+ def provider_classes
20
+ ObjectSpace.each_object(Class).select{|klass| klass < SimpleApiKeyEngine::Providers::AbstractProvider}.reject { |klass|
21
+ klass.priority.nil?
22
+ }.sort_by { |klass| klass.priority }
23
+ end
24
+
25
+ private
26
+ def get_provider(request)
27
+ provider_class = provider_classes.find do |klass|
28
+ klass.acceptable? request
29
+ end
30
+ raise 'Unknown authentication provider' unless provider_class
31
+ provider_class.new request
32
+ end
33
+ end
34
+
35
+ def user_class
36
+ Class.const_get(user_type)
37
+ end
38
+
39
+ def user=(obj)
40
+ self.user_ident = obj.to_param
41
+ self.user_type = obj.class.name
42
+ @user = obj
43
+ end
44
+
45
+ end
46
+ end
47
+
data/config/routes.rb CHANGED
@@ -1,2 +1,2 @@
1
- SimpleApiKeyEngine::Engine.routes.draw do
2
- end
1
+ SimpleApiKeyEngine::Engine.routes.draw do
2
+ end
@@ -1,12 +1,12 @@
1
- class CreateSimpleApiKeyEngineApiKeys < ActiveRecord::Migration
2
- def change
3
- create_table :simple_api_key_engine_api_keys do |t|
4
- t.string :token, index: true
5
- t.datetime :expires_at
6
- t.string :user_ident
7
- t.string :user_type
8
- t.timestamps
9
- t.index [:user_type, :user_ident], name: :simple_api_key_engine_api_keys_user
10
- end
11
- end
12
- end
1
+ class CreateSimpleApiKeyEngineApiKeys < ActiveRecord::Migration
2
+ def change
3
+ create_table :simple_api_key_engine_api_keys do |t|
4
+ t.string :token, index: true
5
+ t.datetime :expires_at
6
+ t.string :user_ident
7
+ t.string :user_type
8
+ t.timestamps
9
+ t.index [:user_type, :user_ident], name: :simple_api_key_engine_api_keys_user
10
+ end
11
+ end
12
+ end
@@ -1,15 +1,15 @@
1
- class CreateSimpleApiKeyEngineAuthentications < ActiveRecord::Migration
2
- def change
3
- create_table :simple_api_key_engine_authentications do |t|
4
- t.string :user_ident
5
- t.string :user_type
6
- t.string :provider
7
- t.string :uid
8
- t.string :token
9
- t.text :auth_hash
10
- t.timestamps
11
- t.index [:user_type, :user_ident], name: :simple_api_key_engine_authentications_user
12
- end
13
-
14
- end
15
- end
1
+ class CreateSimpleApiKeyEngineAuthentications < ActiveRecord::Migration
2
+ def change
3
+ create_table :simple_api_key_engine_authentications do |t|
4
+ t.string :user_ident
5
+ t.string :user_type
6
+ t.string :provider
7
+ t.string :uid
8
+ t.string :token
9
+ t.text :auth_hash
10
+ t.timestamps
11
+ t.index [:user_type, :user_ident], name: :simple_api_key_engine_authentications_user
12
+ end
13
+
14
+ end
15
+ end
@@ -1,7 +1,7 @@
1
- class AddExpiresAtToSimpleApiKeyEngineAuthentications < ActiveRecord::Migration
2
- def change
3
- change_table :simple_api_key_engine_authentications do |t|
4
- t.datetime :expires_at
5
- end
6
- end
7
- end
1
+ class AddExpiresAtToSimpleApiKeyEngineAuthentications < ActiveRecord::Migration
2
+ def change
3
+ change_table :simple_api_key_engine_authentications do |t|
4
+ t.datetime :expires_at
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class AddProviderToSimpleApiKeyEngineApiKeys < ActiveRecord::Migration
2
+ def change
3
+ change_table :simple_api_key_engine_api_keys do |t|
4
+ t.string :provider
5
+ end
6
+ end
7
+ end
@@ -1,17 +1,17 @@
1
- require "simpleapikeyengine/engine"
2
-
3
- module SimpleApiKeyEngine
4
- class << self
5
- attr_writer :configuration
6
- end
7
-
8
- def self.configuration
9
- @configuration ||= SimpleApiKeyEngine::Configuration.new
10
- end
11
-
12
- def self.configure
13
- yield(configuration)
14
- end
15
- end
16
-
17
- require "simpleapikeyengine/providers"
1
+ require "simpleapikeyengine/engine"
2
+
3
+ module SimpleApiKeyEngine
4
+ class << self
5
+ attr_writer :configuration
6
+ end
7
+
8
+ def self.configuration
9
+ @configuration ||= SimpleApiKeyEngine::Configuration.new
10
+ end
11
+
12
+ def self.configure
13
+ yield(configuration)
14
+ end
15
+ end
16
+
17
+ require "simpleapikeyengine/providers"
@@ -1,5 +1,5 @@
1
- module SimpleApiKeyEngine
2
- class Engine < ::Rails::Engine
3
- isolate_namespace SimpleApiKeyEngine
4
- end
5
- end
1
+ module SimpleApiKeyEngine
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace SimpleApiKeyEngine
4
+ end
5
+ end
@@ -1,9 +1,9 @@
1
- class SimpleApiKeyEngine::Configuration < Hash
2
- def method_missing(method, *params)
3
- if method.to_s =~ /^(.+)=$/
4
- self[$1.to_sym] = params.first
5
- else
6
- self[method.to_sym]
7
- end
8
- end
9
- end
1
+ class SimpleApiKeyEngine::Configuration < Hash
2
+ def method_missing(method, *params)
3
+ if method.to_s =~ /^(.+)=$/
4
+ self[$1.to_sym] = params.first
5
+ else
6
+ self[method.to_sym]
7
+ end
8
+ end
9
+ end
@@ -1,7 +1,7 @@
1
- module SimpleApiKeyEngine
2
- module Providers
3
-
4
- end
5
- end
6
-
7
- require 'simpleapikeyengine/providers/abstract_provider'
1
+ module SimpleApiKeyEngine
2
+ module Providers
3
+
4
+ end
5
+ end
6
+
7
+ require 'simpleapikeyengine/providers/abstract_provider'
@@ -1,67 +1,67 @@
1
- require 'simpleapikeyengine/provider_configuration'
2
-
3
- module SimpleApiKeyEngine::Providers
4
- class AbstractProvider
5
- class << self
6
- @@priorities = {}
7
- def priority(val=nil)
8
- if val.present?
9
- @@priorities[self] = val
10
- else
11
- @@priorities[self]
12
- end
13
- end
14
-
15
- def acceptable?(request)
16
- raise NotImplementedError
17
- end
18
- end
19
-
20
- def initialize(request)
21
- @request = request
22
- @params = request.params
23
- end
24
-
25
- def auth(&block)
26
- auth_hash = get_auth_hash!
27
- authentication = ::SimpleApiKeyEngine::Authentication.find_by_provider_and_uid(auth_hash[:provider], auth_hash[:uid])
28
- unless authentication
29
- authentication = ::SimpleApiKeyEngine::Authentication.new(provider: auth_hash[:provider],
30
- uid: auth_hash[:uid])
31
- authentication.user = block.call(auth_hash)
32
- end
33
- authentication.token = auth_hash[:credentials][:token]
34
- authentication.expires_at = auth_hash[:credentials][:expires] ?
35
- Time.at(auth_hash[:credentials][:expires_at]) : nil
36
- authentication.auth_hash = auth_hash
37
- authentication.save!
38
- after_authentication(authentication)
39
- return authentication
40
- end
41
-
42
- def get_auth_hash!
43
- # {
44
- # provider: 'provider_key',
45
- # uid: user_info['id'],
46
- # credentials: {
47
- # token: 'OAuth2Token',
48
- # expires_at: expires_at,
49
- # expires: expires
50
- # },
51
- # info: {
52
- # email: user_info['email'],
53
- # name: user_info['name']
54
- # },
55
- # extra: {
56
- # raw_info: user_info.to_h
57
- # }
58
- # }
59
- raise NotImplementedError
60
- end
61
-
62
- def after_authentication(authentication)
63
- true
64
- end
65
-
66
- end
67
- end
1
+ require 'simpleapikeyengine/provider_configuration'
2
+
3
+ module SimpleApiKeyEngine::Providers
4
+ class AbstractProvider
5
+ class << self
6
+ @@priorities = {}
7
+ def priority(val=nil)
8
+ if val.present?
9
+ @@priorities[self] = val
10
+ else
11
+ @@priorities[self]
12
+ end
13
+ end
14
+
15
+ def acceptable?(request)
16
+ raise NotImplementedError
17
+ end
18
+ end
19
+
20
+ def initialize(request)
21
+ @request = request
22
+ @params = request.params
23
+ end
24
+
25
+ def auth(&block)
26
+ auth_hash = get_auth_hash!
27
+ authentication = ::SimpleApiKeyEngine::Authentication.find_by_provider_and_uid(auth_hash[:provider], auth_hash[:uid])
28
+ unless authentication
29
+ authentication = ::SimpleApiKeyEngine::Authentication.new(provider: auth_hash[:provider],
30
+ uid: auth_hash[:uid])
31
+ authentication.user = block.call(auth_hash)
32
+ end
33
+ authentication.token = auth_hash[:credentials][:token]
34
+ authentication.expires_at = auth_hash[:credentials][:expires] ?
35
+ Time.at(auth_hash[:credentials][:expires_at]) : nil
36
+ authentication.auth_hash = auth_hash
37
+ authentication.save!
38
+ after_authentication(authentication)
39
+ return authentication
40
+ end
41
+
42
+ def get_auth_hash!
43
+ # {
44
+ # provider: 'provider_key',
45
+ # uid: user_info['id'],
46
+ # credentials: {
47
+ # token: 'OAuth2Token',
48
+ # expires_at: expires_at,
49
+ # expires: expires
50
+ # },
51
+ # info: {
52
+ # email: user_info['email'],
53
+ # name: user_info['name']
54
+ # },
55
+ # extra: {
56
+ # raw_info: user_info.to_h
57
+ # }
58
+ # }
59
+ raise NotImplementedError
60
+ end
61
+
62
+ def after_authentication(authentication)
63
+ true
64
+ end
65
+
66
+ end
67
+ end
@@ -1,3 +1,3 @@
1
- module SimpleApiKeyEngine
2
- VERSION = "0.1.0"
3
- end
1
+ module SimpleApiKeyEngine
2
+ VERSION = '0.1.1'
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simpleapikeyengine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuichi Takeuchi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-11 00:00:00.000000000 Z
11
+ date: 2015-05-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -68,6 +68,7 @@ files:
68
68
  - db/migrate/20141110041333_create_simple_api_key_engine_api_keys.rb
69
69
  - db/migrate/20141110041340_create_simple_api_key_engine_authentications.rb
70
70
  - db/migrate/20141117060900_add_expires_at_to_simple_api_key_engine_authentications.rb
71
+ - db/migrate/20150530161500_add_provider_to_simple_api_key_engine_api_keys.rb
71
72
  - lib/simpleapikeyengine.rb
72
73
  - lib/simpleapikeyengine/engine.rb
73
74
  - lib/simpleapikeyengine/provider_configuration.rb
@@ -94,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
95
  version: '0'
95
96
  requirements: []
96
97
  rubyforge_project:
97
- rubygems_version: 2.2.2
98
+ rubygems_version: 2.4.5
98
99
  signing_key:
99
100
  specification_version: 4
100
101
  summary: Simple API Key Engine.