simpleapikeyengine 0.0.3

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: 46e4d64b4441a7fed5b90a00f43627554a4dacc8
4
+ data.tar.gz: 10ea574bd0be806f78b9b868c4e2db550b16f49a
5
+ SHA512:
6
+ metadata.gz: 170e7fe0d1e71a68e05e14ca292b0476ef6da8f2c54edccf7d1bb14c7a9c1f079b214a68ba587b6a460c160daec7cabcff9ce2118670ccc2f599569646bc3941
7
+ data.tar.gz: e7be44ebad81c28a8a5824d8685332a0c1acbf9b3571a8703a966e12e86dd9c1601f335d3745a20715e0b6d6006e1834a861a2c296851a484dcbca2b13bfb2f2
data/MIT-LICENSE ADDED
@@ -0,0 +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.
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ simpleapikeyengine
2
+ =====================
3
+
4
+ Simple API Key Engine.
data/Rakefile ADDED
@@ -0,0 +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
+
@@ -0,0 +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
@@ -0,0 +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
+
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ SimpleApiKeyEngine::Engine.routes.draw do
2
+ end
@@ -0,0 +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
@@ -0,0 +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
@@ -0,0 +1,5 @@
1
+ module SimpleApiKeyEngine
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace SimpleApiKeyEngine
4
+ end
5
+ end
@@ -0,0 +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
@@ -0,0 +1,59 @@
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.auth_hash = auth_hash
34
+ authentication.save!
35
+ return authentication
36
+ end
37
+
38
+ def get_auth_hash!
39
+ # {
40
+ # provider: 'provider_key',
41
+ # uid: user_info['id'],
42
+ # credentials: {
43
+ # token: 'OAuth2Token',
44
+ # expires_at: expires_at,
45
+ # expires: expires
46
+ # },
47
+ # info: {
48
+ # email: user_info['email'],
49
+ # name: user_info['name']
50
+ # },
51
+ # extra: {
52
+ # raw_info: user_info.to_h
53
+ # }
54
+ # }
55
+ raise NotImplementedError
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,7 @@
1
+ module SimpleApiKeyEngine
2
+ module Providers
3
+
4
+ end
5
+ end
6
+
7
+ require 'simpleapikeyengine/providers/abstract_provider'
@@ -0,0 +1,3 @@
1
+ module SimpleApiKeyEngine
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +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"
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simpleapikeyengine
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Yuichi Takeuchi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
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
+ description: "(Description for simpleapikeyengine)"
56
+ email:
57
+ - uzuki05@takeyu-web.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - MIT-LICENSE
63
+ - README.md
64
+ - Rakefile
65
+ - app/models/simple_api_key_engine/api_key.rb
66
+ - app/models/simple_api_key_engine/authentication.rb
67
+ - config/routes.rb
68
+ - db/migrate/20141110041333_create_simple_api_key_engine_api_keys.rb
69
+ - db/migrate/20141110041340_create_simple_api_key_engine_authentications.rb
70
+ - lib/simpleapikeyengine.rb
71
+ - lib/simpleapikeyengine/engine.rb
72
+ - lib/simpleapikeyengine/provider_configuration.rb
73
+ - lib/simpleapikeyengine/providers.rb
74
+ - lib/simpleapikeyengine/providers/abstract_provider.rb
75
+ - lib/simpleapikeyengine/version.rb
76
+ homepage: https://github.com/takeyuweb/simpleapikeyengine
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.2.2
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Simple API Key Engine.
100
+ test_files: []