ruby_jwk 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: 23921b96849d38f01a575f59134abbe026c6848c1cbc4c460c27d92bcf77a809
4
+ data.tar.gz: b787be04a11e0ffbf0fe9f3c1418747589f60563a7067e8910a741b6b56a6335
5
+ SHA512:
6
+ metadata.gz: 1dab2c10dcb374bd786b21e244105bd1ef391416838f5736d6fc168a8dcf57a5ac19e3aaabeb37968f230a6bf8fd26b6e49cbb0e9027b79342443bdeaa06d400
7
+ data.tar.gz: d06b10422d2f8b94def06b55fe6653c818535bed789c83f5cafaf06ed74ca3807bded3fa14d2161f4294aea985cde3c653abb856e9764a3e40dfb18a0fc955b2
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2022 karthikeyan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # RubyJwk
2
+ Authenticate JWKs via public JWT enspoint
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'ruby_jwk'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install ruby_jwk
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ 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,8 @@
1
+ require "bundler/setup"
2
+
3
+ APP_RAKEFILE = File.expand_path("spec/dummy/Rakefile", __dir__)
4
+ load "rails/tasks/engine.rake"
5
+
6
+ load "rails/tasks/statistics.rake"
7
+
8
+ require "bundler/gem_tasks"
@@ -0,0 +1 @@
1
+ //= link_directory ../stylesheets/ruby_jwk .css
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,58 @@
1
+ module RubyJwk
2
+ module Authenticate
3
+ def authenticate_tenant!
4
+ return if RubyJwk.skip_issuers.to_a.include?(payload[:iss])
5
+
6
+ JWT.decode(token, nil, true, { algorithm: 'RS256', jwks: jwk_loader})
7
+ rescue JWT::DecodeError => e
8
+ render status: 401, json: error_response_template("Authentication failed! - #{e.message}")
9
+ end
10
+
11
+ def jwt_payload
12
+ @jwt_payload ||= JWT.decode(token, nil, false).first
13
+ rescue JWT::DecodeError => e
14
+ render status: 401, json: error_response_template("Authentication failed! - #{e.message}")
15
+ end
16
+
17
+ private
18
+
19
+ # invalidate becomes true when kid not found
20
+ def jwk_loader
21
+ ->(options) do
22
+ fetch_jwks(options[:invalidate])
23
+ end
24
+ end
25
+
26
+ def fetch_jwks(force = false)
27
+ Rails.cache.fetch("#{tenant_name}_JWKS", force: force) do
28
+ res = HTTParty.get(RubyJwk.jwk_url.gsub(':tenant_name', tenant_name))
29
+ res.code == 200 ? res.parsed_response : {}
30
+ end
31
+ end
32
+
33
+ def tenant_name
34
+ @payload.fetch('tenant')
35
+ end
36
+
37
+ def error_response_template(msg)
38
+ {
39
+ error: {
40
+ status: 401,
41
+ message: msg,
42
+ errors: nil,
43
+ code: nil
44
+ }
45
+ }
46
+ end
47
+
48
+ def auth_header
49
+ request.headers['Authorization'].to_s
50
+ end
51
+
52
+ def token
53
+ return unless auth_header.starts_with?('Bearer ')
54
+
55
+ auth_header.to_s.gsub('Bearer ', '')
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,7 @@
1
+ module RubyJwk
2
+ class ApplicationController < ::ApplicationController
3
+ def authenticate_tenant!
4
+ byebug
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ module RubyJwk
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module RubyJwk
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module RubyJwk
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: 'from@example.com'
4
+ layout 'mailer'
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module RubyJwk
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Ruby jwk</title>
5
+ <%= csrf_meta_tags %>
6
+ <%= csp_meta_tag %>
7
+
8
+ <%= stylesheet_link_tag "ruby_jwk/application", media: "all" %>
9
+ </head>
10
+ <body>
11
+
12
+ <%= yield %>
13
+
14
+ </body>
15
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ RubyJwk::Engine.routes.draw do
2
+ end
@@ -0,0 +1,5 @@
1
+ module RubyJwk
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace RubyJwk
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module RubyJwk
2
+ VERSION = '0.1.0'
3
+ end
data/lib/ruby_jwk.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "ruby_jwk/version"
2
+ require "ruby_jwk/engine"
3
+ require "jwt"
4
+
5
+ module RubyJwk
6
+ mattr_accessor :jwk_url
7
+ mattr_accessor :skip_issuers
8
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :ruby_jwk do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_jwk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - santhanakarthikeyan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-02-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 6.1.4
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 6.1.4.4
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 6.1.4
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 6.1.4.4
33
+ - !ruby/object:Gem::Dependency
34
+ name: jwt
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 2.3.0
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: 2.3.0
47
+ description: Authenticate JWKs via public JWT enspoint
48
+ email:
49
+ - santhanakarthikeyan@hotmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - MIT-LICENSE
55
+ - README.md
56
+ - Rakefile
57
+ - app/assets/config/ruby_jwk_manifest.js
58
+ - app/assets/stylesheets/ruby_jwk/application.css
59
+ - app/controllers/concerns/ruby_jwk/authenticate.rb
60
+ - app/controllers/ruby_jwk/application_controller.rb
61
+ - app/helpers/ruby_jwk/application_helper.rb
62
+ - app/jobs/ruby_jwk/application_job.rb
63
+ - app/mailers/ruby_jwk/application_mailer.rb
64
+ - app/models/ruby_jwk/application_record.rb
65
+ - app/views/layouts/ruby_jwk/application.html.erb
66
+ - config/routes.rb
67
+ - lib/ruby_jwk.rb
68
+ - lib/ruby_jwk/engine.rb
69
+ - lib/ruby_jwk/version.rb
70
+ - lib/tasks/ruby_jwk_tasks.rake
71
+ homepage: https://github.com/santhanakarthikeyan/ruby_jwk
72
+ licenses:
73
+ - MIT
74
+ metadata:
75
+ homepage_uri: https://github.com/santhanakarthikeyan/ruby_jwk
76
+ source_code_uri: https://github.com/santhanakarthikeyan/ruby_jwk
77
+ changelog_uri: https://github.com/santhanakarthikeyan/ruby_jwk
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubygems_version: 3.2.3
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Authenticate JWKs
97
+ test_files: []