activerecord_simple_oauth2 0.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e1b9d315283a5d0e8765086620eb7e3bc934e43ce1030a6b530d66477fca4785
4
+ data.tar.gz: 2e77cc93bbed2bacd7d001365499b1e608d8da1b1994f5bd3d69b9f9f5db05d6
5
+ SHA512:
6
+ metadata.gz: 7ef4322ff5b2e509c4e0d89a5ef05236c1e7d15b195c441e1a2050a339e0c2a743d94fbe6d54ad525adb4f01858f10ec69dd91735079691f76a89efbd8300219
7
+ data.tar.gz: fee3651f816d3c235df49de59ba6a7b3975f1f867ffe9a4602c5a2d0535a0d543e21b85020bb5cc728390bc8159b39261fbe7ee5607edba5a536e31348db60e7
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Simple OAuth2
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,6 @@
1
+ require 'simple_oauth2'
2
+
3
+ require 'activerecord_simple_oauth2/mixins/access_token'
4
+ require 'activerecord_simple_oauth2/mixins/access_grant'
5
+ require 'activerecord_simple_oauth2/mixins/resource_owner'
6
+ require 'activerecord_simple_oauth2/mixins/client'
@@ -0,0 +1,81 @@
1
+ module ActiveRecord
2
+ module Simple
3
+ module OAuth2
4
+ # AccessGrant role mixin for ActiveRecord.
5
+ # Includes all the required API, associations, validations and callbacks.
6
+ module AccessGrant
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ # Returns associated Client instance.
11
+ #
12
+ # @return [Object] Client instance.
13
+ #
14
+ belongs_to :client, class_name: ::Simple::OAuth2.config.client_class_name, foreign_key: :client_id
15
+
16
+ # Returns associated ResourceOwner instance.
17
+ #
18
+ # @return [Object] ResourceOwner instance.
19
+ #
20
+ belongs_to :resource_owner, class_name: ::Simple::OAuth2.config.resource_owner_class_name,
21
+ foreign_key: :resource_owner_id
22
+
23
+ # Required fields!
24
+ validates :client_id, :redirect_uri, :token, presence: true
25
+ validates :token, uniqueness: true
26
+
27
+ # Generate token
28
+ before_validation :generate_token, on: :create
29
+ # Setup lifetime for `#code` value.
30
+ before_validation :setup_expiration, on: :create
31
+
32
+ # Searches for AccessGrant record with the specific `#token` value.
33
+ #
34
+ # @param token [#to_s] token value (any object that responds to `#to_s`).
35
+ #
36
+ # @return [Object, nil] AccessGrant object or nil if there is no record with such `#token`.
37
+ #
38
+ def self.by_token(token)
39
+ where(token: token.to_s).first
40
+ end
41
+
42
+ # Create a new AccessGrant object.
43
+ #
44
+ # @param client [Object] Client instance.
45
+ # @param resource_owner [Object] ResourceOwner instance.
46
+ # @param redirect_uri [String] Redirect URI callback.
47
+ # @param scopes [String] set of scopes.
48
+ #
49
+ # @return [Object] AccessGrant object.
50
+ #
51
+ def self.create_for(client, resource_owner, redirect_uri, scopes = nil)
52
+ create(
53
+ client_id: client.id,
54
+ resource_owner_id: resource_owner.id,
55
+ redirect_uri: redirect_uri,
56
+ scopes: scopes
57
+ )
58
+ end
59
+
60
+ private
61
+
62
+ # Generate token
63
+ #
64
+ # @return token [String] string object.
65
+ #
66
+ def generate_token
67
+ self.token = ::Simple::OAuth2.config.token_generator.generate
68
+ end
69
+
70
+ # Set lifetime for `#code` value during creating a new record.
71
+ #
72
+ # @return clock [Time] time object.
73
+ #
74
+ def setup_expiration
75
+ self.expires_at = Time.now.utc + ::Simple::OAuth2.config.authorization_code_lifetime if expires_at.nil?
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,132 @@
1
+ module ActiveRecord
2
+ module Simple
3
+ module OAuth2
4
+ # AccessToken role mixin for ActiveRecord.
5
+ # Includes all the required API, associations, validations and callbacks.
6
+ module AccessToken
7
+ extend ActiveSupport::Concern
8
+
9
+ included do # rubocop:disable Metrics/BlockLength
10
+ # Returns associated Client instance.
11
+ #
12
+ # @return [Object] Client instance.
13
+ #
14
+ belongs_to :client, class_name: ::Simple::OAuth2.config.client_class_name, foreign_key: :client_id
15
+
16
+ # Returns associated ResourceOwner instance.
17
+ #
18
+ # @return [Object] ResourceOwner instance.
19
+ #
20
+ belongs_to :resource_owner, class_name: ::Simple::OAuth2.config.resource_owner_class_name,
21
+ foreign_key: :resource_owner_id
22
+
23
+ # Required field!
24
+ validates :token, presence: true, uniqueness: true
25
+
26
+ # Generate tokens
27
+ before_validation :generate_tokens, on: :create
28
+ # Setup lifetime for `#token` value.
29
+ before_validation :setup_expiration, on: :create
30
+
31
+ class << self
32
+ # Searches for AccessToken record with the specific `#token` value.
33
+ #
34
+ # @param token [#to_s] token value (any object that responds to `#to_s`).
35
+ #
36
+ # @return [Object, nil] AccessToken object or nil if there is no record with such `#token`.
37
+ #
38
+ def by_token(token)
39
+ where(token: token.to_s).first
40
+ end
41
+
42
+ # Returns an instance of the AccessToken with specific `#refresh_token` value.
43
+ #
44
+ # @param refresh_token [#to_s] refresh token value (any object that responds to `#to_s`).
45
+ #
46
+ # @return [Object, nil] AccessToken object or nil if there is no record with such `#refresh_token`.
47
+ #
48
+ def by_refresh_token(refresh_token)
49
+ where(refresh_token: refresh_token.to_s).first
50
+ end
51
+
52
+ # Create a new AccessToken object.
53
+ #
54
+ # @param client [Object] Client instance.
55
+ # @param resource_owner [Object] ResourceOwner instance.
56
+ # @param scopes [String] set of scopes.
57
+ #
58
+ # @return [Object] AccessToken object.
59
+ #
60
+ def create_for(client, resource_owner, scopes = nil)
61
+ create(
62
+ client_id: client.id,
63
+ resource_owner_id: resource_owner.id,
64
+ scopes: scopes
65
+ )
66
+ end
67
+ end
68
+
69
+ # Indicates whether the object is expired (`#expires_at` present and expiration time has come).
70
+ #
71
+ # @return [Boolean] true if object expired and false in other case.
72
+ #
73
+ def expired?
74
+ expires_at && Time.now.utc > expires_at
75
+ end
76
+
77
+ # Indicates whether the object has been revoked.
78
+ #
79
+ # @return [Boolean] true if revoked, false in other case.
80
+ #
81
+ def revoked?
82
+ revoked_at && revoked_at <= Time.now.utc
83
+ end
84
+
85
+ # Revokes the object (updates `:revoked_at` attribute setting its value to the specific time).
86
+ #
87
+ # @param revoked_at [Time] time object.
88
+ #
89
+ # @return [Object] AccessToken object or raise ActiveRecord::Error::DocumentInvalid.
90
+ #
91
+ def revoke!(revoked_at = Time.now)
92
+ update_column(:revoked_at, revoked_at.utc)
93
+ end
94
+
95
+ # Exposes token object to Bearer token.
96
+ #
97
+ # @return [Hash] bearer token instance.
98
+ #
99
+ def to_bearer_token
100
+ {
101
+ access_token: token,
102
+ expires_in: expires_at && ::Simple::OAuth2.config.access_token_lifetime.to_i,
103
+ refresh_token: refresh_token,
104
+ scope: scopes
105
+ }
106
+ end
107
+
108
+ private
109
+
110
+ # Generate tokens
111
+ #
112
+ # @return token [String] string object.
113
+ # @return refresh_token [String] string object.
114
+ #
115
+ def generate_tokens
116
+ self.token = ::Simple::OAuth2.config.token_generator.generate if token.blank?
117
+ self.refresh_token = ::Simple::OAuth2::UniqToken.generate if ::Simple::OAuth2.config.issue_refresh_token
118
+ end
119
+
120
+ # Set lifetime for token value during creating a new record.
121
+ #
122
+ # @return clock [Time] time object.
123
+ #
124
+ def setup_expiration
125
+ expires_in = ::Simple::OAuth2.config.access_token_lifetime.to_i
126
+ self.expires_at = Time.now.utc + expires_in if expires_at.nil? && !expires_in.nil?
127
+ end
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,55 @@
1
+ module ActiveRecord
2
+ module Simple
3
+ module OAuth2
4
+ # Client role mixin for ActiveRecord.
5
+ # Includes all the required API, associations, validations and callbacks.
6
+ module Client
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ # Returns associated AccessToken array.
11
+ #
12
+ # @return [Array<Object>] AccessToken array.
13
+ #
14
+ has_many :access_tokens, class_name: ::Simple::OAuth2.config.access_token_class_name,
15
+ foreign_key: :client_id,
16
+ dependent: :destroy
17
+
18
+ # Returns associated AccessGrant array.
19
+ #
20
+ # @return [Array<Object>] AccessGrant array.
21
+ #
22
+ has_many :access_grants, class_name: ::Simple::OAuth2.config.access_grant_class_name, foreign_key: :client_id
23
+
24
+ # Required fields!
25
+ validates :key, :secret, presence: true, uniqueness: true
26
+
27
+ # Generate tokens
28
+ before_validation :generate_tokens, on: :create
29
+
30
+ # Searches for Client record with the specific `#key` value.
31
+ #
32
+ # @param key [#to_s] key value (any object that responds to `#to_s`).
33
+ #
34
+ # @return [Object, nil] Client object or nil if there is no record with such `#key`.
35
+ #
36
+ def self.by_key(key)
37
+ where(key: key.to_s).first
38
+ end
39
+
40
+ private
41
+
42
+ # Generate tokens
43
+ #
44
+ # @return token [String] string object.
45
+ # @return refresh_token [String] string object.
46
+ #
47
+ def generate_tokens
48
+ self.key = ::Simple::OAuth2::UniqToken.generate if key.blank?
49
+ self.secret = ::Simple::OAuth2::UniqToken.generate if secret.blank?
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,26 @@
1
+ module ActiveRecord
2
+ module Simple
3
+ module OAuth2
4
+ # ResourceOwner role mixin for ActiveRecord.
5
+ # Includes all the required API, associations, validations and callbacks
6
+ module ResourceOwner
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ # Searches for ResourceOwner record with the specific params.
11
+ #
12
+ # @param _client [Object] Client instance.
13
+ # @param username [String, #to_s] username value (any object that responds to `#to_s`).
14
+ # @param password [String] password value.
15
+ #
16
+ # @return [Object, nil] ResourceOwner object or nil if there is no record with such params.
17
+ #
18
+ def self.oauth_authenticate(_client, username, password)
19
+ user = where(username: username.to_s).first
20
+ user if user && user.encrypted_password == password
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,29 @@
1
+ module ActiveRecord
2
+ module Simple
3
+ # Semantic versioning
4
+ module OAuth2
5
+ # ActiveRecordSimpleOAuth2 version
6
+ #
7
+ # @return [Gem::Version] version of the gem
8
+ #
9
+ def self.gem_version
10
+ Gem::Version.new VERSION::STRING
11
+ end
12
+
13
+ # ActiveRecordSimpleOAuth2 semantic versioning module.
14
+ # Contains detailed info about gem version
15
+ module VERSION
16
+ # Level changes for implementation level detail changes, such as small bug fixes
17
+ PATCH = 0
18
+ # Level changes for any backwards compatible API changes, such as new functionality/features
19
+ MINOR = 0
20
+ # Level changes for backwards incompatible API changes,
21
+ # such as changes that will break existing users code if they update
22
+ MAJOR = 0
23
+
24
+ # Full gem version string
25
+ STRING = [MAJOR, MINOR, PATCH].join('.')
26
+ end
27
+ end
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord_simple_oauth2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Volodimir Partytskyi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: simple_oauth2
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec-rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 3.6.0
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: 3.6.0
37
+ type: :development
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: 3.6.0
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 3.6.0
47
+ - !ruby/object:Gem::Dependency
48
+ name: database_cleaner
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: 1.5.0
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 1.5.0
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: 1.5.0
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 1.5.0
67
+ description: ActiveRecord mixin for SimpleOAuth2 authorization
68
+ email: volodimir.partytskyi@gmail.com
69
+ executables: []
70
+ extensions: []
71
+ extra_rdoc_files: []
72
+ files:
73
+ - LICENSE
74
+ - lib/activerecord_simple_oauth2.rb
75
+ - lib/activerecord_simple_oauth2/mixins/access_grant.rb
76
+ - lib/activerecord_simple_oauth2/mixins/access_token.rb
77
+ - lib/activerecord_simple_oauth2/mixins/client.rb
78
+ - lib/activerecord_simple_oauth2/mixins/resource_owner.rb
79
+ - lib/activerecord_simple_oauth2/version.rb
80
+ homepage: https://github.com/simple-oauth2/activerecord_simple_oauth2
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 2.2.2
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.7.3
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Mixin for ActiveRecord ORM
104
+ test_files: []