rodauth-pat 0.1.1

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: fa9378550a45520ae988790ca9be8fb99f06a5546dc3ba4bac053956f71a264f
4
+ data.tar.gz: d6e056f4dedf6d79e8dd1ac9fb89abcbf2db1faa59d0dff8620722851244575e
5
+ SHA512:
6
+ metadata.gz: 571f8e21905e4350c097c2a8a6d99245c917f7eadc7a57f296a475cd0017ec971668101ec455ba439c4ded466f42ffab4a0f4a4e7dd48743d1cfefc0316b7602
7
+ data.tar.gz: 7f7b3214370c874608918282f7bc53caff7f09632a3099e4f524dfc0619a5d73aba167ce64f186a452dd31c7f5916767ed68319f027cc33f66997e7a8361d4b9
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --require spec_helper
2
+ --color
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ ## [0.1.1] - 2023-07-24
2
+
3
+ - Initial release
4
+
5
+ ## [0.1.0] - 2023-03-25
6
+
7
+ - Unpublished setup
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
6
+
7
+ gem "rake"
8
+ gem "rackup"
9
+
10
+ gem "rspec"
11
+ gem "capybara"
12
+ gem "bcrypt"
13
+ gem "tilt"
14
+ gem "sqlite3"
data/Gemfile.lock ADDED
@@ -0,0 +1,81 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rodauth-pat (0.1.0)
5
+ rodauth (~> 2.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.8.4)
11
+ public_suffix (>= 2.0.2, < 6.0)
12
+ bcrypt (3.1.19)
13
+ capybara (3.39.2)
14
+ addressable
15
+ matrix
16
+ mini_mime (>= 0.1.3)
17
+ nokogiri (~> 1.8)
18
+ rack (>= 1.6.0)
19
+ rack-test (>= 0.6.3)
20
+ regexp_parser (>= 1.5, < 3.0)
21
+ xpath (~> 3.2)
22
+ diff-lcs (1.5.0)
23
+ matrix (0.4.2)
24
+ mini_mime (1.1.2)
25
+ nokogiri (1.15.3-x86_64-darwin)
26
+ racc (~> 1.4)
27
+ nokogiri (1.15.3-x86_64-linux)
28
+ racc (~> 1.4)
29
+ public_suffix (5.0.3)
30
+ racc (1.7.1)
31
+ rack (3.0.8)
32
+ rack-test (2.1.0)
33
+ rack (>= 1.3)
34
+ rackup (2.1.0)
35
+ rack (>= 3)
36
+ webrick (~> 1.8)
37
+ rake (13.0.6)
38
+ regexp_parser (2.8.1)
39
+ roda (3.70.0)
40
+ rack
41
+ rodauth (2.30.0)
42
+ roda (>= 2.6.0)
43
+ sequel (>= 4)
44
+ rspec (3.12.0)
45
+ rspec-core (~> 3.12.0)
46
+ rspec-expectations (~> 3.12.0)
47
+ rspec-mocks (~> 3.12.0)
48
+ rspec-core (3.12.2)
49
+ rspec-support (~> 3.12.0)
50
+ rspec-expectations (3.12.3)
51
+ diff-lcs (>= 1.2.0, < 2.0)
52
+ rspec-support (~> 3.12.0)
53
+ rspec-mocks (3.12.6)
54
+ diff-lcs (>= 1.2.0, < 2.0)
55
+ rspec-support (~> 3.12.0)
56
+ rspec-support (3.12.1)
57
+ sequel (5.70.0)
58
+ sqlite3 (1.6.3-x86_64-darwin)
59
+ sqlite3 (1.6.3-x86_64-linux)
60
+ tilt (2.2.0)
61
+ webrick (1.8.1)
62
+ xpath (3.2.0)
63
+ nokogiri (~> 1.8)
64
+
65
+ PLATFORMS
66
+ x86_64-darwin-19
67
+ x86_64-linux
68
+ x86_64-linux-musl
69
+
70
+ DEPENDENCIES
71
+ bcrypt
72
+ capybara
73
+ rackup
74
+ rake
75
+ rodauth-pat!
76
+ rspec
77
+ sqlite3
78
+ tilt
79
+
80
+ BUNDLED WITH
81
+ 2.4.17
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Jonas Mueller
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,62 @@
1
+ # Personal Access Tokens for Rodauth
2
+
3
+ This is an extension to the `rodauth` gem which implements Personal
4
+ Access Tokens for an authorization server.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ``` ruby
11
+ gem 'rodauth-pat'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ ``` sh
17
+ $ bundle install
18
+ ```
19
+
20
+ Or install it yourself as:
21
+
22
+ ``` sh
23
+ $ gem install rodauth-pat
24
+ ```
25
+
26
+ ## Usage
27
+
28
+ After setting up basic `rodauth`, you can enable the feature:
29
+
30
+ ``` ruby
31
+ plugin :rodauth do
32
+ enable :login, :personal_access_tokens
33
+ end
34
+
35
+ # then, inside roda
36
+
37
+ route do |r|
38
+ r.rodauth
39
+
40
+ # This will setup 3 routes for management of the Personal Access Tokens:
41
+ # There are not strictly required for operation of #require_token_authentication
42
+ #
43
+ # * /personal-access-tokens Show non-revoked tokens
44
+ # * /personal_access_tokens/:id/revoke Revoke existing tokens
45
+ # * /personal_access_tokens/new Create new tokens
46
+ rodauth.load_personal_access_token_routes
47
+
48
+ r.get "public" do
49
+ "public!"
50
+ end
51
+
52
+ r.get "protected" do
53
+ rodauth.require_authentication
54
+ "secret!"
55
+ end
56
+
57
+ r.get "api" do
58
+ rodauth.require_token_authentication
59
+ "secret with api!"
60
+ end
61
+ end
62
+ ```
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "rake"
2
+ require "rspec/core/rake_task"
3
+
4
+ require "bundler/gem_tasks"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/example/app.rb ADDED
@@ -0,0 +1,64 @@
1
+ require "roda"
2
+ require "sequel/core"
3
+ require "securerandom"
4
+ require "net/http"
5
+ require "bcrypt"
6
+ require "digest/sha1"
7
+
8
+ $LOAD_PATH.unshift(File.expand_path("../lib", __dir__))
9
+
10
+ DB = Sequel.connect("sqlite:/", identifier_mangling: false)
11
+ Sequel.extension :migration
12
+ Sequel::Migrator.run(DB, File.expand_path("../spec/migrate", __dir__))
13
+
14
+ hash = BCrypt::Password.create("password", cost: BCrypt::Engine::MIN_COST)
15
+ DB[:accounts].insert_conflict(target: :email).insert(email: "foo@bar.com", ph: hash)
16
+
17
+ class PersonalAccessTokenApp < Roda
18
+ SECRET = SecureRandom.base64(64)
19
+
20
+ plugin :flash
21
+ plugin :common_logger
22
+ plugin :sessions, secret: SECRET
23
+ plugin :render, layout_opts: { inline: <<~EOS }
24
+ <%= flash["notice"] %>
25
+ <%= flash["error"] %>
26
+ <%= yield %>
27
+ EOS
28
+
29
+ plugin :rodauth do
30
+ db DB
31
+ enable :login
32
+ enable :personal_access_tokens
33
+ account_password_hash_column :ph
34
+ login_return_to_requested_location? true
35
+ hmac_secret SECRET
36
+ end
37
+
38
+ plugin :not_found do
39
+ "Not Found: #{request.path_info}"
40
+ end
41
+
42
+ route do |r|
43
+ r.rodauth
44
+ rodauth.load_personal_access_token_routes
45
+
46
+ r.root do
47
+ view inline: <<~HTML
48
+ This is the root path!<br>
49
+ <a href="/personal-access-tokens">Tokens</a>
50
+ HTML
51
+ end
52
+
53
+ end
54
+ end
55
+
56
+ DB.freeze
57
+
58
+ if $0 == __FILE__
59
+ require "rackup"
60
+
61
+ Rackup::Server.start \
62
+ app: PersonalAccessTokenApp,
63
+ Port: 9292
64
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rodauth
4
+ module PersonalAccessTokens
5
+ VERSION = "0.1.1"
6
+ end
7
+ end
@@ -0,0 +1,147 @@
1
+ require "rodauth"
2
+
3
+ require_relative "personal_access_tokens/version"
4
+
5
+ module Rodauth
6
+ Feature.define(:personal_access_tokens, :PersonalAccessTokens) do
7
+ depends :base, :login
8
+
9
+ auth_value_method :personal_access_tokens_table_name, :personal_access_tokens
10
+ auth_value_method :personal_access_token_name_param, "name"
11
+ auth_value_method :personal_access_tokens_account_id_column, :account_id
12
+ auth_value_method :personal_access_tokens_name_column, :name
13
+ auth_value_method :personal_access_tokens_digest_column, :digest
14
+ auth_value_method :personal_access_tokens_error_status, 401
15
+ auth_value_method :personal_access_tokens_route, "personal-access-tokens"
16
+ auth_value_method :personal_access_tokens_revoke_route, "revoke"
17
+ auth_value_method :personal_access_tokens_new_route, "new"
18
+ auth_value_method :personal_access_tokens_error_body, "Unauthorized"
19
+ auth_value_method :personal_access_tokens_expires_column, :expires_at
20
+ auth_value_method :personal_access_tokens_revoked_column, :revoked_at
21
+ auth_value_method :personal_access_tokens_validity, (60 * 60 * 24 * 365)
22
+ auth_value_method :personal_access_tokens_header_regexp, /\ABearer: (\w+)/
23
+
24
+ loaded_templates %w(
25
+ personal_access_tokens
26
+ new_personal_access_token
27
+ revoke_personal_access_token
28
+ )
29
+
30
+ view "personal-access-tokens", "Personal Access Tokens", "personal_access_tokens"
31
+ view "revoke-personal-access-token", "Revoke Personal Access Token", "revoke_personal_access_token"
32
+ view "new-personal-access-token", "New Personal Access Token", "new_personal_access_token"
33
+
34
+ additional_form_tags
35
+ button "Create", "new_personal_access_token"
36
+ button "Revoke", "revoke_personal_access_token"
37
+ redirect
38
+
39
+ def personal_access_tokens_path
40
+ route_path(personal_access_tokens_route)
41
+ end
42
+
43
+ def revoke_personal_access_token_path(id)
44
+ "#{personal_access_tokens_path}/#{id}/#{personal_access_tokens_revoke_route}"
45
+ end
46
+
47
+ def new_personal_access_token_path
48
+ "#{personal_access_tokens_path}/#{personal_access_tokens_new_route}"
49
+ end
50
+
51
+ def load_personal_access_token_routes
52
+ request.on(personal_access_tokens_route) do
53
+ check_csrf if check_csrf?
54
+ require_account
55
+
56
+ request.is(true) do
57
+ personal_access_tokens_view
58
+ end
59
+
60
+ request.is(personal_access_tokens_new_route) do
61
+ request.get do
62
+ new_personal_access_token_view
63
+ end
64
+
65
+ request.post do
66
+ key = create_key
67
+ name = param(personal_access_token_name_param)
68
+ insert_token(name, key)
69
+ set_notice_flash "Success! New token (#{name}): #{key}"
70
+ redirect personal_access_tokens_path
71
+ end
72
+ end
73
+
74
+ request.on Integer do |id|
75
+ next unless token = account_personal_access_tokens_ds.first(id: id)
76
+
77
+ scope.instance_variable_set(:@token, token)
78
+
79
+ request.is(personal_access_tokens_revoke_route) do
80
+ request.get do
81
+ revoke_personal_access_token_view
82
+ end
83
+
84
+ request.post do
85
+ account_personal_access_tokens_ds
86
+ .where(id: id)
87
+ .update(revoked_at: Time.now)
88
+ set_notice_flash "Success! Token #{token[:name]} revoked"
89
+ redirect personal_access_tokens_path
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+
96
+ def require_token_authentication
97
+ return if token_valid?(request.env["HTTP_AUTHENTICATION"])
98
+
99
+ request.halt [
100
+ personal_access_tokens_error_status,
101
+ {},
102
+ personal_access_tokens_error_body
103
+ ]
104
+ end
105
+
106
+ def insert_token(name, key)
107
+ DB[personal_access_tokens_table_name].insert \
108
+ personal_access_tokens_account_id_column => account_from_session[account_id_column],
109
+ personal_access_tokens_name_column => name,
110
+ personal_access_tokens_digest_column => compute_hmac(key),
111
+ personal_access_tokens_expires_column => Time.now + personal_access_tokens_validity
112
+ end
113
+
114
+ def template_path(page)
115
+ path = File.join(File.dirname(__FILE__), "../../../templates", "#{page}.str")
116
+ return super unless File.exist?(path)
117
+
118
+ path
119
+ end
120
+
121
+ def account_personal_access_tokens_ds
122
+ DB[personal_access_tokens_table_name]
123
+ .where(personal_access_tokens_account_id_column => account_from_session[account_id_column])
124
+ end
125
+
126
+ def create_key
127
+ SecureRandom.alphanumeric(20)
128
+ end
129
+
130
+ def account_personal_access_tokens
131
+ account_personal_access_tokens_ds
132
+ .where(personal_access_tokens_revoked_column => nil)
133
+ .all
134
+ end
135
+
136
+ def token_valid?(header)
137
+ return false unless header
138
+ return false unless key = header[personal_access_tokens_header_regexp, 1]
139
+
140
+ !!DB[personal_access_tokens_table_name]
141
+ .where(Sequel::CURRENT_TIMESTAMP < personal_access_tokens_expires_column)
142
+ .where(personal_access_tokens_revoked_column => nil)
143
+ .first(personal_access_tokens_digest_column => compute_hmac(key))
144
+ end
145
+
146
+ end
147
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/rodauth/features/personal_access_tokens/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "rodauth-pat"
7
+ spec.version = Rodauth::PersonalAccessTokens::VERSION
8
+ spec.authors = ["Jonas Mueller"]
9
+ spec.email = ["jonas@tigger.cloud"]
10
+
11
+ spec.summary = "Implementation of personal access tokens oon top of rodauth."
12
+ spec.description = "Implementation of personal access tokens on top of rodauth."
13
+ spec.homepage = "https://github.com/muellerj/rodauth-pat"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.7.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+ spec.metadata["changelog_uri"] = spec.homepage + "/blob/master/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(__dir__) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
26
+ end
27
+ end
28
+
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_dependency "rodauth", "~> 2.0"
32
+ end
@@ -0,0 +1,13 @@
1
+ <h1>New Personal Access Token</h1>
2
+
3
+ <form method="POST">
4
+ #{rodauth.personal_access_tokens_additional_form_tags}
5
+ #{rodauth.csrf_tag}
6
+ <div>
7
+ <label for="name">Name</label>
8
+ #{rodauth.input_field_string(rodauth.personal_access_token_name_param, 'name')}
9
+ </div>
10
+
11
+ <a href="#{rodauth.personal_access_tokens_path}">Back</a>
12
+ #{rodauth.button(rodauth.new_personal_access_token_button)}
13
+ </form>
@@ -0,0 +1,11 @@
1
+ <h1>My Personal Access Tokens</h1>
2
+
3
+ <ul>
4
+ #{
5
+ rodauth.account_personal_access_tokens.map do |token|
6
+ "<li>" + token.inspect + "<a href='#{rodauth.revoke_personal_access_token_path(token[:id])}'>Revoke</a></li>"
7
+ end.join("\n")
8
+ }
9
+ </ul>
10
+
11
+ <a href="#{rodauth.new_personal_access_token_path}">New Token</a>
@@ -0,0 +1,14 @@
1
+ <h1>Revoke Personal Access Token</h1>
2
+
3
+ <p>
4
+ Are you sure you want to revoke token <strong>#{@token[:name]}</strong>?
5
+ This will immediately terminate all access using this token and cannot be undone.
6
+ </p>
7
+
8
+ <form method="POST">
9
+ #{rodauth.personal_access_tokens_additional_form_tags}
10
+ #{rodauth.csrf_tag}
11
+
12
+ <a href="#{rodauth.personal_access_tokens_path}">Back</a>
13
+ #{rodauth.button(rodauth.revoke_personal_access_token_button)}
14
+ </form>
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rodauth-pat
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Jonas Mueller
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-07-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rodauth
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ description: Implementation of personal access tokens on top of rodauth.
28
+ email:
29
+ - jonas@tigger.cloud
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".rspec"
35
+ - CHANGELOG.md
36
+ - Gemfile
37
+ - Gemfile.lock
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - example/app.rb
42
+ - lib/rodauth/features/personal_access_tokens.rb
43
+ - lib/rodauth/features/personal_access_tokens/version.rb
44
+ - rodauth-pat.gemspec
45
+ - templates/new-personal-access-token.str
46
+ - templates/personal-access-tokens.str
47
+ - templates/revoke-personal-access-token.str
48
+ homepage: https://github.com/muellerj/rodauth-pat
49
+ licenses:
50
+ - MIT
51
+ metadata:
52
+ homepage_uri: https://github.com/muellerj/rodauth-pat
53
+ source_code_uri: https://github.com/muellerj/rodauth-pat
54
+ changelog_uri: https://github.com/muellerj/rodauth-pat/blob/master/CHANGELOG.md
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 2.7.0
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.4.10
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Implementation of personal access tokens oon top of rodauth.
74
+ test_files: []