sequel_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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fdc8cea8d123da0b5cd1b63b0aa769de4b69ce9917823f464089bdebd77558fe
4
+ data.tar.gz: 9120d637e4ae1e1613ba07494266f3e267edc79e67395e3a17570886216dbb73
5
+ SHA512:
6
+ metadata.gz: 19f7de1cb63d8315b73c9a7f95176bcdb9f54d2e3f1449e44fbd81a78375007bef543c3b22e3a8f5af2004aa3f38b963b3a2f7b17ad2163f1da9659a785d6512
7
+ data.tar.gz: 90ba8f192d7701304ce43ceda4cf0cccf987092a9fb621c0423cae67f22cb0456b2428f6a604a9c6b80ce50b10f82f992cd856d15701243d0752e8293055c49c
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 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 'sequel_simple_oauth2/mixins/access_token'
4
+ require 'sequel_simple_oauth2/mixins/access_grant'
5
+ require 'sequel_simple_oauth2/mixins/resource_owner'
6
+ require 'sequel_simple_oauth2/mixins/client'
@@ -0,0 +1,93 @@
1
+ module Sequel
2
+ module Simple
3
+ module OAuth2
4
+ # AccessGrant role mixin for Sequel.
5
+ # Includes all the required API, associations, validations and callbacks.
6
+ module AccessGrant
7
+ extend ActiveSupport::Concern
8
+
9
+ included do # rubocop:disable Metrics/BlockLength
10
+ plugin :validation_helpers
11
+ plugin :timestamps, force: true, update_on_create: true
12
+
13
+ # Returns associated Client instance.
14
+ #
15
+ # @return [Object] Client instance.
16
+ #
17
+ many_to_one :client, class: ::Simple::OAuth2.config.client_class_name, key: :client_id
18
+
19
+ # Returns associated ResourceOwner instance.
20
+ #
21
+ # @return [Object] ResourceOwner instance.
22
+ #
23
+ many_to_one :resource_owner, class: ::Simple::OAuth2.config.resource_owner_class_name, key: :resource_owner_id
24
+
25
+ def before_validation
26
+ if new?
27
+ # Generate token
28
+ generate_token
29
+
30
+ # Setup lifetime for `#code` value.
31
+ setup_expiration
32
+ end
33
+
34
+ super
35
+ end
36
+
37
+ # Required fields!
38
+ def validate
39
+ super
40
+ validates_presence %i[token client_id redirect_uri]
41
+ validates_unique %i[token]
42
+ end
43
+
44
+ # Searches for AccessGrant record with the specific `#token` value.
45
+ #
46
+ # @param token [#to_s] token value (any object that responds to `#to_s`).
47
+ #
48
+ # @return [Object, nil] AccessGrant object or nil if there is no record with such `#token`.
49
+ #
50
+ def self.by_token(token)
51
+ first(token: token.to_s)
52
+ end
53
+
54
+ # Create a new AccessGrant object.
55
+ #
56
+ # @param client [Object] Client instance.
57
+ # @param resource_owner [Object] ResourceOwner instance.
58
+ # @param redirect_uri [String] Redirect URI callback.
59
+ # @param scopes [String] set of scopes.
60
+ #
61
+ # @return [Object] AccessGrant object.
62
+ #
63
+ def self.create_for(client, resource_owner, redirect_uri, scopes = nil)
64
+ create(
65
+ client_id: client.id,
66
+ resource_owner_id: resource_owner.id,
67
+ redirect_uri: redirect_uri,
68
+ scopes: scopes
69
+ )
70
+ end
71
+
72
+ private
73
+
74
+ # Generate token
75
+ #
76
+ # @return token [String] string object.
77
+ #
78
+ def generate_token
79
+ self.token = ::Simple::OAuth2.config.token_generator.generate
80
+ end
81
+
82
+ # Set lifetime for `#code` value during creating a new record.
83
+ #
84
+ # @return clock [Time] time object.
85
+ #
86
+ def setup_expiration
87
+ self.expires_at = Time.now.utc + ::Simple::OAuth2.config.authorization_code_lifetime if expires_at.nil?
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,147 @@
1
+ module Sequel
2
+ module Simple
3
+ module OAuth2
4
+ # AccessToken role mixin for Sequel.
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
+ plugin :validation_helpers
11
+ plugin :timestamps, force: true, update_on_create: true
12
+
13
+ # Returns associated Client instance.
14
+ #
15
+ # @return [Object] Client instance.
16
+ #
17
+ many_to_one :client, class: ::Simple::OAuth2.config.client_class_name, key: :client_id
18
+
19
+ # Returns associated ResourceOwner instance.
20
+ #
21
+ # @return [Object] ResourceOwner instance.
22
+ #
23
+ many_to_one :resource_owner, class: ::Simple::OAuth2.config.resource_owner_class_name,
24
+ key: :resource_owner_id
25
+
26
+ def before_validation
27
+ if new?
28
+ # Setup lifetime for `#token` value.
29
+ setup_expiration
30
+
31
+ # Generate tokens
32
+ generate_tokens
33
+ end
34
+
35
+ super
36
+ end
37
+
38
+ # Required fields!
39
+ def validate
40
+ super
41
+ validates_presence :token
42
+ validates_unique :token
43
+ end
44
+
45
+ class << self
46
+ # Searches for AccessToken record with the specific `#token` value.
47
+ #
48
+ # @param token [#to_s] token value (any object that responds to `#to_s`).
49
+ #
50
+ # @return [Object, nil] AccessToken object or nil if there is no record with such `#token`.
51
+ #
52
+ def by_token(token)
53
+ first(token: token.to_s)
54
+ end
55
+
56
+ # Returns an instance of the AccessToken with specific `#refresh_token` value.
57
+ #
58
+ # @param refresh_token [#to_s] refresh token value (any object that responds to `#to_s`).
59
+ #
60
+ # @return [Object, nil] AccessToken object or nil if there is no record with such `#refresh_token`.
61
+ #
62
+ def by_refresh_token(refresh_token)
63
+ first(refresh_token: refresh_token.to_s)
64
+ end
65
+
66
+ # Create a new AccessToken object.
67
+ #
68
+ # @param client [Object] Client instance.
69
+ # @param resource_owner [Object] ResourceOwner instance.
70
+ # @param scopes [String] set of scopes.
71
+ #
72
+ # @return [Object] AccessToken object.
73
+ #
74
+ def create_for(client, resource_owner, scopes = nil)
75
+ create(
76
+ client_id: client.id,
77
+ resource_owner_id: resource_owner.id,
78
+ scopes: scopes
79
+ )
80
+ end
81
+ end
82
+
83
+ # Indicates whether the object is expired (`#expires_at` present and expiration time has come).
84
+ #
85
+ # @return [Boolean] true if object expired and false in other case.
86
+ #
87
+ def expired?
88
+ expires_at && Time.now.utc > expires_at
89
+ end
90
+
91
+ # Indicates whether the object has been revoked.
92
+ #
93
+ # @return [Boolean] true if revoked, false in other case.
94
+ #
95
+ def revoked?
96
+ revoked_at && revoked_at <= Time.now.utc
97
+ end
98
+
99
+ # Revokes the object (updates `:revoked_at` attribute setting its value to the specific time).
100
+ #
101
+ # @param revoked_at [Time] time object.
102
+ #
103
+ # @return [Object] AccessToken object or raise Sequel::Error::DocumentInvalid.
104
+ #
105
+ def revoke!(revoked_at = Time.now)
106
+ set(revoked_at: revoked_at.utc)
107
+ save(columns: [:revoked_at], validate: false)
108
+ end
109
+
110
+ # Exposes token object to Bearer token.
111
+ #
112
+ # @return [Hash] bearer token instance.
113
+ #
114
+ def to_bearer_token
115
+ {
116
+ access_token: token,
117
+ expires_in: expires_at && ::Simple::OAuth2.config.access_token_lifetime.to_i,
118
+ refresh_token: refresh_token,
119
+ scope: scopes
120
+ }
121
+ end
122
+
123
+ private
124
+
125
+ # Generate tokens
126
+ #
127
+ # @return token [String] string object.
128
+ # @return refresh_token [String] string object.
129
+ #
130
+ def generate_tokens
131
+ self.token = ::Simple::OAuth2.config.token_generator.generate if token.blank?
132
+ self.refresh_token = ::Simple::OAuth2::UniqToken.generate if ::Simple::OAuth2.config.issue_refresh_token
133
+ end
134
+
135
+ # Set lifetime for token value during creating a new record.
136
+ #
137
+ # @return clock [Time] time object.
138
+ #
139
+ def setup_expiration
140
+ expires_in = ::Simple::OAuth2.config.access_token_lifetime.to_i
141
+ self.expires_at = Time.now.utc + expires_in if expires_at.nil? && !expires_in.nil?
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,65 @@
1
+ module Sequel
2
+ module Simple
3
+ module OAuth2
4
+ # Client role mixin for Sequel.
5
+ # Includes all the required API, associations, validations and callbacks.
6
+ module Client
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ plugin :validation_helpers
11
+ plugin :timestamps, force: true, update_on_create: true
12
+ plugin :association_dependencies
13
+
14
+ # Returns associated AccessToken array.
15
+ #
16
+ # @return [Array<Object>] AccessToken array.
17
+ #
18
+ one_to_many :access_tokens, class: ::Simple::OAuth2.config.access_token_class_name, key: :client_id
19
+ add_association_dependencies access_tokens: :delete
20
+
21
+ # Returns associated AccessGrant array.
22
+ #
23
+ # @return [Array<Object>] AccessGrant array.
24
+ #
25
+ one_to_many :access_grants, class: ::Simple::OAuth2.config.access_grant_class_name, key: :client_id
26
+
27
+ def before_validation
28
+ # Generate tokens
29
+ generate_tokens if new?
30
+ super
31
+ end
32
+
33
+ # Required fields!
34
+ def validate
35
+ super
36
+ validates_presence %i[key secret]
37
+ validates_unique %i[key secret]
38
+ end
39
+
40
+ # Searches for Client record with the specific `#key` value.
41
+ #
42
+ # @param key [#to_s] key value (any object that responds to `#to_s`).
43
+ #
44
+ # @return [Object, nil] Client object or nil if there is no record with such `#key`.
45
+ #
46
+ def self.by_key(key)
47
+ first(key: key.to_s)
48
+ end
49
+
50
+ private
51
+
52
+ # Generate tokens
53
+ #
54
+ # @return token [String] string object.
55
+ # @return refresh_token [String] string object.
56
+ #
57
+ def generate_tokens
58
+ self.key = ::Simple::OAuth2::UniqToken.generate if key.blank?
59
+ self.secret = ::Simple::OAuth2::UniqToken.generate if secret.blank?
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,28 @@
1
+ module Sequel
2
+ module Simple
3
+ module OAuth2
4
+ # ResourceOwner role mixin for Sequel.
5
+ # Includes all the required API, associations, validations and callbacks
6
+ module ResourceOwner
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ plugin :timestamps, force: true, update_on_create: true
11
+
12
+ # Searches for ResourceOwner record with the specific params.
13
+ #
14
+ # @param _client [Object] Client instance.
15
+ # @param username [String, #to_s] username value (any object that responds to `#to_s`).
16
+ # @param password [String] password value.
17
+ #
18
+ # @return [Object, nil] ResourceOwner object or nil if there is no record with such params.
19
+ #
20
+ def self.oauth_authenticate(_client, username, password)
21
+ user = find(username: username.to_s)
22
+ user if user && user.encrypted_password == password
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,29 @@
1
+ module Sequel
2
+ module Simple
3
+ # Semantic versioning
4
+ module OAuth2
5
+ # SequelSimpleOAuth2 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
+ # SequelSimpleOAuth2 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: sequel_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: 2018-02-18 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: Sequel mixin for SimpleOAuth2 authorization
68
+ email: volodimir.partytskyi@gmail.com
69
+ executables: []
70
+ extensions: []
71
+ extra_rdoc_files: []
72
+ files:
73
+ - LICENSE
74
+ - lib/sequel_simple_oauth2.rb
75
+ - lib/sequel_simple_oauth2/mixins/access_grant.rb
76
+ - lib/sequel_simple_oauth2/mixins/access_token.rb
77
+ - lib/sequel_simple_oauth2/mixins/client.rb
78
+ - lib/sequel_simple_oauth2/mixins/resource_owner.rb
79
+ - lib/sequel_simple_oauth2/version.rb
80
+ homepage: https://github.com/simple-oauth2/sequel_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 Sequel ORM
104
+ test_files: []