fb-jwt-auth 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ff3e7406a923af4e7374f7855ddce3de5bbe9e46267b63a2d06c92a74f3ec13a
4
+ data.tar.gz: 1a0db0196134bed15fec85fb83006f0254f5dc0b2e96f02644b4d452499a121c
5
+ SHA512:
6
+ metadata.gz: 9ef0173a6261f0b8f8377f1987ab3d8c16b7364f25209ab10094240a096e2b9ef24f35944f12ad81a06a85357da1e48c76899285dcd40f8210254993da44a81b
7
+ data.tar.gz: aee6abbd74914c5ba92158c5e6eb3ba3b62bd55e36bd5fa57d2877fb76a5ebd806317072a5ee8d6fd91b7318af788ec89da4bf15ad421d66426a908636b8d1c0
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ Gemfile.lock
13
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1 @@
1
+ 2.7.2
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.6.3
6
+ before_install: gem install bundler -v 2.1.4
File without changes
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in fb-jwt-auth.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
8
+ gem 'byebug'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 matttei
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.
@@ -0,0 +1,32 @@
1
+ # Fb::Jwt::Auth
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'fb-jwt-auth'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle install
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install fb-jwt-auth
18
+
19
+ ## Usage
20
+
21
+ ```
22
+ Fb::Jwt::Auth.configure do |config|
23
+ config.service_token_cache_root_url = ENV['SERVICE_TOKEN_CACHE_ROOT_URL']
24
+ end
25
+
26
+ Fb::Jwt::Auth.new(
27
+ access_token: request.headers['x-access-token-v2'],
28
+ key: 'fb-editor', # service name
29
+ leeway: ENV['MAX_IAT_SKEW_SECONDS'],
30
+ logger: Rails.logger
31
+ ).verify!
32
+ ```
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fb/jwt/auth"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ require_relative 'lib/fb/jwt/auth/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "fb-jwt-auth"
5
+ spec.version = Fb::Jwt::Auth::VERSION
6
+ spec.authors = ['Form builder developers']
7
+ spec.email = ['form-builder-developers@digital.justice.gov.uk']
8
+
9
+ spec.summary = %q{JWT authentication done in form builder team}
10
+ spec.description = %q{JWT authentication done in all apps in form builder}
11
+ spec.homepage = 'https://github.com/ministryofjustice/fb-jwt-auth'
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/ministryofjustice/fb-jwt-auth"
17
+ spec.metadata["changelog_uri"] = "https://github.com/ministryofjustice/fb-jwt-auth/blob/main/Changelog.md"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_dependency 'jwt'
29
+ spec.add_dependency 'json'
30
+ spec.add_dependency 'activesupport'
31
+ end
@@ -0,0 +1,79 @@
1
+ require 'fb/jwt/auth/version'
2
+ require 'openssl'
3
+ require 'jwt'
4
+ require 'active_support/core_ext'
5
+
6
+ module Fb
7
+ module Jwt
8
+ class Auth
9
+ def self.service_token_cache_root_url=(value)
10
+ @@service_token_cache_root_url = value
11
+ end
12
+
13
+ def self.service_token_cache_root_url
14
+ @@service_token_cache_root_url
15
+ end
16
+
17
+ def self.configure(&block)
18
+ yield self
19
+ end
20
+
21
+ autoload :ServiceTokenClient, 'fb/jwt/auth/service_token_client'
22
+
23
+ class TokenNotPresentError < StandardError
24
+ end
25
+
26
+ class TokenNotValidError < StandardError
27
+ end
28
+
29
+ class TokenExpiredError < StandardError
30
+ end
31
+
32
+ attr_accessor :token, :key, :leeway, :logger
33
+
34
+ def initialize(token:, key:, leeway:, logger:)
35
+ @token = token
36
+ @key = key
37
+ @leeway = leeway
38
+ @logger = logger
39
+ end
40
+
41
+ def verify!
42
+ raise TokenNotPresentError if token.nil?
43
+
44
+ begin
45
+ hmac_secret = public_key(key)
46
+ payload, _header = JWT.decode(
47
+ token,
48
+ hmac_secret,
49
+ true,
50
+ exp_leeway: leeway,
51
+ algorithm: 'RS256'
52
+ )
53
+ rescue StandardError => e
54
+ error_message = "Couldn't parse that token - error #{e}"
55
+ logger.debug(error_message)
56
+ raise TokenNotValidError.new(error_message)
57
+ end
58
+
59
+ # NOTE: verify_iat used to be in the JWT gem, but was removed in v2.2
60
+ # so we have to do it manually
61
+ iat_skew = payload['iat'].to_i - Time.zone.now.to_i
62
+
63
+ if iat_skew.abs > leeway.to_i
64
+ error_message = "iat skew is #{iat_skew}, max is #{leeway} - INVALID"
65
+ logger.debug(error_message)
66
+
67
+ raise TokenExpiredError.new(error_message)
68
+ end
69
+
70
+ logger.debug 'token is valid'
71
+ payload
72
+ end
73
+
74
+ def public_key
75
+ OpenSSL::PKey::RSA.new(ServiceTokenClient.new(key).public_key)
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,37 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'base64'
4
+
5
+ class Fb::Jwt::Auth::ServiceTokenClient
6
+ class ServiceTokenCacheError < StandardError; end
7
+
8
+ attr_accessor :key, :root_url
9
+
10
+ def initialize(key)
11
+ @key = key
12
+ @root_url = Fb::Jwt::Auth.service_token_cache_root_url
13
+ end
14
+
15
+ def public_key
16
+ response = Net::HTTP.get_response(public_key_uri)
17
+
18
+ unless response.code.to_i == 200
19
+ raise ServiceTokenCacheError.new(
20
+ "Unexpected response code\n" \
21
+ "Response code: #{response.code} => Response body: #{response.body}"
22
+ )
23
+ end
24
+
25
+ Base64.strict_decode64(JSON.parse(response.body).fetch('token'))
26
+ rescue Errno::ECONNREFUSED => e
27
+ raise ServiceTokenCacheError.new(
28
+ "Unable to connect to the Service Token Cache\n#{e.message}"
29
+ )
30
+ end
31
+
32
+ private
33
+
34
+ def public_key_uri
35
+ URI.join(@root_url, '/service/v2/', key)
36
+ end
37
+ end
@@ -0,0 +1,7 @@
1
+ module Fb
2
+ module Jwt
3
+ class Auth
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fb-jwt-auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Form builder developers
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-10-21 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: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: JWT authentication done in all apps in form builder
56
+ email:
57
+ - form-builder-developers@digital.justice.gov.uk
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".ruby-version"
65
+ - ".travis.yml"
66
+ - Changelog.md
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - fb-jwt-auth.gemspec
74
+ - lib/fb/jwt/auth.rb
75
+ - lib/fb/jwt/auth/service_token_client.rb
76
+ - lib/fb/jwt/auth/version.rb
77
+ homepage: https://github.com/ministryofjustice/fb-jwt-auth
78
+ licenses:
79
+ - MIT
80
+ metadata:
81
+ homepage_uri: https://github.com/ministryofjustice/fb-jwt-auth
82
+ source_code_uri: https://github.com/ministryofjustice/fb-jwt-auth
83
+ changelog_uri: https://github.com/ministryofjustice/fb-jwt-auth/blob/main/Changelog.md
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 2.3.0
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubygems_version: 3.1.4
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: JWT authentication done in form builder team
103
+ test_files: []