virtuatable-core 1.6.0.dev0 → 1.6.0.dev1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4ebed77d032ef7111c0bde0982cdfc947a4a1933c1dc22e2cb09a3edeca12af1
4
- data.tar.gz: '081072a04de88c52c94df498047f6cec83147fccf8c163e2c5856a9bde5398ac'
3
+ metadata.gz: d04349e236702f20e0c4a2542187eb4d1a0ef59d6ff4b44f90ff7d1a2484f783
4
+ data.tar.gz: f1e41d1f2b0a89c2851d617c2642222351a96cda77f0baeefed1a67eb98150d5
5
5
  SHA512:
6
- metadata.gz: db0b0b6d2343f36483293ce34e0e93e6d0cc9a803959f1f84caafaf338309984aaf6b226f33a363a612e4b3724c9b225dc2dfc97139a729d55dfeb2f6a9ef70f
7
- data.tar.gz: 7fc28600dbd1fa9c0a2cab48fc8431e0778e599a07ab98af8d87d3e9f4dc1e34423d1bfe0daa3fffdaa1744be92097dbab7aa9f32a59961d2e913675ad08b73e
6
+ metadata.gz: d1a87bca549bdc10d5f9dbee7c483051a0af0d7ec0cfb5594051a64b617e07fe8e32f8fcb7231398a078e4ca30d4d51e3434eb703bbdf5f5790474da897d8c6d
7
+ data.tar.gz: 910921c135060a8b78065c146e41ccc6e13f32e24ed75bafd4b3f507058ccb22a47c88f75ecfff991831c65382e1c56c3971124b07855e7e7513242871eec184
@@ -0,0 +1,9 @@
1
+ module Core
2
+ module Decorators
3
+ class Account < Core::Decorators::Base
4
+ def has_password?(password)
5
+ BCrypt::Password.new(object.password_digest) == password
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,17 @@
1
+ module Core
2
+ module Decorators
3
+ class Application < Core::Decorators::Base
4
+ def to_h
5
+ {
6
+ client_id: client_id,
7
+ name: name,
8
+ premium: premium
9
+ }
10
+ end
11
+
12
+ def has_secret?(secret)
13
+ object.client_secret == secret
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ module Core
2
+ module Decorators
3
+ class Session < Core::Decorators::Base
4
+ def to_h
5
+ {
6
+ token: token,
7
+ account_id: account.id.to_s,
8
+ created_at: created_at.iso8601
9
+ }
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,7 +1,10 @@
1
1
  module Core
2
2
  module Decorators
3
+ autoload :Account, 'core/decorators/account'
4
+ autoload :Application, 'core/decorators/application'
3
5
  autoload :Base, 'core/decorators/base'
4
6
  autoload :Campaign, 'core/decorators/campaign'
7
+ autoload :Session, 'core/decorators/session'
5
8
  autoload :Token, 'core/decorators/token'
6
9
  end
7
10
  end
@@ -2,17 +2,41 @@
2
2
 
3
3
  module Core
4
4
  module Services
5
- class Accounts
5
+ # Service managing user accounts.
6
+ # @author Vincent Courtois <courtois.vincent@outlook.com>
7
+ class Accounts < Core::Services::Base
6
8
  include Singleton
7
9
 
8
- def get_by_username(username)
10
+ # Gets an account given the nickname of the user.
11
+ #
12
+ # @param username [String] the nickname the user chose at account creation.
13
+ # @return [Core::Models::Account] the account linked to this username.
14
+ #
15
+ # @raise [Core::Helpers::Errors::BadRequest] if the username is not given.
16
+ # @raise [Core::Helpers::Errors::NotFound] if the username does not exist.
17
+ def get_by_username(username: nil, **ignored)
18
+ require_parameters username: username
9
19
  account = Core::Models::Account.find_by(username: username)
10
- if account.nil?
11
- raise Core::Helpers::Errors::NotFound.new(
12
- field: 'username',
13
- error: 'unknown'
14
- )
15
- end
20
+ raise unknown_err(field: 'username') if account.nil?
21
+
22
+ Core::Decorators::Account.new(account)
23
+ end
24
+
25
+ # Gets and authenticates an account using its credentials.
26
+ #
27
+ # @param username [String] the nickname the user chose at account creation.
28
+ # @param password [String] the password, in clear, to identify the user with.
29
+ # @return [Core::Decorators::Account] the account if it is correctly found.
30
+ #
31
+ # @raise [Core::Helpers::Errors::BadRequest] if a needed parameter is not given.
32
+ # @raise [Core::Helpers::Errors::NotFound] if a user with this nickname is not found.
33
+ # @raise [Core::Helpers::Errors::Forbidden] if the password does not match the user.
34
+ def get_by_credentials(username: nil, password: nil, **ignored)
35
+ require_parameters password: password
36
+ account = get_by_username(username: username)
37
+
38
+ raise forbidden_err(field: 'password', error: 'wrong') unless account.has_password?(password)
39
+
16
40
  account
17
41
  end
18
42
  end
@@ -20,7 +20,7 @@ module Core
20
20
  def get_by_credentials(client_id: nil, client_secret: nil, **_ignored)
21
21
  require_parameters client_secret: client_secret
22
22
  application = get_by_id(client_id: client_id)
23
- raise forbidden_err(field: 'client_secret', error: 'wrong') if application.client_secret != client_secret
23
+ raise forbidden_err(field: 'client_secret', error: 'wrong') unless application.has_secret?(client_secret)
24
24
 
25
25
  application
26
26
  end
@@ -36,7 +36,7 @@ module Core
36
36
  application = Core::Models::OAuth::Application.find_by(client_id: client_id)
37
37
  raise unknown_err(field: 'client_id') if application.nil?
38
38
 
39
- application
39
+ Core::Decorators::Application.new(application)
40
40
  end
41
41
  end
42
42
  end
@@ -7,8 +7,9 @@ module Core
7
7
  module Services
8
8
  # Service concerning sessions (log in and log out)
9
9
  # @author Vincent Courtois <courtois.vincent@outlook.com>
10
- class Sessions
10
+ class Sessions < Core::Services::Base
11
11
  include Singleton
12
+
12
13
  # Creates a new session from the given user credentials. IT will
13
14
  # * check that the user exists in the database
14
15
  # * check that the password matches the user encrypted password
@@ -18,17 +19,31 @@ module Core
18
19
  # @param username [string] the name of the user trying to log in
19
20
  # @param password [string] the password the user has provided
20
21
  # @return [Core::Models::Authentication::Session] the login session
21
- def create(username, password)
22
- account = Core.svc.accounts.get_by_username(username)
23
- if BCrypt::Password.new(account.password_digest) != password
24
- raise Core::Helpers::Errors::Forbidden.new(field: 'password', error: 'wrong')
25
- end
26
-
27
- Core::Models::Authentication::Session.create(
22
+ def create_from_credentials(username: nil, password: nil, **ignored)
23
+ account = Core.svc.accounts.get_by_credentials(
24
+ username: username,
25
+ password: password
26
+ )
27
+ session = Core::Models::Authentication::Session.create(
28
28
  account: account,
29
29
  token: SecureRandom.uuid
30
30
  )
31
31
  end
32
+
33
+ # Gets the session by its unique identifier.
34
+ #
35
+ # @param session_id [String] the unique identifier of the session you're searching.
36
+ # @return [Core::Decorators::Session] the decorated session to display in the API.
37
+ #
38
+ # @raise [Core::Helpers::Errors::BadRequest] if the session ID is not given or nil
39
+ # @raise [Core::Helpers::Errors::NotFound] if no session with its ID exist in the database.
40
+ def get_by_id(session_id: nil, **ignored)
41
+ require_parameters session_id: session_id
42
+ session = Core::Models::Authentication::Session.find_by(token: session_id)
43
+ raise unknown_err(field: 'session_id') if session.nil?
44
+
45
+ Core::Decorators::Session.new(session)
46
+ end
32
47
  end
33
48
  end
34
49
  end
data/lib/core/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Core
4
- VERSION = '1.6.0.dev0'
4
+ VERSION = '1.6.0.dev1'
5
5
  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.6.0.dev0
4
+ version: 1.6.0.dev1
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-05-18 00:00:00.000000000 Z
11
+ date: 2022-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: database_cleaner
@@ -300,8 +300,11 @@ files:
300
300
  - lib/core/controllers.rb
301
301
  - lib/core/controllers/base.rb
302
302
  - lib/core/decorators.rb
303
+ - lib/core/decorators/account.rb
304
+ - lib/core/decorators/application.rb
303
305
  - lib/core/decorators/base.rb
304
306
  - lib/core/decorators/campaign.rb
307
+ - lib/core/decorators/session.rb
305
308
  - lib/core/decorators/token.rb
306
309
  - lib/core/helpers.rb
307
310
  - lib/core/helpers/accounts.rb