virtuatable-core 1.2.2 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/core/controllers/base.rb +3 -2
- data/lib/core/helpers/accounts.rb +2 -2
- data/lib/core/helpers/applications.rb +4 -4
- data/lib/core/helpers/declarators.rb +6 -62
- data/lib/core/helpers/scopes.rb +22 -0
- data/lib/core/helpers/tokens.rb +28 -0
- data/lib/core/helpers.rb +2 -1
- data/lib/core/models/account.rb +0 -4
- data/lib/core/models/concerns/sluggable.rb +9 -15
- data/lib/core/models/oauth/access_token.rb +24 -1
- data/lib/core/models/oauth/application.rb +13 -4
- data/lib/core/models/oauth/authorization.rb +13 -1
- data/lib/core/models/oauth/refresh_token.rb +9 -2
- data/lib/core/models/oauth/scope.rb +32 -0
- data/lib/core/models/oauth.rb +1 -0
- data/lib/core/models.rb +16 -19
- data/lib/core/services.rb +5 -1
- data/lib/core/version.rb +4 -2
- data/lib/core.rb +4 -5
- metadata +5 -8
- data/lib/core/helpers/sessions.rb +0 -30
- data/lib/core/models/permissions/category.rb +0 -19
- data/lib/core/models/permissions/group.rb +0 -34
- data/lib/core/models/permissions/right.rb +0 -23
- data/lib/core/models/permissions/route.rb +0 -35
- data/lib/core/models/permissions.rb +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bb7485c8bb6e781dcf4b6d3299d3065769a4556e01421fba08eb2404a3d982a5
|
4
|
+
data.tar.gz: 281cce31ddad58a7ffb44e8fd78de1bdefdc5d5f1f2f6b53f8a89e9125e8792e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2df4695bf1450c3c99cc3d86e5cee1cc91050275b6f54e40c587553376e93bd9752aac1683692a556c3265c16bab9fb06daa530b30645b3b4fdb6748a5e741ec
|
7
|
+
data.tar.gz: eee4af9a5591e1ef3fa5a7a5a1071791680a02af442df91bcfb03d5f4f63cc334ea6fe78edfbfb848a358af3d0df9327fb13429b0d104637e9622ae71c568d64
|
@@ -13,8 +13,9 @@ module Core
|
|
13
13
|
# Includes the custom errors throwers and responses helpers.
|
14
14
|
include Core::Helpers::Errors
|
15
15
|
include Core::Helpers::Responses
|
16
|
-
# Includes the checking methods for
|
17
|
-
include Core::Helpers::
|
16
|
+
# Includes the checking methods for access tokens.
|
17
|
+
include Core::Helpers::Tokens
|
18
|
+
include Core::Helpers::Scopes
|
18
19
|
# Include the checkers and getters for OAuth apps
|
19
20
|
include Core::Helpers::Applications
|
20
21
|
# Include checkers for field requirement and check
|
@@ -10,8 +10,8 @@ module Core
|
|
10
10
|
def account
|
11
11
|
return @account unless @account.nil?
|
12
12
|
|
13
|
-
|
14
|
-
@account
|
13
|
+
@account = token.authorization.account
|
14
|
+
@account
|
15
15
|
end
|
16
16
|
|
17
17
|
def account_id_not_found
|
@@ -10,10 +10,10 @@ module Core
|
|
10
10
|
def application(premium: false)
|
11
11
|
return @application unless @application.nil?
|
12
12
|
|
13
|
-
check_presence '
|
14
|
-
@application = application_model.find_by(
|
15
|
-
api_not_found '
|
16
|
-
api_forbidden '
|
13
|
+
check_presence 'client_id'
|
14
|
+
@application = application_model.find_by(client_id: params['client_id'])
|
15
|
+
api_not_found 'client_id.unknown' if @application.nil?
|
16
|
+
api_forbidden 'client_id.forbidden' if premium && !@application.premium
|
17
17
|
|
18
18
|
@application
|
19
19
|
end
|
@@ -6,9 +6,6 @@ module Core
|
|
6
6
|
# to declare routes whithin a service, performing needed checks and filters.
|
7
7
|
# @author Vincent Courtois <courtois.vincent@outlook.com>
|
8
8
|
module Declarators
|
9
|
-
# @!attribute [r] routes
|
10
|
-
# @return [Array<Core::Models::Permissions::Route>] the currently declared routes.
|
11
|
-
attr_reader :api_routes
|
12
9
|
|
13
10
|
# Main method to declare new routes, persisting them in the database and
|
14
11
|
# declaring it in the Sinatra application with the needed before checks.
|
@@ -16,68 +13,15 @@ module Core
|
|
16
13
|
# @param verb [String] the HTTP method for the route.
|
17
14
|
# @param path [String] the whole URI with parameters for the route.
|
18
15
|
# @param options [Hash] the additional options for the route.
|
19
|
-
def api_route(verb, path,
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
# add some treatments but avoid many problems if route.premium
|
26
|
-
send(route.verb, route.path) do
|
27
|
-
application(premium: current_route.premium)
|
28
|
-
session if current_route.authenticated
|
16
|
+
def api_route(verb, path, premium: false, scopes: ['data::usage'], &block)
|
17
|
+
send(verb, path) do
|
18
|
+
scope_objects = fetch_scopes(scopes)
|
19
|
+
appli = application(premium: premium)
|
20
|
+
check_app_scopes(appli, scope_objects)
|
21
|
+
check_token_scopes(token, scope_objects)
|
29
22
|
instance_eval(&block)
|
30
23
|
end
|
31
24
|
end
|
32
|
-
|
33
|
-
# Add a route to the database, then to the routes array.
|
34
|
-
# @param verb [String] the HTTP method used to request this route.
|
35
|
-
# @param path [String] the path used to request this route.
|
36
|
-
# @return [Core::Models::Permissions::Route] the created route.
|
37
|
-
def add_route(verb:, path:, options:)
|
38
|
-
route = Core::Models::Permissions::Route.find_or_create_by!(
|
39
|
-
path: path,
|
40
|
-
verb: verb.downcase,
|
41
|
-
premium: options[:premium],
|
42
|
-
authenticated: options[:authenticated]
|
43
|
-
)
|
44
|
-
api_routes.nil? ? @api_routes = [route] : push_route(route)
|
45
|
-
add_permissions(route)
|
46
|
-
route
|
47
|
-
end
|
48
|
-
|
49
|
-
# Pushes the route in the api routes list, by creating it if needed
|
50
|
-
# @param route [Core::Models::Permissions::Route] the route to push in the list of routes.
|
51
|
-
def push_route(route)
|
52
|
-
@api_routes << route if api_routes.none? do |tmp_route|
|
53
|
-
route.id == tmp_route.id
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
# Add the default access permissions to a route. Any group tagged superuser
|
58
|
-
# can automatically access any newly declared_route.
|
59
|
-
# params route [Core::Models::Permissions::Route] the route to add the permissions to.
|
60
|
-
def add_permissions(route)
|
61
|
-
groups = Core::Models::Permissions::Group.where(is_superuser: true)
|
62
|
-
groups.each do |group|
|
63
|
-
unless route.groups.where(id: group.id).exists?
|
64
|
-
route.groups << group
|
65
|
-
route.save!
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
# The default options for a route, being the most used value for each key.
|
71
|
-
# @return [Hash] the default options as a hash.
|
72
|
-
def default_options
|
73
|
-
{
|
74
|
-
# If TRUE the application MUST be premium to access the route.
|
75
|
-
# Mainly used to protect administration routes against illegal accesses.
|
76
|
-
premium: false,
|
77
|
-
# If TRUE the user MUST be authenticated to access the route.
|
78
|
-
authenticated: true
|
79
|
-
}
|
80
|
-
end
|
81
25
|
end
|
82
26
|
end
|
83
27
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Core
|
2
|
+
module Helpers
|
3
|
+
module Scopes
|
4
|
+
|
5
|
+
def fetch_scopes(names)
|
6
|
+
(names.map { |n| Core::Models::OAuth::Scope.find_by(name: n) }).select { |s| !s.nil? }
|
7
|
+
end
|
8
|
+
|
9
|
+
def check_token_scopes(token, scopes)
|
10
|
+
scopes.each do |scope|
|
11
|
+
api_forbidden 'scope.forbidden' if !token.scopes.include? scope
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def check_app_scopes(application, scopes)
|
16
|
+
scopes.each do |scope|
|
17
|
+
api_forbidden 'scope.forbidden' if !application.scopes.include? scope
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Core
|
4
|
+
module Helpers
|
5
|
+
# This helper aims at providing vanity methods concerning OAuth tokens.
|
6
|
+
# @author Vincent Courtois <courtois.vincent@outlook.com>
|
7
|
+
module Tokens
|
8
|
+
# Returns the database object representing the current OAuth token, or
|
9
|
+
# raises an error if the token seems to be invalid for any reason.
|
10
|
+
# @return [Core::Models::Oauth::AccessToken] the token if everything went well.
|
11
|
+
# @raise [Core::Helpers::Errors::BadRequest] if the token is not given.
|
12
|
+
# @raise [Core::Helpers::Errors::NotFound] if the token is not found in the
|
13
|
+
# database searching for the value passed as parameter.
|
14
|
+
# @raise [Core::Helpers::Errors::Forbidden] if the token belongs to another
|
15
|
+
# application.
|
16
|
+
def token
|
17
|
+
return @token unless @token.nil?
|
18
|
+
|
19
|
+
check_presence 'token'
|
20
|
+
@token = Core::Models::OAuth::AccessToken.find_by(value: params['token'])
|
21
|
+
api_not_found 'token.unknown' if @token.nil?
|
22
|
+
token_app_id = token.authorization.application.id.to_s
|
23
|
+
api_forbidden 'token.mismatch' if token_app_id != application.id.to_s
|
24
|
+
@token
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/core/helpers.rb
CHANGED
@@ -13,6 +13,7 @@ module Core
|
|
13
13
|
autoload :Parameters, 'core/helpers/parameters'
|
14
14
|
autoload :Responses, 'core/helpers/responses'
|
15
15
|
autoload :Routes, 'core/helpers/routes'
|
16
|
-
autoload :
|
16
|
+
autoload :Scopes, 'core/helpers/scopes'
|
17
|
+
autoload :Tokens, 'core/helpers/tokens'
|
17
18
|
end
|
18
19
|
end
|
data/lib/core/models/account.rb
CHANGED
@@ -37,10 +37,6 @@ module Core
|
|
37
37
|
# @!attribute [w] password_confirmation
|
38
38
|
# @return [String] the confirmation of the password, do not get, just set it ; it must be the same as the password.
|
39
39
|
has_secure_password validations: false
|
40
|
-
|
41
|
-
# @!attribute [rw] groups
|
42
|
-
# @return [Array<Core::Models::Permissions::Group>] the groups giving their corresponding rights to the current account.
|
43
|
-
has_and_belongs_to_many :groups, class_name: 'Core::Models::Permissions::Group', inverse_of: :accounts
|
44
40
|
|
45
41
|
# @!attribute [rw] applications
|
46
42
|
# @return [Array<Core::Models::OAuth::Application] the applications this user has created and owns.
|
@@ -6,22 +6,16 @@ module Core
|
|
6
6
|
module Sluggable
|
7
7
|
extend ActiveSupport::Concern
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
# @param entity_type [String,Symbol] the name of the model including it, to be included in the error messages.
|
14
|
-
def make_sluggable(entity_type)
|
15
|
-
# @!attribute [rw] slug
|
16
|
-
# @return [String] the slug of the current entity ; it must be snake-cased, longer than four characters, unique for the entity and given.
|
17
|
-
field :slug, type: String
|
9
|
+
included do
|
10
|
+
# @!attribute [rw] slug
|
11
|
+
# @return [String] the slug of the current entity ; it must be snake-cased, longer than four characters, unique for the entity and given.
|
12
|
+
field :slug, type: String
|
18
13
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
end
|
14
|
+
validates :slug,
|
15
|
+
length: {minimum: 4, message: 'minlength', if: :slug?},
|
16
|
+
format: {with: /\A[a-z]+(_[a-z]+)*\z/, message: 'pattern', if: :slug?},
|
17
|
+
uniqueness: {message: 'uniq', if: :slug?},
|
18
|
+
presence: {message: 'required'}
|
25
19
|
end
|
26
20
|
end
|
27
21
|
end
|
@@ -8,7 +8,7 @@ module Core
|
|
8
8
|
include Mongoid::Document
|
9
9
|
include Mongoid::Timestamps
|
10
10
|
|
11
|
-
store_in collection: '
|
11
|
+
store_in collection: 'oauth_access_tokens'
|
12
12
|
|
13
13
|
# @!attribute [rw] value
|
14
14
|
# @return [String] the value of the token, returned to the application when built.
|
@@ -21,6 +21,12 @@ module Core
|
|
21
21
|
# @return [Core::Models::OAuth::Authorization] the authorization code that issued this token to the application for this user.
|
22
22
|
belongs_to :authorization, class_name: 'Core::Models::OAuth::Authorization', inverse_of: :tokens
|
23
23
|
|
24
|
+
|
25
|
+
# A refresh token is attached to each and every refresh token so that it can be used to deliver a new access token.
|
26
|
+
# @!attribute [rx] refresh_token
|
27
|
+
# @return [Core::Models::OAuth::RefreshToken] the refresh token linked to this token
|
28
|
+
has_one :refresh_token, class_name: 'Core::Models::OAuth::RefreshToken', inverse_of: :token
|
29
|
+
|
24
30
|
validates :value,
|
25
31
|
presence: {message: 'required'},
|
26
32
|
uniqueness: {message: 'uniq'}
|
@@ -28,8 +34,25 @@ module Core
|
|
28
34
|
# Checks if the current date is inferior to the creation date + expiration period
|
29
35
|
# @return [Boolean] TRUE if the token is expired, FALSE otherwise.
|
30
36
|
def expired?
|
37
|
+
# Handles the case where the token is given to a premium app (our apps have infinite tokens).
|
38
|
+
return false if premium?
|
39
|
+
return true if refresh_token.used?
|
40
|
+
|
31
41
|
created_at.to_time.to_i + expiration < Time.now.to_i
|
32
42
|
end
|
43
|
+
|
44
|
+
# Returns the scopes this access token can use to access the application
|
45
|
+
# @return [Array<Core::Models::OAuth::Scope>] the array of scopes from the linked authorization
|
46
|
+
def scopes
|
47
|
+
# Premium applications (our applications) have all the rights on the API.
|
48
|
+
return Core::Models::OAuth::Scope.all.to_a if premium?
|
49
|
+
|
50
|
+
authorization.scopes
|
51
|
+
end
|
52
|
+
|
53
|
+
def premium?
|
54
|
+
authorization.application.premium
|
55
|
+
end
|
33
56
|
end
|
34
57
|
end
|
35
58
|
end
|
@@ -7,14 +7,17 @@ module Core
|
|
7
7
|
include Mongoid::Document
|
8
8
|
include Mongoid::Timestamps
|
9
9
|
|
10
|
-
store_in collection: '
|
10
|
+
store_in collection: 'oauth_applications'
|
11
11
|
|
12
12
|
# @!attribute [rw] name
|
13
13
|
# @return [String] the unique name of the application, mainly used to identify and display it.
|
14
14
|
field :name, type: String
|
15
|
-
# @!attribute [rw]
|
15
|
+
# @!attribute [rw] client_id
|
16
16
|
# @return [String] the unique key for the application, identifying it when requesting a token for the API.
|
17
|
-
field :
|
17
|
+
field :client_id, type: String, default: ->{ SecureRandom.hex }
|
18
|
+
# @!attribute [rw] client_secret
|
19
|
+
# @return [String] the "password" of the application, used to identify it when requesting tokens.
|
20
|
+
field :client_secret, type: String, default: ->{ SecureRandom.hex }
|
18
21
|
# @!attribute [rw] premium
|
19
22
|
# @return [Boolean] a value indicating whether the application should automatically receive a token when an account is created, or not.
|
20
23
|
field :premium, type: Mongoid::Boolean, default: false
|
@@ -28,16 +31,22 @@ module Core
|
|
28
31
|
# @!attribute [rw] authorizations
|
29
32
|
# @return [Array<Core::Models::OAuth::Authorization>] the authorizations linked to the accounts this application can get the data from.
|
30
33
|
has_many :authorizations, class_name: 'Core::Models::OAuth::Authorization', inverse_of: :application
|
34
|
+
# @!attribute [rw]
|
35
|
+
# @return [Array<Core::Models::OAuth::Scope>] the scopes this application will transmit to its token
|
36
|
+
has_and_belongs_to_many :scopes, class_name: 'Core::Models::OAuth::Scope', inverse_of: :applications
|
31
37
|
|
32
38
|
validates :name,
|
33
39
|
presence: {message: 'required'},
|
34
40
|
length: {minimum: 6, message: 'minlength'},
|
35
41
|
uniqueness: {message: 'uniq'}
|
36
42
|
|
37
|
-
validates :
|
43
|
+
validates :client_id,
|
38
44
|
presence: {message: 'required'},
|
39
45
|
uniqueness: {message: 'uniq'}
|
40
46
|
|
47
|
+
validates :client_secret,
|
48
|
+
presence: {message: 'required'}
|
49
|
+
|
41
50
|
validate :redirect_uris_values
|
42
51
|
|
43
52
|
# Checks the URIs to get sure they are correct, a URI is correct if :
|
@@ -10,11 +10,14 @@ module Core
|
|
10
10
|
include Mongoid::Document
|
11
11
|
include Mongoid::Timestamps
|
12
12
|
|
13
|
-
store_in collection: '
|
13
|
+
store_in collection: 'oauth_authorizations'
|
14
14
|
|
15
15
|
# @!attribute [rw] code
|
16
16
|
# @return [String] the value corresponding to the authentication code in the RFC of OAuth2.0, kep for historic purpose.
|
17
17
|
field :code, type: String, default: ->{ SecureRandom.hex }
|
18
|
+
# @!attribute [rw] expiration
|
19
|
+
# @return [Integer] the time, in seconds, after which the authorization is declared expired.
|
20
|
+
field :expiration, type: Integer, default: 86400
|
18
21
|
|
19
22
|
# @!attribute [rw] account
|
20
23
|
# @return [Arkaaan::Account] the account granting the authorization to access its data to the application.
|
@@ -25,10 +28,19 @@ module Core
|
|
25
28
|
# @!attribute [rw] token
|
26
29
|
# @return [Core::Models::OAuth::AccessToken] the access token used further in the application process to access private data of the account.
|
27
30
|
has_many :tokens, class_name: 'Core::Models::OAuth::AccessToken', inverse_of: :authorization
|
31
|
+
# @!attribute [rw]
|
32
|
+
# @return [Array<Core::Models::OAuth::Scope>] the scopes this access token has.
|
33
|
+
has_and_belongs_to_many :scopes, class_name: 'Core::Models::OAuth::Scope', inverse_of: :authorizations
|
28
34
|
|
29
35
|
validates :code,
|
30
36
|
presence: {message: 'required'},
|
31
37
|
uniqueness: {message: 'uniq'}
|
38
|
+
|
39
|
+
# Checks if the current date is inferior to the creation date + expiration period
|
40
|
+
# @return [Boolean] TRUE if the authorization is expired, FALSE otherwise.
|
41
|
+
def expired?
|
42
|
+
created_at.to_time.to_i + expiration < Time.now.to_i
|
43
|
+
end
|
32
44
|
end
|
33
45
|
end
|
34
46
|
end
|
@@ -7,15 +7,22 @@ module Core
|
|
7
7
|
include Mongoid::Document
|
8
8
|
include Mongoid::Timestamps
|
9
9
|
|
10
|
-
store_in collection: '
|
10
|
+
store_in collection: 'oauth_refresh_tokens'
|
11
11
|
|
12
12
|
# @!attribute [rw] value
|
13
13
|
# @return [String] the value of the token, returned to the application when built.
|
14
14
|
field :value, type: String, default: ->{ SecureRandom.hex }
|
15
|
+
# @!attribute [rw] used_at
|
16
|
+
# @return [DateTime] the date and time at which this refresh token has been useds to create a new access token.
|
17
|
+
field :used_at, type: DateTime, default: nil
|
15
18
|
|
16
19
|
# @!attribute [rw] authorization
|
17
20
|
# @return [Core::Models::OAuth::Authorization] the authorization code that issued this token to the application for this user.
|
18
|
-
belongs_to :
|
21
|
+
belongs_to :token, class_name: 'Core::Models::OAuth::AccessToken', inverse_of: :refresh_token
|
22
|
+
|
23
|
+
def used?
|
24
|
+
!used_at.nil? && used_at < DateTime.now
|
25
|
+
end
|
19
26
|
end
|
20
27
|
end
|
21
28
|
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Core
|
2
|
+
module Models
|
3
|
+
module OAuth
|
4
|
+
# A scope gives access to some parts of the API, for example to the management of campaigns,
|
5
|
+
# applications or for account profile management.
|
6
|
+
# @author Vincent Courtois <courtois.vincent@outlook.com>
|
7
|
+
class Scope
|
8
|
+
include Mongoid::Document
|
9
|
+
include Mongoid::Timestamps
|
10
|
+
|
11
|
+
store_in collection: 'oauth_scopes'
|
12
|
+
|
13
|
+
# @!attribute [rw] name
|
14
|
+
# @return [String] the name of the scope, used to get its translation on the frontend.
|
15
|
+
field :name, type: String, default: ''
|
16
|
+
|
17
|
+
# @!attribute [rw] applications
|
18
|
+
# @return [Array<Core::Models::OAuth::Application>] the applications that want to have access to this
|
19
|
+
# scope from the users of the platform. These rights will be carried on to the tokens and frozen.
|
20
|
+
has_and_belongs_to_many :applications, class_name: 'Core::Models::OAuth::Application', inverse_of: :scopes
|
21
|
+
# @!attribute [rw] tokeauthorizationsns
|
22
|
+
# @return [Array<Core::Models::OAuth::Authorization] the tokens having these scopes.
|
23
|
+
has_and_belongs_to_many :authorizations, class_name: 'Core::Models::OAuth::Authorization', inverse_of: :scopes
|
24
|
+
|
25
|
+
validates :name,
|
26
|
+
presence: {message: 'required'},
|
27
|
+
length: {minimum: 6, if: :name?, message: 'minlength'},
|
28
|
+
uniqueness: {id: :name?, message: 'uniq'}
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/lib/core/models/oauth.rb
CHANGED
data/lib/core/models.rb
CHANGED
@@ -1,24 +1,21 @@
|
|
1
|
-
|
2
|
-
require 'active_model'
|
3
|
-
require 'active_support'
|
4
|
-
require 'dotenv/load'
|
1
|
+
# frozen_string_literal: true
|
5
2
|
|
6
|
-
# Main module of the application, holding all the subsequent classes.
|
7
|
-
# @author Vincent Courtois <courtois.vincent@outlook.com>
|
8
3
|
module Core
|
4
|
+
# Module holding the representations of the business objects we're manipulating
|
5
|
+
# in the database. Models are declared as Mongoid classes to connect to MongoDB
|
6
|
+
# @author Vincent Courtois <courtois.vincent@outlook.com>
|
9
7
|
module Models
|
10
|
-
autoload :Account
|
8
|
+
autoload :Account, 'core/models/account'
|
11
9
|
autoload :Authentication, 'core/models/authentication'
|
12
|
-
autoload :Campaign
|
13
|
-
autoload :Campaigns
|
14
|
-
autoload :Chatroom
|
15
|
-
autoload :Chatrooms
|
16
|
-
autoload :Concerns
|
17
|
-
autoload :Event
|
18
|
-
autoload :Files
|
19
|
-
autoload :Notification
|
20
|
-
autoload :OAuth
|
21
|
-
autoload :
|
22
|
-
autoload :Ruleset , 'core/models/ruleset'
|
10
|
+
autoload :Campaign, 'core/models/campaign'
|
11
|
+
autoload :Campaigns, 'core/models/campaigns'
|
12
|
+
autoload :Chatroom, 'core/models/chatroom'
|
13
|
+
autoload :Chatrooms, 'core/models/chatrooms'
|
14
|
+
autoload :Concerns, 'core/models/concerns'
|
15
|
+
autoload :Event, 'core/models/event'
|
16
|
+
autoload :Files, 'core/models/files'
|
17
|
+
autoload :Notification, 'core/models/notification'
|
18
|
+
autoload :OAuth, 'core/models/oauth'
|
19
|
+
autoload :Ruleset, 'core/models/ruleset'
|
23
20
|
end
|
24
|
-
end
|
21
|
+
end
|
data/lib/core/services.rb
CHANGED
@@ -1,8 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Core
|
4
|
+
# Services are orchestrating models to provide more high-level operations.
|
5
|
+
# @author Vincent Courtois <courtois.vincent@outlook.com>
|
2
6
|
module Services
|
3
7
|
autoload :Accounts, 'core/services/accounts'
|
4
8
|
autoload :Base, 'core/services/base'
|
5
9
|
autoload :Registry, 'core/services/registry'
|
6
10
|
autoload :Sessions, 'core/services/sessions'
|
7
11
|
end
|
8
|
-
end
|
12
|
+
end
|
data/lib/core/version.rb
CHANGED
data/lib/core.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require 'dotenv/load'
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
%w[active_model mongoid active_support].each { |g| require g }
|
5
4
|
|
6
5
|
# Main module of the application, holding all the subsequent classes.
|
7
6
|
# @author Vincent Courtois <courtois.vincent@outlook.com>
|
@@ -10,4 +9,4 @@ module Core
|
|
10
9
|
autoload :Helpers, 'core/helpers'
|
11
10
|
autoload :Models, 'core/models'
|
12
11
|
autoload :Services, 'core/services'
|
13
|
-
end
|
12
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: virtuatable-core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vincent Courtois
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-05-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: database_cleaner
|
@@ -298,7 +298,8 @@ files:
|
|
298
298
|
- lib/core/helpers/parameters.rb
|
299
299
|
- lib/core/helpers/responses.rb
|
300
300
|
- lib/core/helpers/routes.rb
|
301
|
-
- lib/core/helpers/
|
301
|
+
- lib/core/helpers/scopes.rb
|
302
|
+
- lib/core/helpers/tokens.rb
|
302
303
|
- lib/core/models.rb
|
303
304
|
- lib/core/models/account.rb
|
304
305
|
- lib/core/models/authentication.rb
|
@@ -332,11 +333,7 @@ files:
|
|
332
333
|
- lib/core/models/oauth/application.rb
|
333
334
|
- lib/core/models/oauth/authorization.rb
|
334
335
|
- lib/core/models/oauth/refresh_token.rb
|
335
|
-
- lib/core/models/
|
336
|
-
- lib/core/models/permissions/category.rb
|
337
|
-
- lib/core/models/permissions/group.rb
|
338
|
-
- lib/core/models/permissions/right.rb
|
339
|
-
- lib/core/models/permissions/route.rb
|
336
|
+
- lib/core/models/oauth/scope.rb
|
340
337
|
- lib/core/models/ruleset.rb
|
341
338
|
- lib/core/services.rb
|
342
339
|
- lib/core/services/accounts.rb
|
@@ -1,30 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Core
|
4
|
-
module Helpers
|
5
|
-
# This helper gives access to methods about user's session on the API.
|
6
|
-
# @author Vincent Courtois <courtois.vincent@outlook.com>
|
7
|
-
module Sessions
|
8
|
-
# Checks the session of the user requesting the API and returns an error
|
9
|
-
# if it either not exists with the given token, or the token is not given.
|
10
|
-
#
|
11
|
-
# @raise [Virtuatable::API::Errors::NotFound] if the session is not found
|
12
|
-
# or the token not given in the parameters of the request.
|
13
|
-
# @raise [Virtuatable::API::Errors::BadRequest] if the session token is
|
14
|
-
# not correctly given in the parameters.
|
15
|
-
#
|
16
|
-
# @return [Core::Models::Authentication::Session] the current session of the user.
|
17
|
-
def session
|
18
|
-
return @session unless @session.nil?
|
19
|
-
|
20
|
-
check_presence 'session_id'
|
21
|
-
@session = session_model.find_by(token: params['session_id'])
|
22
|
-
@session.nil? ? api_not_found('session_id.unknown') : @session
|
23
|
-
end
|
24
|
-
|
25
|
-
def session_model
|
26
|
-
Core::Models::Authentication::Session
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
module Core
|
2
|
-
module Models
|
3
|
-
module Permissions
|
4
|
-
# A category of rights regroups one or several rights for convenience purposes.
|
5
|
-
# @author Vincent Courtois <courtois.vincent@outlook.com>
|
6
|
-
class Category
|
7
|
-
include Mongoid::Document
|
8
|
-
include Mongoid::Timestamps
|
9
|
-
include Core::Models::Concerns::Sluggable
|
10
|
-
|
11
|
-
store_in collection: 'categories'
|
12
|
-
|
13
|
-
has_many :rights, class_name: 'Core::Models::Permissions::Right', inverse_of: :category
|
14
|
-
|
15
|
-
make_sluggable 'category'
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
module Core
|
2
|
-
module Models
|
3
|
-
module Permissions
|
4
|
-
# A group gathers one or several users to give them the same rights for conviniency purposes.
|
5
|
-
# @author Vincent Courtois <courtois.vincent@outlook.com>
|
6
|
-
class Group
|
7
|
-
include Mongoid::Document
|
8
|
-
include Mongoid::Timestamps
|
9
|
-
include Core::Models::Concerns::Sluggable
|
10
|
-
|
11
|
-
store_in collection: 'groups'
|
12
|
-
|
13
|
-
# @!attribute [rw] is_default
|
14
|
-
# @return [Boolean] a boolean indicating whether this group is given when a new user registered or not.
|
15
|
-
field :is_default, type: Mongoid::Boolean, default: false
|
16
|
-
# @!attribute [rw] is_superuser
|
17
|
-
# @return [Boolean] a boolean indicating whether this group should have access to all groups and rights or not.
|
18
|
-
field :is_superuser, type: Mongoid::Boolean, default: false
|
19
|
-
|
20
|
-
# @!attribute [rw] accounts
|
21
|
-
# @return [Array<Core::Models::Account>] the accounts having the rights granted by this group.
|
22
|
-
has_and_belongs_to_many :accounts, class_name: 'Core::Models::Account', inverse_of: :groups
|
23
|
-
# @!attribute [rw] rights
|
24
|
-
# @return [Array<Core::Models::Permissions::Right>] the rights granted by belonging to this group.
|
25
|
-
has_and_belongs_to_many :rights, class_name: 'Core::Models::Permissions::Right', inverse_of: :groups
|
26
|
-
# @!attribute [rw] routes
|
27
|
-
# @return [Array<Core::Models::Monitoring::Route>] the routes this group can access in the API.
|
28
|
-
has_and_belongs_to_many :routes, class_name: 'Core::Models::Permissions::Route', inverse_of: :groups
|
29
|
-
|
30
|
-
make_sluggable 'group'
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
module Core
|
2
|
-
module Models
|
3
|
-
module Permissions
|
4
|
-
# A right is the access to one or several features in the application. It's applied to a group, and transitively to an account.
|
5
|
-
# @author Vincent Courtois <courtois;vincent@outlook.com>
|
6
|
-
class Right
|
7
|
-
include Mongoid::Document
|
8
|
-
include Mongoid::Timestamps
|
9
|
-
include Core::Models::Concerns::Sluggable
|
10
|
-
|
11
|
-
store_in collection: 'rights'
|
12
|
-
|
13
|
-
# @!attribute [rw] groups
|
14
|
-
# @return [Array<Core::Models::Permissions::Group>] the groups granted with the permission to access features opened by this right.
|
15
|
-
has_and_belongs_to_many :groups, class_name: 'Core::Models::Permissions::Group', inverse_of: :rights
|
16
|
-
|
17
|
-
belongs_to :category, class_name: 'Core::Models::Permissions::Category', inverse_of: :rights
|
18
|
-
|
19
|
-
make_sluggable 'right'
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
module Core
|
2
|
-
module Models
|
3
|
-
module Permissions
|
4
|
-
# A route is an endpoint accessible in the API. Each route has to have an associated endpoint in the deployed instances.
|
5
|
-
# @param Vincent Courtois <courtois.vincent@outlook.com>
|
6
|
-
class Route
|
7
|
-
include Mongoid::Document
|
8
|
-
include Mongoid::Timestamps
|
9
|
-
include Core::Models::Concerns::Premiumable
|
10
|
-
include Core::Models::Concerns::Activable
|
11
|
-
|
12
|
-
store_in collection: 'routes'
|
13
|
-
|
14
|
-
# @!attribute [rw] path
|
15
|
-
# @return [String] the path (URI) of the route in the API.
|
16
|
-
field :path, type: String, default: '/'
|
17
|
-
# @!attribute [rw] verb
|
18
|
-
# @return [String] the verb (HTTP method) of this route in the API.
|
19
|
-
field :verb, type: String, default: 'get'
|
20
|
-
# @!attribute [rw] authenticated
|
21
|
-
# @return [Boolean] if true, the session_id is needed for this route, if false it is not.
|
22
|
-
field :authenticated, type: Mongoid::Boolean, default: true
|
23
|
-
# @!attribute [rw] groups
|
24
|
-
# @return [Array<Core::Models::Permissions::Group>] the groups having permission to access this route.
|
25
|
-
has_and_belongs_to_many :groups, class_name: 'Core::Models::Permissions::Group', inverse_of: :groups
|
26
|
-
|
27
|
-
validates :path,
|
28
|
-
format: {with: /\A(\/|((\/:?[a-zA-Z0-9_]+)+))\z/, message: 'pattern', if: :path?}
|
29
|
-
|
30
|
-
validates :verb,
|
31
|
-
inclusion: {message: 'unknown', in: ['get', 'post', 'put', 'delete', 'patch', 'option']}
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
@@ -1,13 +0,0 @@
|
|
1
|
-
module Core
|
2
|
-
module Models
|
3
|
-
# This module holds the logic for all the classes concerning the permissions abd rights for the user.
|
4
|
-
# A permission is restricting the access to one or several features to the users having it.
|
5
|
-
# @author Vincent Courtois <courtois.vincent@outlook.com>
|
6
|
-
module Permissions
|
7
|
-
autoload :Right , 'core/models/permissions/right'
|
8
|
-
autoload :Group , 'core/models/permissions/group'
|
9
|
-
autoload :Category, 'core/models/permissions/category'
|
10
|
-
autoload :Route , 'core/models/permissions/route'
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|