DMAO-WardenJWT 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +10 -0
- data/WardenJWT.gemspec +41 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/DMAO/WardenJWT.rb +7 -0
- data/lib/DMAO/WardenJWT/strategy.rb +105 -0
- data/lib/DMAO/WardenJWT/user.rb +41 -0
- data/lib/DMAO/WardenJWT/version.rb +5 -0
- metadata +157 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0968f9f144a37bb4db7bda7ff8ddce80a9fcee3b
|
4
|
+
data.tar.gz: ea6a42261e86932bd62c3a8439bebff478796cde
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 51182564008f72aa5b7569b0f4f3f8d7723cc8db5b1383cfe677b3afc11057b52fc77ec7f572ae7c0c71254428a4ed4a9705d96b47bb73068d4ce87f2ea50796
|
7
|
+
data.tar.gz: 8602006bf45eabb12fc725a5728fd3bcbdac991f297bcc6f926d7c4de50a1eef359e4348bd1c8198cf6ea818b58aae402c02c91d222766346438c391dbebc7be
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Stephen Robinson
|
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,41 @@
|
|
1
|
+
# WardenJWT
|
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/WardenJWT`. 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
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'WardenJWT'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install WardenJWT
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/WardenJWT.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
41
|
+
|
data/Rakefile
ADDED
data/WardenJWT.gemspec
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'DMAO/WardenJWT/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "DMAO-WardenJWT"
|
8
|
+
spec.version = DMAO::WardenJWT::VERSION
|
9
|
+
spec.authors = ["Stephen Robinson", "LULibrary", "Digitial Innovation, Lancaster University Library"]
|
10
|
+
spec.email = ["stephen@stephen-robinson.co.uk"]
|
11
|
+
|
12
|
+
spec.summary = %q{Warden Strategy for DMA Online JWT Auth}
|
13
|
+
spec.description = %q{}
|
14
|
+
spec.homepage = "https://github.com/lulibrary/DMAO-WardenJWT"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
"public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_dependency "warden", "~> 1.2"
|
34
|
+
spec.add_dependency "jwt", "~> 1.5"
|
35
|
+
|
36
|
+
spec.add_development_dependency "bundler", ">= 1.12"
|
37
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
38
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
39
|
+
spec.add_development_dependency "mocha", "~> 1.2"
|
40
|
+
spec.add_development_dependency "codeclimate-test-reporter", "~> 1.0"
|
41
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "WardenJWT"
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'DMAO/WardenJWT/user'
|
2
|
+
require 'warden'
|
3
|
+
require 'jwt'
|
4
|
+
require 'logger'
|
5
|
+
|
6
|
+
module DMAO
|
7
|
+
module WardenJWT
|
8
|
+
class Strategy < ::Warden::Strategies::Base
|
9
|
+
|
10
|
+
def valid?
|
11
|
+
!jwt.nil? && !jwt.empty?
|
12
|
+
end
|
13
|
+
|
14
|
+
def authenticate!
|
15
|
+
|
16
|
+
valid_jwt? ? success!(User.from_jwt_claims(jwt_claims)) : fail!(:invalid_jwt)
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
def store?
|
21
|
+
false
|
22
|
+
end
|
23
|
+
|
24
|
+
def bearer_token
|
25
|
+
pattern = /^Bearer /
|
26
|
+
header = request.get_header('HTTP_AUTHORIZATION') # <= env
|
27
|
+
header.gsub(pattern, '') if header && header.match(pattern)
|
28
|
+
end
|
29
|
+
|
30
|
+
def jwt
|
31
|
+
|
32
|
+
if params[:jwt]
|
33
|
+
return params[:jwt]
|
34
|
+
end
|
35
|
+
|
36
|
+
if !bearer_token.nil? && !bearer_token.empty?
|
37
|
+
return bearer_token
|
38
|
+
end
|
39
|
+
|
40
|
+
nil
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
def jwt_claims
|
45
|
+
|
46
|
+
jwt = decode_jwt
|
47
|
+
|
48
|
+
if jwt.nil?
|
49
|
+
nil
|
50
|
+
else
|
51
|
+
jwt[0]
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
def decode_jwt
|
57
|
+
|
58
|
+
logger = ::Logger.new(STDOUT)
|
59
|
+
|
60
|
+
verify_iat = ENV['JWT_VERIFY_IAT']
|
61
|
+
verify_aud = ENV['JWT_VERIFY_AUD']
|
62
|
+
verify_iss = ENV['JWT_VERIFY_ISS']
|
63
|
+
jwt_secret = ENV['JWT_SECRET']
|
64
|
+
jwt_issuer = ENV['JWT_ISSUER']
|
65
|
+
jwt_audience = ENV['JWT_AUDIENCE']
|
66
|
+
|
67
|
+
begin
|
68
|
+
decoded_token = JWT.decode jwt, jwt_secret, true, { :verify_iat => verify_iat, :iss => jwt_issuer, :verify_iss => verify_iss, :aud => jwt_audience, :verify_aud => verify_aud, :algorithm => 'HS256'}
|
69
|
+
rescue JWT::ExpiredSignature
|
70
|
+
logger.info('JWT - Expired Signature')
|
71
|
+
return nil
|
72
|
+
rescue JWT::InvalidIssuerError
|
73
|
+
logger.info('JWT - Invalid Issuer')
|
74
|
+
return nil
|
75
|
+
rescue JWT::InvalidAudError
|
76
|
+
logger.info('JWT - Invalid Audience')
|
77
|
+
return nil
|
78
|
+
rescue JWT::InvalidIatError
|
79
|
+
logger.info('JWT - Invalid Issued At Timestamp')
|
80
|
+
return nil
|
81
|
+
rescue JWT::VerificationError
|
82
|
+
logger.info('JWT - Signature Verification Failed')
|
83
|
+
return nil
|
84
|
+
rescue JWT::DecodeError
|
85
|
+
logger.info('JWT - Error decoding JWT')
|
86
|
+
return nil
|
87
|
+
end
|
88
|
+
|
89
|
+
decoded_token
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
def valid_jwt?
|
94
|
+
|
95
|
+
if decode_jwt.nil?
|
96
|
+
return nil
|
97
|
+
end
|
98
|
+
|
99
|
+
true
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module DMAO
|
2
|
+
module WardenJWT
|
3
|
+
class User
|
4
|
+
|
5
|
+
attr_reader(:id, :subject_id, :institution_id, :roles)
|
6
|
+
|
7
|
+
def initialize(attributes)
|
8
|
+
|
9
|
+
@id = attributes[:uid] || nil
|
10
|
+
@subject_id = attributes[:subject_id] || nil
|
11
|
+
@institution_id = attributes[:institution_id] || nil
|
12
|
+
@roles = attributes[:roles] || []
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.from_jwt_claims jwt_claims
|
17
|
+
|
18
|
+
custom_claims = ENV['JWT_CUSTOM_CLAIMS_ATTRIBUTE']
|
19
|
+
|
20
|
+
uid = jwt_claims[custom_claims]["uid"].nil? ? jwt_claims["sub"] : jwt_claims[custom_claims]["uid"]
|
21
|
+
|
22
|
+
attributes = {
|
23
|
+
uid: uid,
|
24
|
+
subject_id: jwt_claims["sub"],
|
25
|
+
institution_id: jwt_claims[custom_claims]["institution_id"],
|
26
|
+
roles: jwt_claims[custom_claims]["roles"]
|
27
|
+
}
|
28
|
+
|
29
|
+
new(attributes)
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
def has_role? role
|
34
|
+
|
35
|
+
@roles.include? role.to_s
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: DMAO-WardenJWT
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Stephen Robinson
|
8
|
+
- LULibrary
|
9
|
+
- Digitial Innovation, Lancaster University Library
|
10
|
+
autorequire:
|
11
|
+
bindir: exe
|
12
|
+
cert_chain: []
|
13
|
+
date: 2017-01-24 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: warden
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - "~>"
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.2'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - "~>"
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: '1.2'
|
29
|
+
- !ruby/object:Gem::Dependency
|
30
|
+
name: jwt
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - "~>"
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '1.5'
|
36
|
+
type: :runtime
|
37
|
+
prerelease: false
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - "~>"
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '1.5'
|
43
|
+
- !ruby/object:Gem::Dependency
|
44
|
+
name: bundler
|
45
|
+
requirement: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '1.12'
|
50
|
+
type: :development
|
51
|
+
prerelease: false
|
52
|
+
version_requirements: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '1.12'
|
57
|
+
- !ruby/object:Gem::Dependency
|
58
|
+
name: rake
|
59
|
+
requirement: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - "~>"
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '10.0'
|
64
|
+
type: :development
|
65
|
+
prerelease: false
|
66
|
+
version_requirements: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - "~>"
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '10.0'
|
71
|
+
- !ruby/object:Gem::Dependency
|
72
|
+
name: minitest
|
73
|
+
requirement: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - "~>"
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '5.0'
|
78
|
+
type: :development
|
79
|
+
prerelease: false
|
80
|
+
version_requirements: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - "~>"
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '5.0'
|
85
|
+
- !ruby/object:Gem::Dependency
|
86
|
+
name: mocha
|
87
|
+
requirement: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - "~>"
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '1.2'
|
92
|
+
type: :development
|
93
|
+
prerelease: false
|
94
|
+
version_requirements: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - "~>"
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '1.2'
|
99
|
+
- !ruby/object:Gem::Dependency
|
100
|
+
name: codeclimate-test-reporter
|
101
|
+
requirement: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - "~>"
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '1.0'
|
106
|
+
type: :development
|
107
|
+
prerelease: false
|
108
|
+
version_requirements: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - "~>"
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '1.0'
|
113
|
+
description: ''
|
114
|
+
email:
|
115
|
+
- stephen@stephen-robinson.co.uk
|
116
|
+
executables: []
|
117
|
+
extensions: []
|
118
|
+
extra_rdoc_files: []
|
119
|
+
files:
|
120
|
+
- ".gitignore"
|
121
|
+
- Gemfile
|
122
|
+
- LICENSE.txt
|
123
|
+
- README.md
|
124
|
+
- Rakefile
|
125
|
+
- WardenJWT.gemspec
|
126
|
+
- bin/console
|
127
|
+
- bin/setup
|
128
|
+
- lib/DMAO/WardenJWT.rb
|
129
|
+
- lib/DMAO/WardenJWT/strategy.rb
|
130
|
+
- lib/DMAO/WardenJWT/user.rb
|
131
|
+
- lib/DMAO/WardenJWT/version.rb
|
132
|
+
homepage: https://github.com/lulibrary/DMAO-WardenJWT
|
133
|
+
licenses:
|
134
|
+
- MIT
|
135
|
+
metadata:
|
136
|
+
allowed_push_host: https://rubygems.org
|
137
|
+
post_install_message:
|
138
|
+
rdoc_options: []
|
139
|
+
require_paths:
|
140
|
+
- lib
|
141
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
147
|
+
requirements:
|
148
|
+
- - ">="
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: '0'
|
151
|
+
requirements: []
|
152
|
+
rubyforge_project:
|
153
|
+
rubygems_version: 2.5.1
|
154
|
+
signing_key:
|
155
|
+
specification_version: 4
|
156
|
+
summary: Warden Strategy for DMA Online JWT Auth
|
157
|
+
test_files: []
|