tinytokenauth-rails 0.1.11 → 0.90.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: f6baf29c41c8a1e86f24609a31cf2331a1cbed8a9206c9f65acf3bf39e140d19
4
- data.tar.gz: 8f886cd34190b7177e8264b87f99bb08c208e689d61489abae911e004cbee358
3
+ metadata.gz: eb8e1dd95f2f3b5a573ea6cdf6a4a5e628b3ef6d7066f33433e72389dda9f9a2
4
+ data.tar.gz: bb891e4c277d766c82a1bac13153e58dfc6f54e4a48165d24eca0aede90508b1
5
5
  SHA512:
6
- metadata.gz: b6878943a558518f36e2939ec891e03019d3b7646ca360729ac9a324c4b58e1aff1408f26a2cc425272bbf10327c0bc9f2e2e7bb2b1f28400ab56c4557754ff2
7
- data.tar.gz: 254048055fb0c78ec3dc5343dfe3297cf9df3c87df2a6c62a8dea8af23b8c6277c2dcf3e100ee8867516b1ebb65cf37016654e9eb0f2b2b0dca381310d2c7cda
6
+ metadata.gz: b35c4590d997dada90621046be56aa91218fe6c3b5c27a35f81466df4d7d92729210896e56974c88978b410d14df626a9a840f4763907ccf0a501cfa5e17c5de
7
+ data.tar.gz: c73602893d2f2eb083cc8ac1d88607d2a25b4dde89930b084601b3f13a91da86d0fee95b3d48e4b148b9b446727e1ece69b38e3aba00a333cd8cb9409cf04550
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tinytokenauth-rails (0.1.11)
4
+ tinytokenauth-rails (0.90.0)
5
5
  jwt (~> 2.7)
6
6
  rails (>= 6.0)
7
7
 
data/README.md CHANGED
@@ -1,8 +1,11 @@
1
1
  # Tinytokenauth::Rails
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/tinytokenauth/rails`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ This gem wants to help you with user authentication without bloating up beyond what
4
+ is required. It uses a JWT (JSON Web Token) in a cookie to store session state in the browser.
4
5
 
5
- TODO: Delete this and the text above, and describe your gem
6
+ Since the JWT is signed with a secret, and this signature is verified server-side, the user can't
7
+ tamper with its contents. Its content is not encrypted, so frontend libraries can use this information
8
+ if they need to distinguish between 'signed in' vs 'signed out' state.
6
9
 
7
10
  ## Installation
8
11
 
@@ -16,7 +19,74 @@ If bundler is not being used to manage dependencies, install the gem by executin
16
19
 
17
20
  ## Usage
18
21
 
19
- TODO: Write usage instructions here
22
+ Include the module `Tinytokenauth::Authorizable` wherever you need to sign a user in/out or want to know if a user is signed in or not.
23
+ One option is to do this in `ApplicationController`, so the useful methods from this gem are available everywhere
24
+
25
+ ```ruby
26
+ # app/controller/application_controller.rb
27
+ class ApplicationController < ActionController::Base
28
+ include Tinytokenauth::Authorizable
29
+ before_action :set_current_user
30
+ end
31
+ ```
32
+
33
+ You will then have the user set in the variable `@current_user` or this will be nil if no user is signed in.
34
+
35
+ If a signed in user is required for some action, you can use the following pattern, the content of the block after
36
+ `require_current_user` is an example and depends on your project
37
+
38
+ ```ruby
39
+ class PostsController < ApplicationController
40
+ before_action ->{ require_current_user do
41
+ # new_session_path is a route you need to setup same for the controller
42
+ redirect_to new_session_path(forward_to: request.path), notice: "Please sign in again"
43
+ end }, only: [:new, :create]
44
+ # ...
45
+ end
46
+ ```
47
+
48
+ User authentication need to be managed by yourself, afterwards you can leverage the helper method to sign the user in with the token
49
+
50
+ Below is an example how you can handle this yourself
51
+
52
+ ```ruby
53
+ class SessionsController < ApplicationController
54
+
55
+ def new
56
+ end
57
+
58
+ def create
59
+ user = User.find_by_email(params[:email]) # This depends on your use case
60
+ if user&.authenticate(params[:password]) # This depends on your use case, this method comes from 'has_secure_password' in the model
61
+ sign_in_with_token user # THIS IS FROM Tinytokenauth
62
+ redirect_to params[:forward_to] || root_path, notice: 'Signed in!'
63
+ else
64
+ flash[:alert] = 'NOT signed in!'
65
+ render 'new', status: :unauthorized
66
+ end
67
+ end
68
+
69
+ def destroy
70
+ sign_out_with_token # THIS IS FROM Tinytokenauth
71
+ redirect_to params[:forward_to] || root_path, notice: 'Signed out!'
72
+ end
73
+ end
74
+ ```
75
+
76
+ If you want to configure the gem, please create a custom initializer like the one below. The values show below are the defaults
77
+
78
+ ```ruby
79
+ # config/initializers/tinytokenauth.rb
80
+ require 'tinytokenauth'
81
+
82
+ Tinytokenauth.configure do |config|
83
+ config.user_class = 'User' # what is your modal that needs to be checked for a signed in user?
84
+ config.token_validity_hours = 24 # how long should a token be valid?
85
+ config.token_secret = Rails.application.credentials.secret_key_base # with which secret is the JWT signed?
86
+ config.token_auto_renew_hours = 4 # if the token expires in less than X hours, renew it automatically
87
+ config.cookie_name = 'ttauth' # what should be the name of the cookie that stores the auth information
88
+ end
89
+ ```
20
90
 
21
91
  ## Development
22
92
 
@@ -32,11 +32,11 @@ module Tinytokenauth
32
32
  token = cookies[Tinytokenauth.configuration.cookie_name]
33
33
  begin
34
34
  @decoded = JsonWebToken.decode(Tinytokenauth.configuration.token_secret, token)
35
- @current_user = Tinytokenauth.configuration.user_class.constantize.send 'find', @decoded[:user_id]
35
+ @current_user = Tinytokenauth.configuration.user_class.constantize.send 'find', @decoded[:tinytokenauth_id]
36
36
  @exp = @decoded[:exp]
37
37
  if Tinytokenauth.configuration.token_auto_renew_hours &&
38
38
  @exp < Tinytokenauth.configuration.token_auto_renew_hours.hours.from_now.to_i
39
- sign_in @current_user
39
+ sign_in_with_token @current_user
40
40
  end
41
41
  rescue ActiveRecord::RecordNotFound, JWT::DecodeError => e
42
42
  if block_given? && current_user.nil?
@@ -58,14 +58,18 @@ module Tinytokenauth
58
58
  @current_user
59
59
  end
60
60
 
61
- def sign_in(user)
61
+ def sign_in_with_token(user)
62
62
  @current_user = user
63
63
  jwt = JsonWebToken.encode(Tinytokenauth.configuration.token_validity_hours.hours.from_now,
64
64
  Tinytokenauth.configuration.token_secret,
65
- user_id: user.id,)
65
+ tinytokenauth_id: user.id,)
66
66
  cookies[Tinytokenauth.configuration.cookie_name] = jwt
67
67
  end
68
68
 
69
+ def sign_out_with_token
70
+ cookies[Tinytokenauth.configuration.cookie_name] = nil
71
+ end
72
+
69
73
  def current_user
70
74
  @current_user
71
75
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tinytokenauth
4
- VERSION = "0.1.11"
4
+ VERSION = "0.90.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tinytokenauth-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 0.90.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kim Laplume
@@ -38,9 +38,11 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '2.7'
41
- description: Soon
41
+ description: |-
42
+ This gem wants to help you with user authentication without bloating up beyond what
43
+ is required. It uses a JWT (JSON Web Token) in a cookie to store session state in the browser.
42
44
  email:
43
- - kim.laplume@protonmail.com
45
+ - klap@hey.com
44
46
  executables: []
45
47
  extensions: []
46
48
  extra_rdoc_files: []
@@ -53,7 +55,6 @@ files:
53
55
  - LICENSE.txt
54
56
  - README.md
55
57
  - Rakefile
56
- - app/helpers/tinytokenauth-rails/current_user_helper.rb
57
58
  - lib/tinytokenauth.rb
58
59
  - lib/tinytokenauth/authorizable.rb
59
60
  - lib/tinytokenauth/configuration.rb
@@ -66,7 +67,6 @@ homepage: https://github.com/1klap/tinytokenauth-rails
66
67
  licenses:
67
68
  - MIT
68
69
  metadata:
69
- allowed_push_host: https://rubygems.org
70
70
  homepage_uri: https://github.com/1klap/tinytokenauth-rails
71
71
  source_code_uri: https://github.com/1klap/tinytokenauth-rails
72
72
  changelog_uri: https://github.com/1klap/tinytokenauth-rails/blob/main/CHANGELOG.md
@@ -88,5 +88,5 @@ requirements: []
88
88
  rubygems_version: 3.4.1
89
89
  signing_key:
90
90
  specification_version: 4
91
- summary: Soon
91
+ summary: Minimalistic JWT-based authentication that gets out of your way
92
92
  test_files: []
@@ -1,7 +0,0 @@
1
- module Tinytokenauth
2
- module CurrentUserHelper
3
- def foo(options = {}, &block)
4
- # render SimpleCalendar::Calendar.new(self, options), &block
5
- end
6
- end
7
- end