nobrainer_simple_oauth2 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 955c0bdff34ddd7464485fcc5f7d4b3d2394d525
4
+ data.tar.gz: 71ccae573dbb76e7d18b0c7450b02badd230756d
5
+ SHA512:
6
+ metadata.gz: c090ea2135222a63d4dff028dee177e97456bcb1eca749b77c1088d12dcc654c6e41be5715f83f58fd93b77ce9668328c4b0874acd897b76d01677cf7c59371e
7
+ data.tar.gz: 666aa53c23421d3dd7fea8f47c9e7947880242cf397da770385640e63289aa3f4832e49b3d7c776506b168105099ec0c3c280049dfe711370ab2e1a916ceee68
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Volodimir Partytskyi
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,11 @@
1
+ require 'simple_oauth2'
2
+
3
+ require 'nobrainer_simple_oauth2/mixins/concerns/fields/resource_owner'
4
+ require 'nobrainer_simple_oauth2/mixins/concerns/fields/client'
5
+ require 'nobrainer_simple_oauth2/mixins/concerns/fields/access_token'
6
+ require 'nobrainer_simple_oauth2/mixins/concerns/fields/access_grant'
7
+
8
+ require 'nobrainer_simple_oauth2/mixins/access_token'
9
+ require 'nobrainer_simple_oauth2/mixins/access_grant'
10
+ require 'nobrainer_simple_oauth2/mixins/resource_owner'
11
+ require 'nobrainer_simple_oauth2/mixins/client'
@@ -0,0 +1,58 @@
1
+ module NoBrainer
2
+ module Simple
3
+ module OAuth2
4
+ # Includes all the required API, associations, validations and callbacks
5
+ module AccessGrant
6
+ extend ActiveSupport::Concern
7
+
8
+ include NoBrainer::Simple::OAuth2::Fields::AccessGrant
9
+
10
+ included do
11
+ belongs_to :client, class_name: ::Simple::OAuth2.config.client_class_name,
12
+ foreign_key: :client_id, primary_key: :id
13
+ belongs_to :resource_owner, class_name: ::Simple::OAuth2.config.resource_owner_class_name,
14
+ foreign_key: :resource_owner_id, primary_key: :id
15
+
16
+ before_save { self.updated_at = Time.now }
17
+ before_validation :setup_expiration, if: :new_record?
18
+
19
+ # Searches for AccessGrant record with the specific token value
20
+ #
21
+ # @param token [#to_s] token value (any object that responds to `#to_s`)
22
+ #
23
+ # @return [AccessGrant, nil] AccessGrant object or nil if there is no record with such `#token`
24
+ #
25
+ scope(:by_token) { |token| where(token: token.to_s).first }
26
+
27
+ # Create a new AccessGrant object
28
+ #
29
+ # @param client [Object] Client instance
30
+ # @param resource_owner [Object] ResourceOwner instance
31
+ # @param redirect_uri [String] Redirect URI callback
32
+ # @param scopes [String] set of scopes
33
+ #
34
+ # @return [AccessGrant] AccessGrant object
35
+ #
36
+ def self.create_for(client, resource_owner, redirect_uri, scopes = nil)
37
+ create(
38
+ client_id: client.id,
39
+ resource_owner_id: resource_owner.id,
40
+ redirect_uri: redirect_uri,
41
+ scopes: scopes
42
+ )
43
+ end
44
+
45
+ private
46
+
47
+ # Set lifetime for code value during creating a new record
48
+ #
49
+ # @return clock [Time] time object
50
+ #
51
+ def setup_expiration
52
+ self.expires_at = Time.now.utc + ::Simple::OAuth2.config.authorization_code_lifetime if expires_at.nil?
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,102 @@
1
+ module NoBrainer
2
+ module Simple
3
+ module OAuth2
4
+ # Includes all the required API, associations, validations and callbacks
5
+ module AccessToken
6
+ extend ActiveSupport::Concern
7
+
8
+ include NoBrainer::Simple::OAuth2::Fields::AccessToken
9
+
10
+ included do # rubocop:disable Metrics/BlockLength
11
+ before_save { self.updated_at = Time.now }
12
+ before_validation :setup_expiration, if: :new_record?
13
+
14
+ belongs_to :client, class_name: ::Simple::OAuth2.config.client_class_name,
15
+ foreign_key: :client_id, primary_key: :id
16
+ belongs_to :resource_owner, class_name: ::Simple::OAuth2.config.resource_owner_class_name,
17
+ foreign_key: :resource_owner_id, primary_key: :id
18
+
19
+ # Searches for AccessToken record with the specific `#token` value
20
+ #
21
+ # @param token [#to_s] token value (any object that responds to `#to_s`)
22
+ #
23
+ # @return [AccessToken, nil] AccessToken object or nil if there is no record with such `#token`
24
+ #
25
+ scope(:by_token) { |token| where(token: token.to_s).first }
26
+
27
+ # Returns an instance of the AccessToken with specific `#refresh_token` value
28
+ #
29
+ # @param refresh_token [#to_s] refresh token value (any object that responds to `#to_s`)
30
+ #
31
+ # @return [AccessToken, nil] AccessToken object or nil if there is no record with such `#refresh_token`
32
+ #
33
+ scope(:by_refresh_token) { |refresh_token| where(refresh_token: refresh_token.to_s).first }
34
+
35
+ # Create a new AccessToken object
36
+ #
37
+ # @param client [Object] Client instance
38
+ # @param resource_owner [Object] ResourceOwner instance
39
+ # @param scopes [String] set of scopes
40
+ #
41
+ # @return [AccessToken] AccessToken object
42
+ #
43
+ def self.create_for(client, resource_owner, scopes = nil)
44
+ create(
45
+ client_id: client.id,
46
+ resource_owner_id: resource_owner.id,
47
+ scopes: scopes
48
+ )
49
+ end
50
+
51
+ # Indicates whether the object is expired (`#expires_at` present and expiration time has come)
52
+ #
53
+ # @return [Boolean] true if object expired and false in other case
54
+ #
55
+ def expired?
56
+ expires_at && Time.now.utc > expires_at
57
+ end
58
+
59
+ # Indicates whether the object has been revoked
60
+ #
61
+ # @return [Boolean] true if revoked, false in other case
62
+ #
63
+ def revoked?
64
+ revoked_at && revoked_at <= Time.now.utc
65
+ end
66
+
67
+ # Revokes the object (updates `:revoked_at` attribute setting its value to the specific time)
68
+ #
69
+ # @param clock [Time] time object
70
+ #
71
+ def revoke!(revoked_at = Time.now.utc)
72
+ update!(revoked_at: revoked_at)
73
+ end
74
+
75
+ # Exposes token object to Bearer token
76
+ #
77
+ # @return [Hash] bearer token instance
78
+ #
79
+ def to_bearer_token
80
+ {
81
+ access_token: token,
82
+ expires_in: expires_at && ::Simple::OAuth2.config.access_token_lifetime.to_i,
83
+ refresh_token: refresh_token,
84
+ scope: scopes
85
+ }
86
+ end
87
+
88
+ private
89
+
90
+ # Set lifetime for token value during creating a new record
91
+ #
92
+ # @return clock [Time] time object
93
+ #
94
+ def setup_expiration
95
+ expires_in = ::Simple::OAuth2.config.access_token_lifetime.to_i
96
+ self.expires_at = Time.now.utc + expires_in if expires_at.nil? && !expires_in.nil?
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,27 @@
1
+ module NoBrainer
2
+ module Simple
3
+ module OAuth2
4
+ # Includes all the required API, associations, validations and callbacks
5
+ module Client
6
+ extend ActiveSupport::Concern
7
+
8
+ include NoBrainer::Simple::OAuth2::Fields::Client
9
+
10
+ included do
11
+ before_save { self.updated_at = Time.now }
12
+
13
+ has_many :access_tokens, class_name: ::Simple::OAuth2.config.access_token_class_name, foreign_key: :client_id
14
+ has_many :access_grants, class_name: ::Simple::OAuth2.config.access_grant_class_name, foreign_key: :client_id
15
+
16
+ # Searches for Client record with the specific `#key` value
17
+ #
18
+ # @param key [#to_s] key value (any object that responds to `#to_s`)
19
+ #
20
+ # @return [Client, nil] Client object or nil if there is no record with such `#key`
21
+ #
22
+ scope(:by_key) { |key| where(key: key.to_s).first }
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,35 @@
1
+ module NoBrainer
2
+ module Simple
3
+ module OAuth2
4
+ module Fields
5
+ # Defines a AccessGrant model with next fields
6
+ module AccessGrant
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ include ::NoBrainer::Document
11
+ include ::NoBrainer::Document::Timestamps
12
+
13
+ field :resource_owner_id, type: String, index: true, required: true
14
+ field :client_id, type: String, index: true, required: true
15
+
16
+ field :token,
17
+ type: String,
18
+ required: true,
19
+ uniq: true,
20
+ index: true,
21
+ default: -> { ::Simple::OAuth2.config.token_generator.generate }
22
+
23
+ field :redirect_uri, type: String, required: true
24
+ field :scopes, type: String
25
+
26
+ field :revoked_at, type: Time
27
+ field :expires_at, type: Time, required: true
28
+ field :created_at, type: Time, required: true, default: -> { Time.now }
29
+ field :updated_at, type: Time, required: true, default: -> { Time.now }
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,40 @@
1
+ module NoBrainer
2
+ module Simple
3
+ module OAuth2
4
+ module Fields
5
+ # Defines a AccessToken model with next fields
6
+ module AccessToken
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ include ::NoBrainer::Document
11
+ include ::NoBrainer::Document::Timestamps
12
+
13
+ field :resource_owner_id, type: String, index: true, required: true
14
+ field :client_id, type: String, index: true, required: true
15
+ field :token,
16
+ type: String,
17
+ index: true,
18
+ required: true,
19
+ uniq: true,
20
+ default: -> { ::Simple::OAuth2.config.token_generator.generate }
21
+ field :refresh_token,
22
+ type: String,
23
+ index: true,
24
+ uniq: true,
25
+ default: -> do
26
+ ::Simple::OAuth2.config.issue_refresh_token ? ::Simple::OAuth2.config.token_generator.generate : ''
27
+ end
28
+
29
+ field :scopes, type: String
30
+
31
+ field :revoked_at, type: Time
32
+ field :expires_at, type: Time, required: true
33
+ field :created_at, type: Time, required: true, default: -> { Time.now }
34
+ field :updated_at, type: Time, required: true, default: -> { Time.now }
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,36 @@
1
+ module NoBrainer
2
+ module Simple
3
+ module OAuth2
4
+ module Fields
5
+ # Defines a Client model with next fields
6
+ module Client
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ include ::NoBrainer::Document
11
+ include ::NoBrainer::Document::Timestamps
12
+
13
+ field :name, type: String, required: true
14
+ field :redirect_uri, type: String, required: true
15
+
16
+ field :key,
17
+ type: String,
18
+ required: true,
19
+ index: true,
20
+ uniq: true,
21
+ default: -> { ::Simple::OAuth2.config.token_generator.generate }
22
+ field :secret,
23
+ type: String,
24
+ required: true,
25
+ index: true,
26
+ uniq: true,
27
+ default: -> { ::Simple::OAuth2.config.token_generator.generate }
28
+
29
+ field :created_at, type: Time, required: true, default: -> { Time.now }
30
+ field :updated_at, type: Time, required: true, default: -> { Time.now }
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,19 @@
1
+ module NoBrainer
2
+ module Simple
3
+ module OAuth2
4
+ module Fields
5
+ # Defines a ResourceOwner model with next fields
6
+ module ResourceOwner
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ include ::NoBrainer::Document
11
+
12
+ field :username, type: String, required: true, index: true, uniq: true
13
+ field :encrypted_password, type: String, required: true, length: (8..32)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,27 @@
1
+ module NoBrainer
2
+ module Simple
3
+ module OAuth2
4
+ # Includes all the required API, associations, validations and callbacks
5
+ module ResourceOwner
6
+ extend ActiveSupport::Concern
7
+
8
+ include NoBrainer::Simple::OAuth2::Fields::ResourceOwner
9
+
10
+ included do
11
+ # Searches for ResourceOwner record with the specific params
12
+ #
13
+ # @param _client [Object] Client instance
14
+ # @param username [String, #to_s] username value (any object that responds to `#to_s`)
15
+ # @param password [String] password value
16
+ #
17
+ # @return [ResourceOwner, nil] ResourceOwner object or nil if there is no record with such params
18
+ #
19
+ def self.oauth_authenticate(_client, username, password)
20
+ user = where(username: username.to_s).first
21
+ user if user && user.encrypted_password == password
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,29 @@
1
+ module NoBrainer
2
+ module Simple
3
+ # Semantic versioning
4
+ module OAuth2
5
+ # NoBrainerSimpleOAuth2 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
+ # NoBrainerSimpleOAuth2 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,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nobrainer_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'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nobrainer
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.33.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.33.0
41
+ description: NoBrainer mixin for SimpleOAuth2 authorization
42
+ email: volodimir.partytskyi@gmail.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - LICENSE
48
+ - lib/nobrainer_simple_oauth2.rb
49
+ - lib/nobrainer_simple_oauth2/mixins/access_grant.rb
50
+ - lib/nobrainer_simple_oauth2/mixins/access_token.rb
51
+ - lib/nobrainer_simple_oauth2/mixins/client.rb
52
+ - lib/nobrainer_simple_oauth2/mixins/concerns/fields/access_grant.rb
53
+ - lib/nobrainer_simple_oauth2/mixins/concerns/fields/access_token.rb
54
+ - lib/nobrainer_simple_oauth2/mixins/concerns/fields/client.rb
55
+ - lib/nobrainer_simple_oauth2/mixins/concerns/fields/resource_owner.rb
56
+ - lib/nobrainer_simple_oauth2/mixins/resource_owner.rb
57
+ - lib/nobrainer_simple_oauth2/version.rb
58
+ homepage: https://github.com/simple-oauth2/nobrainer_simple_oauth2
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 2.2.2
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.6.8
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Mixin for NoBrainer ORM
82
+ test_files: []