fb-jwt-auth 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 +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.ruby-version +1 -0
- data/.travis.yml +6 -0
- data/Changelog.md +0 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +32 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/fb-jwt-auth.gemspec +31 -0
- data/lib/fb/jwt/auth.rb +79 -0
- data/lib/fb/jwt/auth/service_token_client.rb +37 -0
- data/lib/fb/jwt/auth/version.rb +7 -0
- metadata +103 -0
checksums.yaml
ADDED
@@ -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
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.7.2
|
data/.travis.yml
ADDED
data/Changelog.md
ADDED
File without changes
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -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.
|
data/README.md
ADDED
@@ -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
|
+
```
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -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__)
|
data/bin/setup
ADDED
data/fb-jwt-auth.gemspec
ADDED
@@ -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
|
data/lib/fb/jwt/auth.rb
ADDED
@@ -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
|
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: []
|