virtuatable-core 1.6.0.dev0 → 1.6.0.dev4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4ebed77d032ef7111c0bde0982cdfc947a4a1933c1dc22e2cb09a3edeca12af1
4
- data.tar.gz: '081072a04de88c52c94df498047f6cec83147fccf8c163e2c5856a9bde5398ac'
3
+ metadata.gz: ed2fb554bca98c5a51745c7872a81eafd22c8c741ea1025f8757313182f5ba07
4
+ data.tar.gz: a13f5931693e5a41f21c6aafe664c8ad37e0709ce716f3bb16b01fa45f0f5705
5
5
  SHA512:
6
- metadata.gz: db0b0b6d2343f36483293ce34e0e93e6d0cc9a803959f1f84caafaf338309984aaf6b226f33a363a612e4b3724c9b225dc2dfc97139a729d55dfeb2f6a9ef70f
7
- data.tar.gz: 7fc28600dbd1fa9c0a2cab48fc8431e0778e599a07ab98af8d87d3e9f4dc1e34423d1bfe0daa3fffdaa1744be92097dbab7aa9f32a59961d2e913675ad08b73e
6
+ metadata.gz: d404bb036b5f7f96ba6b15ac350d3f67c1e409d1be876ab54c8fff997ba1f18994a014cdfdd96e6ced3fe302fd9d77a8ba4964d809944f42812752b13721da8e
7
+ data.tar.gz: d8a5ef0902184dbbe8c63b908acddad0bbdcf6e9480d44a8c48ce977db627b7083136a3c803e4aba9b83ecda7e54c8bb4c589e4bb922712ea7d7fae85112926e
@@ -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,9 @@
1
+ module Core
2
+ module Decorators
3
+ class Authorization < Core::Decorators::Base
4
+ def to_h
5
+ { code: object.code }
6
+ end
7
+ end
8
+ end
9
+ 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,11 @@
1
1
  module Core
2
2
  module Decorators
3
+ autoload :Account, 'core/decorators/account'
4
+ autoload :Application, 'core/decorators/application'
5
+ autoload :Authorization, 'core/decorators/authorization'
3
6
  autoload :Base, 'core/decorators/base'
4
7
  autoload :Campaign, 'core/decorators/campaign'
8
+ autoload :Session, 'core/decorators/session'
5
9
  autoload :Token, 'core/decorators/token'
6
10
  end
7
11
  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
@@ -42,7 +42,7 @@ module Core
42
42
  authorization = Core::Models::OAuth::Authorization.find_by(code: authorization_code)
43
43
  raise unknown_err(field: 'authorization_code') if authorization.nil?
44
44
 
45
- authorization
45
+ Core::Decorators::Authorization.new(authorization)
46
46
  end
47
47
 
48
48
  private
@@ -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,16 +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
+ Decorators::Session.new(session)
32
+ end
33
+
34
+ # Gets the session by its unique identifier.
35
+ #
36
+ # @param session_id [String] the unique identifier of the session you're searching.
37
+ # @return [Core::Decorators::Session] the decorated session to display in the API.
38
+ #
39
+ # @raise [Core::Helpers::Errors::BadRequest] if the session ID is not given or nil
40
+ # @raise [Core::Helpers::Errors::NotFound] if no session with its ID exist in the database.
41
+ def get_by_id(session_id: nil, **ignored)
42
+ require_parameters session_id: session_id
43
+ session = Core::Models::Authentication::Session.find_by(token: session_id)
44
+ raise unknown_err(field: 'session_id') if session.nil?
45
+
46
+ Core::Decorators::Session.new(session)
31
47
  end
32
48
  end
33
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.dev4'
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.dev4
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
@@ -280,16 +280,16 @@ dependencies:
280
280
  name: draper
281
281
  requirement: !ruby/object:Gem::Requirement
282
282
  requirements:
283
- - - ">="
283
+ - - '='
284
284
  - !ruby/object:Gem::Version
285
- version: '0'
285
+ version: 4.0.2
286
286
  type: :runtime
287
287
  prerelease: false
288
288
  version_requirements: !ruby/object:Gem::Requirement
289
289
  requirements:
290
- - - ">="
290
+ - - '='
291
291
  - !ruby/object:Gem::Version
292
- version: '0'
292
+ version: 4.0.2
293
293
  description: This gem holds the model layer for my table-top RPG games application.
294
294
  email: courtois.vincent@outlook.com
295
295
  executables: []
@@ -300,8 +300,12 @@ 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
305
+ - lib/core/decorators/authorization.rb
303
306
  - lib/core/decorators/base.rb
304
307
  - lib/core/decorators/campaign.rb
308
+ - lib/core/decorators/session.rb
305
309
  - lib/core/decorators/token.rb
306
310
  - lib/core/helpers.rb
307
311
  - lib/core/helpers/accounts.rb