tinytokenauth-rails 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 56710adbeea3a54def8b6280ba56d760316061457d30c6c76938068d855b2f86
4
+ data.tar.gz: 578dd2d02f006c3d8e0ab4b016c77e434eb22c82910d00097238c021aff6c23d
5
+ SHA512:
6
+ metadata.gz: 7895965842d6a660960f1c12bb9b73aaaf0b3d1f48e354e48aa0d5d26905e5c50b429ac56e0388df36bc36390596c0c436f9b058f86bb7a062f79763f7fe004c
7
+ data.tar.gz: e46643638fec5fd36587be45754a75ceeb4af27bc8f7b2ffece2e461c1095ce4db115a4e6af0ec219e7133fecddd79b4ba2e78e71e4f7a453a2965e5b3c187b0
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/testdouble/standard
3
+ ruby_version: 2.6
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-10-29
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in tinytokenauth-rails.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "standard", "~> 1.3"
data/Gemfile.lock ADDED
@@ -0,0 +1,83 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ tinytokenauth-rails (0.1.0)
5
+ bcrypt (~> 3.1)
6
+ jwt (~> 2.7)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ ast (2.4.2)
12
+ base64 (0.1.1)
13
+ bcrypt (3.1.19)
14
+ diff-lcs (1.5.0)
15
+ json (2.6.3)
16
+ jwt (2.7.1)
17
+ language_server-protocol (3.17.0.3)
18
+ lint_roller (1.1.0)
19
+ parallel (1.23.0)
20
+ parser (3.2.2.4)
21
+ ast (~> 2.4.1)
22
+ racc
23
+ racc (1.7.1)
24
+ rainbow (3.1.1)
25
+ rake (13.0.6)
26
+ regexp_parser (2.8.2)
27
+ rexml (3.2.6)
28
+ rspec (3.12.0)
29
+ rspec-core (~> 3.12.0)
30
+ rspec-expectations (~> 3.12.0)
31
+ rspec-mocks (~> 3.12.0)
32
+ rspec-core (3.12.2)
33
+ rspec-support (~> 3.12.0)
34
+ rspec-expectations (3.12.3)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.12.0)
37
+ rspec-mocks (3.12.6)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.12.0)
40
+ rspec-support (3.12.1)
41
+ rubocop (1.56.4)
42
+ base64 (~> 0.1.1)
43
+ json (~> 2.3)
44
+ language_server-protocol (>= 3.17.0)
45
+ parallel (~> 1.10)
46
+ parser (>= 3.2.2.3)
47
+ rainbow (>= 2.2.2, < 4.0)
48
+ regexp_parser (>= 1.8, < 3.0)
49
+ rexml (>= 3.2.5, < 4.0)
50
+ rubocop-ast (>= 1.28.1, < 2.0)
51
+ ruby-progressbar (~> 1.7)
52
+ unicode-display_width (>= 2.4.0, < 3.0)
53
+ rubocop-ast (1.30.0)
54
+ parser (>= 3.2.1.0)
55
+ rubocop-performance (1.19.1)
56
+ rubocop (>= 1.7.0, < 2.0)
57
+ rubocop-ast (>= 0.4.0)
58
+ ruby-progressbar (1.13.0)
59
+ standard (1.31.2)
60
+ language_server-protocol (~> 3.17.0.2)
61
+ lint_roller (~> 1.0)
62
+ rubocop (~> 1.56.4)
63
+ standard-custom (~> 1.0.0)
64
+ standard-performance (~> 1.2)
65
+ standard-custom (1.0.2)
66
+ lint_roller (~> 1.0)
67
+ rubocop (~> 1.50)
68
+ standard-performance (1.2.1)
69
+ lint_roller (~> 1.1)
70
+ rubocop-performance (~> 1.19.1)
71
+ unicode-display_width (2.5.0)
72
+
73
+ PLATFORMS
74
+ arm64-darwin-22
75
+
76
+ DEPENDENCIES
77
+ rake (~> 13.0)
78
+ rspec (~> 3.0)
79
+ standard (~> 1.3)
80
+ tinytokenauth-rails!
81
+
82
+ BUNDLED WITH
83
+ 2.4.15
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Kim Laplume
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # Tinytokenauth::Rails
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.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add tinytokenauth-rails
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install tinytokenauth-rails
16
+
17
+ ## Usage
18
+
19
+ TODO: Write usage instructions here
20
+
21
+ ## Development
22
+
23
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
24
+
25
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/tinytokenauth-rails.
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -0,0 +1,134 @@
1
+ module Tinytokenauth
2
+ module Rails
3
+ module Authorizable
4
+ # class Configuration
5
+ # attr_accessor :token_duration
6
+ # end
7
+
8
+ class << self
9
+ def configuration
10
+ @configuration ||= Configuration.new
11
+ end
12
+
13
+ def configure
14
+ yield(configuration)
15
+ end
16
+ end
17
+
18
+ def authorize_with_header(klass = User)
19
+ token = ''
20
+ header = request.headers['Authorization']
21
+ token = header.split(' ').last if header
22
+ # p "token from header: #{token}"
23
+
24
+ begin
25
+ @decoded = JsonWebToken.decode(token)
26
+ # @current_user = User.find(@decoded[:user_id])
27
+ @current_user = klass.send 'find', @decoded[:user_id]
28
+ rescue ActiveRecord::RecordNotFound => e
29
+ render json: { errors: e.message }, status: :unauthorized
30
+ rescue JWT::DecodeError => e
31
+ render json: { errors: e.message }, status: :unauthorized
32
+ end
33
+ end
34
+
35
+ # Tinytokenauth.configure do |config|
36
+ # config.token_duration = 24.hours
37
+ # end
38
+
39
+ def require_current_user(klass = User)
40
+ token = cookies['klap-auth']
41
+ # p "token from cookie: #{token}"
42
+
43
+ begin
44
+ @decoded = JsonWebToken.decode(token)
45
+ # @current_user = User.find(@decoded[:user_id])
46
+ @current_user = klass.send 'find', @decoded[:user_id]
47
+ @exp = @decoded[:exp]
48
+ #if @exp < 24.hours.from_now.to_i # Always refresh token
49
+ if @exp < 4.hours.from_now.to_i # Always refresh token
50
+ sign_in @current_user
51
+ end
52
+ rescue ActiveRecord::RecordNotFound => e
53
+ # TODO: evaluate if we should always forward
54
+ redirect_to new_session_path(forward_to: request.path), notice: "Please sign in again" #, status: :unauthorized
55
+ rescue JWT::DecodeError => e
56
+ # TODO: evaluate if we should always forward
57
+ # render json: { errors: e.message }, status: :unauthorized
58
+ redirect_to new_session_path(forward_to: request.path), notice: "Please sign in again" #, status: :unauthorized
59
+ end
60
+ end
61
+
62
+ # def require_current_user2(klass = User, &block)
63
+ # current_user = set_current_user(klass)
64
+ # if block_given? && current_user.nil?
65
+ # block.call
66
+ # else
67
+ # raise MissingArgumentError
68
+ # end
69
+ # end
70
+
71
+ def require_current_user2(klass = User, &block)
72
+ token = cookies['klap-auth']
73
+ # p "token from cookie: #{token}"
74
+ begin
75
+ @decoded = JsonWebToken.decode(token)
76
+ # @current_user = User.find(@decoded[:user_id])
77
+ @current_user = klass.send 'find', @decoded[:user_id]
78
+ @exp = @decoded[:exp]
79
+ #if @exp < 24.hours.from_now.to_i # Always refresh token
80
+ if @exp < 4.hours.from_now.to_i # Always refresh token
81
+ sign_in @current_user
82
+ end
83
+ rescue ActiveRecord::RecordNotFound, JWT::DecodeError => e
84
+ if block_given? && current_user.nil?
85
+ block.call
86
+ else
87
+ raise e
88
+ end
89
+ end
90
+ end
91
+
92
+ def set_current_user(klass = User)
93
+ token = cookies['klap-auth']
94
+ begin
95
+ @decoded = JsonWebToken.decode(token)
96
+ # @current_user = User.find(@decoded[:user_id])
97
+ @current_user = klass.send 'find', @decoded[:user_id]
98
+ @exp = @decoded[:exp]
99
+ #if @exp < 24.hours.from_now.to_i # Always refresh token
100
+ if @exp < 4.hours.from_now.to_i # Always refresh token
101
+ # token = JsonWebToken.encode(user_id: @current_user.id)
102
+ # cookies['klap-auth'] = token
103
+ sign_in @current_user
104
+ end
105
+ rescue ActiveRecord::RecordNotFound
106
+ # Ignored
107
+ rescue JWT::DecodeError
108
+ # Ignored
109
+ end
110
+ @current_user
111
+ end
112
+
113
+ def set_current_user2(klass = User)
114
+ begin
115
+ require_current_user2(klass)
116
+ rescue ActiveRecord::RecordNotFound
117
+ # Ignored
118
+ rescue JWT::DecodeError
119
+ # Ignored
120
+ end
121
+ @current_user
122
+ end
123
+
124
+ def sign_in(user)
125
+ @current_user = user
126
+ cookies['klap-auth'] = JsonWebToken.encode(user_id: user.id)
127
+ end
128
+
129
+ def current_user
130
+ @current_user
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,7 @@
1
+ module Tinytokenauth
2
+ module Rails
3
+ class Configuration
4
+ attr_accessor :token_duration
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ module Tinytokenauth
2
+ module Rails
3
+ class Engine < Rails::Engine
4
+ initializer "tinytokenauth-rails.view_helpers" do
5
+ ActiveSupport.on_load(:action_view) do
6
+ include ViewHelpers
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tinytokenauth
4
+ module Rails
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ module Tinytokenauth
2
+ module Rails
3
+ module ViewHelpers
4
+ def current_user
5
+ @current_user
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "rails/version"
4
+
5
+ require_relative "rails/authorizable"
6
+ require_relative "rails/railtie"
7
+ require_relative "rails/view_helpers"
8
+
9
+ # module Tinytokenauth
10
+ # module Rails
11
+ # class Error < StandardError; end
12
+ # # Your code goes here...
13
+ # end
14
+ # end
@@ -0,0 +1,6 @@
1
+ module Tinytokenauth
2
+ module Rails
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tinytokenauth-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kim Laplume
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-10-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jwt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bcrypt
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.1'
41
+ description: Soon
42
+ email:
43
+ - kim.laplume@protonmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".rspec"
49
+ - ".standard.yml"
50
+ - CHANGELOG.md
51
+ - Gemfile
52
+ - Gemfile.lock
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - lib/tinytokenauth/rails.rb
57
+ - lib/tinytokenauth/rails/authorizable.rb
58
+ - lib/tinytokenauth/rails/configuration.rb
59
+ - lib/tinytokenauth/rails/railtie.rb
60
+ - lib/tinytokenauth/rails/version.rb
61
+ - lib/tinytokenauth/rails/view_helpers.rb
62
+ - sig/tinytokenauth/rails.rbs
63
+ homepage: https://github.com/1klap/tinytokenauth-rails
64
+ licenses:
65
+ - MIT
66
+ metadata:
67
+ allowed_push_host: https://rubygems.org
68
+ homepage_uri: https://github.com/1klap/tinytokenauth-rails
69
+ source_code_uri: https://github.com/1klap/tinytokenauth-rails
70
+ changelog_uri: https://github.com/1klap/tinytokenauth-rails/blob/main/CHANGELOG.md
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: 2.6.0
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubygems_version: 3.4.1
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: Soon
90
+ test_files: []