mumsnet_jwt 2.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/mumsnet_jwt.rb +80 -0
  3. metadata +57 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7bed91ac812b9e5990c5043d5bf29e54a4a0e027
4
+ data.tar.gz: f8e683320979e9b972ff40167de215d5e18c58fd
5
+ SHA512:
6
+ metadata.gz: 777525ba93a335af4a13c29bc07975c31b4c68ad2843d5a0eb25e6becbd75eb1bc8767918750b3da5ccd940f8e857b666d733d13038a05978fdb0cfb97a863bb
7
+ data.tar.gz: cb4a175115256e9c8c23c80b9cbc405f00788064e1b769650bea79601e1f91099f5517df1306ee0ac12066123239f6257393e2c9d96706df3f41823c136286c4
@@ -0,0 +1,80 @@
1
+ module MumsnetJWT
2
+ class << self
3
+ require 'jwt'
4
+ require 'json'
5
+ require 'base64'
6
+ require 'logger'
7
+ @logger = Logger.new(STDOUT)
8
+ DEFAULT_EXP = Time.now.utc.to_i + 60 * 60 * 24
9
+ def tokenify(extra_payload: {})
10
+ return false if env_defined?
11
+ payload = { client_id: ENV['JWT_CLIENT_ID'], iss: ENV['JWT_ISSUER'], exp: DEFAULT_EXP }.merge(extra_payload)
12
+ JWT.encode payload, jwt_secret, 'HS256' unless jwt_secret.nil?
13
+ end
14
+
15
+ def check_authorization_header(header)
16
+ return false if env_defined?
17
+ return false unless header.split(' ').first == 'Bearer'
18
+ token = header.split(' ').last
19
+ check_token(token)
20
+ rescue StandardError => e
21
+ puts 'JWT ERROR: check_authorization_header'
22
+ puts "Header: #{header}"
23
+ puts e
24
+ puts 'END JWT ERROR'
25
+ false
26
+ end
27
+
28
+ def check_token(token)
29
+ return false if env_defined?
30
+ client_id = client_id_from_token(token)
31
+ if !client_id.nil?
32
+ decode_token(token: token, key: 'client_id') == client_id
33
+ else
34
+ false
35
+ end
36
+ rescue StandardError => e
37
+ puts 'JWT ERROR: check_token'
38
+ puts "Token: #{token}"
39
+ puts e
40
+ puts 'END JWT ERROR'
41
+ false
42
+ end
43
+
44
+ def decode_token(token:, key: nil)
45
+ return nil if env_defined?
46
+ client_id = client_id_from_token(token)
47
+ decoded_token = JWT.decode(token, jwt_secret(client_id), true, algorithm: 'HS256') unless jwt_secret(client_id).nil?
48
+ if key
49
+ decoded_token[0][key]
50
+ else
51
+ decoded_token
52
+ end
53
+ rescue StandardError· => e
54
+ puts 'JWT ERROR: decode_token'
55
+ puts "token: #{token}"
56
+ puts "key: #{key}"
57
+ puts e
58
+ puts 'END JWT ERROR'
59
+ nil
60
+ end
61
+
62
+ private
63
+
64
+ def env_defined?
65
+ ENV['JWT_CLIENT_ID'].nil? || ENV['JWT_SECRETS'].nil? || ENV['JWT_ISSUER'].nil?
66
+ end
67
+
68
+ def client_id_from_token(token)
69
+ JSON.parse(Base64.decode64(token.split('.')[1]))['client_id']
70
+ end
71
+
72
+ def jwt_secret(client_id = ENV['JWT_CLIENT_ID'])
73
+ jwt_secret = nil
74
+ JSON.parse(ENV['JWT_SECRETS']).each do |item|
75
+ jwt_secret = item['secret'] if item['client_id'] == client_id
76
+ end
77
+ jwt_secret
78
+ end
79
+ end
80
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mumsnet_jwt
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Murray Catto
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-07-11 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
+ description:
28
+ email:
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/mumsnet_jwt.rb
34
+ homepage:
35
+ licenses: []
36
+ metadata: {}
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubyforge_project:
53
+ rubygems_version: 2.5.2.3
54
+ signing_key:
55
+ specification_version: 4
56
+ summary: JSON web token lib
57
+ test_files: []