simpleapikeyengine 0.0.7 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8070d5d66770bf384b261321ccf434bc83ca6ec7
4
- data.tar.gz: a48cd9e4a9eede6fbb6f92a78beb1bd85eb4ecc9
3
+ metadata.gz: 536016096a163e0a2561c8352de594f35cf25a9f
4
+ data.tar.gz: 5624f9f5ca889ac170588bd336c309d60092bf8b
5
5
  SHA512:
6
- metadata.gz: 783b22cc87e54a08050a913a70802a6917311d07fc5e51c616aa69afa0311e8214098c9cfe95efdf8ab2531f79bd49745a22e7496c75b9010dbed1dc232f91a6
7
- data.tar.gz: 4da1741a188c6c4b06534eea73ed19d33eab80efd0ab7d1a6aa51983b383518a71252009728df5cea7e48d1a7cfa85707a92174d221a0831e8bb5da47465264d
6
+ metadata.gz: 8c84fe3a41311e386485db06438e5e9c3f28c6e13b2151b5f94200a337f41a5472cf15ba00a6d16601d7ae01555439650bd6859e0bdaa7c6222a48e5350189b7
7
+ data.tar.gz: eec86b2c43670628520600ef751ee55cb01de78c735b99e941d52bf7406087efbc114911c5c62750a88aa042487ee1589ba5f9ed784955bf880ec160e2542f63
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,46 @@
1
- module SimpleApiKeyEngine
2
- class Authentication < ActiveRecord::Base
3
- store :auth_hash
4
-
5
- class << self
6
- def auth(auth_hash, &block)
7
- get_provider(auth_hash).auth(&block)
8
- end
9
-
10
- def activate(auth_hash, &block)
11
- authentication = auth(auth_hash, &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(auth_hash)
26
- provider_class = provider_classes.find do |klass|
27
- klass.acceptable? auth_hash
28
- end
29
- raise 'Unknown authentication provider' unless provider_class
30
- provider_class.new auth_hash
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!(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
+
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
@@ -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,61 +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?(auth_hash)
16
- raise NotImplementedError
17
- end
18
- end
19
-
20
- def initialize(params)
21
- @params = params
22
- end
23
-
24
- def auth(&block)
25
- auth_hash = get_auth_hash!
26
- authentication = ::SimpleApiKeyEngine::Authentication.find_by_provider_and_uid(auth_hash[:provider], auth_hash[:uid])
27
- unless authentication
28
- authentication = ::SimpleApiKeyEngine::Authentication.new(provider: auth_hash[:provider],
29
- uid: auth_hash[:uid])
30
- authentication.user = block.call(auth_hash)
31
- end
32
- authentication.token = auth_hash[:credentials][:token]
33
- authentication.expires_at = auth_hash[:credentials][:expires] ?
34
- Time.at(auth_hash[:credentials][:expires_at]) : nil
35
- authentication.auth_hash = auth_hash
36
- authentication.save!
37
- return authentication
38
- end
39
-
40
- def get_auth_hash!
41
- # {
42
- # provider: 'provider_key',
43
- # uid: user_info['id'],
44
- # credentials: {
45
- # token: 'OAuth2Token',
46
- # expires_at: expires_at,
47
- # expires: expires
48
- # },
49
- # info: {
50
- # email: user_info['email'],
51
- # name: user_info['name']
52
- # },
53
- # extra: {
54
- # raw_info: user_info.to_h
55
- # }
56
- # }
57
- raise NotImplementedError
58
- end
59
-
60
- end
61
- 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.0.7"
3
- end
1
+ module SimpleApiKeyEngine
2
+ VERSION = "0.1.0"
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.0.7
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuichi Takeuchi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-17 00:00:00.000000000 Z
11
+ date: 2015-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails