ru.Bee 2.1.1 → 2.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cc6a4fbeb28a460ca66f75ccf1f5c97fe1572f422392f9ee1b72d082e05cb287
4
- data.tar.gz: f4dbeaf7338af243f6e225a2a798a87de75b001fa4196bed94345d78887ba8ea
3
+ metadata.gz: f5d297ac622918eccf7bd285f4cfdf848e2909cbd19f57378105022505dfd222
4
+ data.tar.gz: 8976743ad813675bdd5c32d9149c5ffd70c3254980ab4a3b25835532e852d0cc
5
5
  SHA512:
6
- metadata.gz: 996510c80521425797e17a1b641d767d6319877e127883692d519457c7573835ae2670a8ed17950f302b1934a0aae1270d8b065acfee079341935ddb8964a26f
7
- data.tar.gz: 75a8ab899d3c98fd492a30ec5501982526318387c9214b78ebdf57ec2c39a91778268283db82cbeb5ae9072aa4c62507b1d175423e7f86beec892cddfea0bb90
6
+ metadata.gz: 0d9053f14123576c2a28e58fe39398a207bd5b350e96b871bf291f24ae9470ec15659cfbbbce643fb54a689fb9f45199792b571a3be4634397c7988bc5e9b42d
7
+ data.tar.gz: 3f2572054237820d93a8cec5daee95f86df00e36991b74e74fa1b5cf5bfeffc088c96d01026d07a1d163486fc8ad47d594c3f484396e8b467832e2946adea869
@@ -0,0 +1,15 @@
1
+ class CreateClients
2
+ def call
3
+ return if Rubee::SequelObject::DB.tables.include?(:clients)
4
+
5
+ Rubee::SequelObject::DB.create_table(:clients) do
6
+ primary_key(:id)
7
+ String(:name)
8
+ String(:digest_password)
9
+ index(:name)
10
+ # timestamps
11
+ datetime(:created)
12
+ datetime(:updated)
13
+ end
14
+ end
15
+ end
@@ -3,7 +3,7 @@ require 'date'
3
3
 
4
4
  module Rubee
5
5
  module AuthTokenable
6
- KEY = "secret#{Date.today}".freeze unless defined?(KEY) # Feel free to cusomtize it
6
+ KEY ="secret#{ENV['JWT_KEY']}#{Date.today}".freeze unless defined?(KEY) # Feel free to cusomtize it
7
7
  EXPIRE = 3600 unless defined?(EXPIRE)
8
8
 
9
9
  def self.included(base)
@@ -27,10 +27,10 @@ module Rubee
27
27
  @request.env['rack.session']&.[]('authentificated')
28
28
  end
29
29
 
30
- def authentificated_user
31
- # User model must be created with email and password properties at least
32
- if params[:email] && params[:password]
33
- @authentificated_user ||= ::User.where(email: params[:email], password: params[:password]).first
30
+ def authentificated_user(user_model: ::User, login: :email, password: :password)
31
+ if params[login] && params[password]
32
+ query_params = { login => params[login], password => params[password] }
33
+ @authentificated_user ||= user_model.where(query_params).first
34
34
  elsif @request.cookies['jwt'] && valid_token?
35
35
  token = @request.cookies['jwt']
36
36
  hash = ::JWT.decode(token, Rubee::AuthTokenable::KEY, true, { algorithm: 'HS256' })
@@ -38,11 +38,11 @@ module Rubee
38
38
  end
39
39
  end
40
40
 
41
- def authentificate!
42
- return false unless authentificated_user
41
+ def authentificate!(user_model: ::User, login: :email, password: :password)
42
+ return false unless authentificated_user(user_model:, login:, password:)
43
43
 
44
44
  # Generate token
45
- payload = { username: params[:email], exp: Time.now.to_i + EXPIRE }
45
+ payload = { username: params[login], exp: Time.now.to_i + EXPIRE }
46
46
  @token = ::JWT.encode(payload, KEY, 'HS256')
47
47
  # Set jwt token to the browser within cookie, so next browser request will include it.
48
48
  # make sure it passed to response_with headers options
data/lib/rubee.rb CHANGED
@@ -17,7 +17,7 @@ module Rubee
17
17
  CSS_DIR = File.join(APP_ROOT, LIB, 'css') unless defined?(CSS_DIR)
18
18
  ROOT_PATH = File.expand_path(File.join(__dir__, '..')) unless defined?(ROOT_PATH)
19
19
 
20
- VERSION = '2.1.1'
20
+ VERSION = '2.2.0'
21
21
 
22
22
  require_relative 'rubee/router'
23
23
  require_relative 'rubee/logger'
@@ -1,5 +1,55 @@
1
1
  require_relative '../test_helper'
2
2
 
3
+ class TestController < Rubee::BaseController
4
+ include(Rubee::AuthTokenable)
5
+ auth_methods(:show)
6
+ def show
7
+ response_with(type: :json, object: { ok: :ok })
8
+ end
9
+
10
+ # POST /test/login (login logic)
11
+ def login
12
+ if authentificate! # AuthTokenable method that init @token_header
13
+ # Redirect to restricted area, make sure headers: @token_header is passed
14
+ response_with(type: :json, object: { ok: :ok }, headers: @token_header)
15
+ else
16
+ @error = "Wrong email or password"
17
+ response_with(type: :json, object: { error: 'user unauthenticated' }, status: :unauthenticated)
18
+ end
19
+ end
20
+
21
+ # POST /test/logout (logout logic)
22
+ def logout
23
+ unauthentificate! # AuthTokenable method aimed to handle logout action.
24
+ # Make sure @zeroed_token_header is paRssed within headers options
25
+ response_with(type: :json, object: { ok: 'logged out' }, headers: @zeroed_token_header)
26
+ end
27
+ end
28
+
29
+ class TesttwoController < Rubee::BaseController
30
+ include(Rubee::AuthTokenable)
31
+ auth_methods(:show)
32
+ def show
33
+ response_with(type: :json, object: { ok: :ok })
34
+ end
35
+
36
+ # POST /testtwo/login (login logic)
37
+ def login
38
+ if authentificate!(user_model: Client, login: :name, password: :digest_password)
39
+ response_with(type: :json, object: { ok: :ok }, headers: @token_header)
40
+ else
41
+ @error = "Wrong email or password"
42
+ response_with(type: :json, object: { error: 'user unauthenticated' }, status: :unauthenticated)
43
+ end
44
+ end
45
+
46
+ # POST /testtwo/logout (logout logic)
47
+ def logout
48
+ unauthentificate!(user_model: Client, login: :name, password: :digest_password)
49
+ response_with(type: :json, object: { ok: 'logged out' }, headers: @zeroed_token_header)
50
+ end
51
+ end
52
+
3
53
  class AuthTokenableTest < Minitest::Test
4
54
  include Rack::Test::Methods
5
55
 
@@ -9,21 +59,43 @@ class AuthTokenableTest < Minitest::Test
9
59
 
10
60
  def setup
11
61
  Rubee::Autoload.call
62
+ Rubee::Router.draw do |route|
63
+ route.post('/test/login', to: 'test#login')
64
+ route.post('/test/logout', to: 'test#logout')
65
+ route.get('/test/show', to: 'test#show')
66
+ route.post('/testtwo/login', to: 'testtwo#login')
67
+ route.post('/testtwo/logout', to: 'testtwo#logout')
68
+ route.get('/testtwo/show', to: 'testtwo#show')
69
+ end
70
+ User.create(email: '9oU8S@example.com', password: '123456')
71
+ Client.create(name: '9oU8S@example.com', digest_password: '123456')
12
72
  end
13
73
 
14
- def teardown
15
- # detach auth methods
16
- return unless WelcomeController.instance_variable_defined?(:@auth_methods)
74
+ def test_test_controller_included_auth_tokenable
75
+ get('/test/show')
17
76
 
18
- WelcomeController.send(:remove_instance_variable, :@auth_methods)
77
+ assert_equal(last_response.status, 401)
19
78
  end
20
79
 
21
- def test_welcome_controller_included_auth_tokenable
22
- WelcomeController.include(Rubee::AuthTokenable)
23
- WelcomeController.auth_methods(:show)
80
+ def test_test_controller_included_auth_tokenable_authenticated
81
+ post('/test/login', { email: '9oU8S@example.com', password: '123456' })
82
+ rack_mock_session.cookie_jar["jwt"] = last_response.cookies["jwt"].value.last
83
+ get('/test/show')
24
84
 
25
- get('/')
85
+ assert_equal(last_response.status, 200)
86
+ end
87
+
88
+ def test_test_controller_included_auth_tokenable_unauthenticated_custom_model
89
+ get('/testtwo/show')
26
90
 
27
91
  assert_equal(last_response.status, 401)
28
92
  end
93
+
94
+ def test_test_controller_included_auth_tokenable_authenticated_custom_model
95
+ post('/testtwo/login', { name: '9oU8S@example.com', digest_password: '123456' })
96
+ rack_mock_session.cookie_jar["jwt"] = last_response.cookies["jwt"].value.last
97
+ get('/testtwo/show')
98
+
99
+ assert_equal(last_response.status, 200)
100
+ end
29
101
  end
@@ -0,0 +1,3 @@
1
+ class Client < Rubee::SequelObject
2
+ attr_accessor :id, :name, :digest_password, :created, :updated
3
+ end
data/lib/tests/test.db CHANGED
Binary file
data/readme.md CHANGED
@@ -151,14 +151,14 @@ cd my_project
151
151
 
152
152
  ***Prerequisites***<br />
153
153
  Make sure:
154
- **Ruby** language (3.1>) is installed
154
+ **Ruby** language (3.1 or higher, 3.4.1 recommended) is installed
155
155
  **Bundler** is installed
156
156
 
157
157
  ```bash
158
158
  bundle install
159
159
  ```
160
160
 
161
- 4. Run RUBER server. Default port is 7000
161
+ 4. Run ru.Bee server. Default port is 7000
162
162
  ```bash
163
163
  rubee start # or rubee start_dev for development
164
164
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ru.Bee
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oleg Saltykov
@@ -58,6 +58,7 @@ files:
58
58
  - lib/css/app.css
59
59
  - lib/db/create_accounts.rb
60
60
  - lib/db/create_addresses.rb
61
+ - lib/db/create_clients.rb
61
62
  - lib/db/create_comments.rb
62
63
  - lib/db/create_posts.rb
63
64
  - lib/db/create_users.rb
@@ -280,6 +281,7 @@ files:
280
281
  - lib/tests/controllers/users_controller_test.rb
281
282
  - lib/tests/example_models/account.rb
282
283
  - lib/tests/example_models/address.rb
284
+ - lib/tests/example_models/client.rb
283
285
  - lib/tests/example_models/comment.rb
284
286
  - lib/tests/example_models/post.rb
285
287
  - lib/tests/example_models/user.rb