auth_jwt 0.0.8

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 228962c8c3dd2f87adb2bb74fefa87b21f843bda
4
+ data.tar.gz: d052cadebba0f0dfc24cf3276389c5ef36b3970d
5
+ SHA512:
6
+ metadata.gz: 9353ec971ba24d9536b4230bae62e6fbd051e2495a179d39a4ed55511163550ed7a0d0c8d31b964fcd3859f4901a9e45265eea8cb2a34b8dd71a7162f0832d50
7
+ data.tar.gz: 80da236829814acd95d703d7e2c0b4be78ab569710a1a317ad8ffcfafe0a9a89f706395f444cd343e834baa23228938581afd89d4b43bdd854624151d10f47f9
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Aurelien GASTON
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.
@@ -0,0 +1,23 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'AuthJwt'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
@@ -0,0 +1,20 @@
1
+ module AuthJwt
2
+ require 'json/jwt'
3
+ require 'auth_jwt/exception/unauthorized'
4
+ require 'auth_jwt/controller_additions'
5
+ require 'auth_jwt/user_model_additions'
6
+ require 'auth_jwt/configuration'
7
+ require 'auth_jwt/engine'
8
+
9
+ class << self
10
+ attr_writer :configuration
11
+ end
12
+
13
+ def self.configuration
14
+ @configuration ||= Configuration.new
15
+ end
16
+
17
+ def self.configure
18
+ yield(configuration)
19
+ end
20
+ end
@@ -0,0 +1,24 @@
1
+ module AuthJwt
2
+ # Hold custom configurations
3
+ class Configuration
4
+ attr_accessor :user_class, :login_field, :password_field, :salt, :jwt_sign_key, :public_key, :private_key, :iss, :aud, :exp
5
+
6
+ # Set the default settings
7
+ def initialize
8
+ @user_class = 'User'
9
+ @login_field = 'login'
10
+ @password_field = 'password'
11
+ @salt = SecureRandom.hex(16)
12
+ end
13
+
14
+ # Load public_key file
15
+ def public_key=(value)
16
+ @public_key = OpenSSL::PKey::RSA.new(File.new(value))
17
+ end
18
+
19
+ # Load private_key file
20
+ def private_key=(value)
21
+ @private_key = OpenSSL::PKey::RSA.new(File.new(value))
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,69 @@
1
+ module AuthJwt
2
+ # Include #login_user and #require_auth and @current_user to all Controllers.
3
+ module ControllerAdditions
4
+ # Add InstanceMethods
5
+ def self.included(klass)
6
+ klass.class_eval do
7
+ include InstanceMethods
8
+ end
9
+ end
10
+
11
+ # InstanceMethods to be included
12
+ module InstanceMethods
13
+ # The current user or nil if not authenticated
14
+ attr_reader :current_user
15
+
16
+ # Sets up a method to check a user credentials
17
+ #
18
+ # return the authenticated user or raise AuthJwt::Unauthorized
19
+ #
20
+ #
21
+ # ```ruby
22
+ # class AnyController < ApplicationController
23
+ # def login
24
+ # login_user(credentials)
25
+ # end
26
+ # end
27
+ # ```
28
+ def login_user(credentials)
29
+ user_class = AuthJwt.configuration.user_class.constantize
30
+ login_field = AuthJwt.configuration.login_field.to_sym
31
+ password_field = AuthJwt.configuration.password_field.to_sym
32
+
33
+ user = user_class.find_by(login_field => credentials[login_field])
34
+ if user
35
+ fail AuthJwt::Unauthorized, 'Invalid credentials' unless user.authenticate(credentials[password_field])
36
+ @current_user = user
37
+ else
38
+ fail AuthJwt::Unauthorized, 'Invalid user'
39
+ end
40
+ end
41
+
42
+ # Sets up a method to check if the user is authenticated
43
+ #
44
+ # * if the user is authenticated setup current_user
45
+ # * if the user is not authenticated, raise AuthJwt::Unauthorized
46
+ #
47
+ # ```ruby
48
+ # class AnyController < ApplicationController
49
+ # before_filter :require_auth
50
+ # end
51
+ # ```
52
+ def require_auth
53
+ fail 'not in a controller scope' if request.nil?
54
+ fail AuthJwt::Unauthorized, 'No Auth' if request.authorization.nil?
55
+ user_class = AuthJwt.configuration.user_class.constantize
56
+ begin
57
+ user = user_class.from_jwe request.authorization
58
+ if user
59
+ @current_user = user
60
+ else
61
+ fail 'Not Found'
62
+ end
63
+ rescue
64
+ raise AuthJwt::Unauthorized, 'Invalid token'
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,9 @@
1
+ module AuthJwt
2
+ # Add the AuthJwt ControllerAdditions to the ActionController
3
+ class Engine < Rails::Engine
4
+ initializer 'extend Controllers and UserModel with AuthJwt' do
5
+ ActionController::Base.send(:include, AuthJwt::ControllerAdditions)
6
+ AuthJwt.configuration.user_class.constantize.send(:include, AuthJwt::UserModelAdditions)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ module AuthJwt
2
+ # Exception raised when the auth is invalid or missing
3
+ class Unauthorized < Exception
4
+ end
5
+ end
@@ -0,0 +1,49 @@
1
+ module AuthJwt
2
+ module UserModelAdditions
3
+ # Add InstanceMethods
4
+ def self.included(klass)
5
+ klass.class_eval do
6
+ extend ClassMethods
7
+ include InstanceMethods
8
+ end
9
+ end
10
+
11
+ module InstanceMethods
12
+ def jwe(expire_time=AuthJwt.configuration.exp)
13
+ claim = {
14
+ iss: AuthJwt.configuration.iss,
15
+ aud: AuthJwt.configuration.aud,
16
+ iat: Time.now,
17
+ nbf: Time.now,
18
+ exp: expire_time,
19
+ payload: { user_id: id }
20
+ }
21
+ jwt = JSON::JWT.new(claim)
22
+ jws = jwt.sign(AuthJwt.configuration.jwt_sign_key)
23
+ jwe = jws.encrypt(AuthJwt.configuration.public_key)
24
+ jwe.to_s
25
+ end
26
+ end
27
+
28
+ module ClassMethods
29
+ def from_jwe(jwe_string)
30
+ jwe = JSON::JWE.new jwe_string
31
+ jwe.alg, jwe.enc = :RSA1_5, :'A128CBC-HS256'
32
+ jws = jwe.decrypt!(AuthJwt.configuration.private_key).to_s
33
+ jwt = JSON::JWT.decode(jws, AuthJwt.configuration.jwt_sign_key)
34
+ verify_jwt_integrity! jwt
35
+ AuthJwt.configuration.user_class.constantize.find jwt['payload']['user_id']
36
+ end
37
+
38
+ private
39
+
40
+ def verify_jwt_integrity!(jwt)
41
+ fail AuthJwt::Unauthorized, 'Unknown Issuer' if jwt['iss'].nil? || jwt['iss'] != AuthJwt.configuration.iss
42
+ fail AuthJwt::Unauthorized, 'Unknown Audience' if jwt['aud'].nil? || jwt['aud'] != AuthJwt.configuration.aud
43
+ fail AuthJwt::Unauthorized, 'Not Yet Valid' if jwt['nbf'].nil? || Time.new(jwt['nbf']) < Time.now
44
+ fail AuthJwt::Unauthorized, 'Expired' if jwt['exp'].nil? || Time.new(jwt['exp']) < Time.now
45
+ fail AuthJwt::Unauthorized, 'Missing Payload' if jwt['payload'].nil? || jwt['payload']['user_id'].nil?
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module AuthJwt
2
+ VERSION = '0.0.8'
3
+ end
@@ -0,0 +1,23 @@
1
+ module AuthJwt
2
+ # Generate the initializer
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ # generate a random number
7
+ def gen_secret
8
+ SecureRandom.hex(16)
9
+ end
10
+
11
+ desc 'This generator creates an initializer file for AuthJwt at config/initializers/auth_jwt.rb'
12
+ def create_initializer_file
13
+ template 'initializer.rb', 'config/initializers/auth_jwt.rb'
14
+ end
15
+
16
+ desc 'This generator creates public/private keys at config/keys/jwt_public_key.pem and config/keys/jwt_private_key.pem'
17
+ def create_keys_files
18
+ rsa_private = OpenSSL::PKey::RSA.generate(2048)
19
+ create_file 'config/keys/jwt_private_key.pem', rsa_private.to_pem
20
+ create_file 'config/keys/jwt_public_key.pem', rsa_private.public_key.to_pem
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,30 @@
1
+ # Here you can configure AuthJwt features.
2
+ AuthJwt.configure do |config|
3
+ # Set the user class model
4
+ # config.user_class = 'User'
5
+
6
+ # Set the login field
7
+ # config.login_field = 'login'
8
+
9
+ # Set the password field
10
+ # config.password_field = 'password'
11
+
12
+ # Salt used to store password with
13
+ config.salt = '<%= gen_secret %>'
14
+
15
+ # Secret key to sign jwt with
16
+ config.jwt_sign_key = '<%= gen_secret %>'
17
+
18
+ # Public/Private keys path
19
+ config.public_key = Rails.root.join('config', 'keys', 'jwt_public_key.pem')
20
+ config.private_key = Rails.root.join('config', 'keys', 'jwt_private_key.pem')
21
+
22
+ # issuer of the jwt (random string or URI)
23
+ config.iss = '<%= gen_secret %>'
24
+
25
+ # audience of the jwt (random string or URI)
26
+ config.aud = '<%= gen_secret %>'
27
+
28
+ # lifetime of the token
29
+ config.exp = 3.hours.from_now
30
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :auth_jwt do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,196 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: auth_jwt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.8
5
+ platform: ruby
6
+ authors:
7
+ - Aurelien GASTON
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-13 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: 4.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: json-jwt
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.3
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: bcrypt
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
+ - !ruby/object:Gem::Dependency
56
+ name: pg
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sdoc
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec-rails
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: factory_girl_rails
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: faker
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: database_cleaner
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: Authentication via JWT (with encryption)
154
+ email:
155
+ - aurelien.gaston@krondor.fr
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - MIT-LICENSE
161
+ - Rakefile
162
+ - lib/auth_jwt.rb
163
+ - lib/auth_jwt/configuration.rb
164
+ - lib/auth_jwt/controller_additions.rb
165
+ - lib/auth_jwt/engine.rb
166
+ - lib/auth_jwt/exception/unauthorized.rb
167
+ - lib/auth_jwt/user_model_additions.rb
168
+ - lib/auth_jwt/version.rb
169
+ - lib/generators/auth_jwt/install_generator.rb
170
+ - lib/generators/auth_jwt/templates/initializer.rb
171
+ - lib/tasks/auth_jwt_tasks.rake
172
+ homepage: https://amos.krondor.fr/rails/auth_jwt
173
+ licenses:
174
+ - MIT
175
+ metadata: {}
176
+ post_install_message:
177
+ rdoc_options: []
178
+ require_paths:
179
+ - lib
180
+ required_ruby_version: !ruby/object:Gem::Requirement
181
+ requirements:
182
+ - - ">="
183
+ - !ruby/object:Gem::Version
184
+ version: '0'
185
+ required_rubygems_version: !ruby/object:Gem::Requirement
186
+ requirements:
187
+ - - ">="
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ requirements: []
191
+ rubyforge_project:
192
+ rubygems_version: 2.4.8
193
+ signing_key:
194
+ specification_version: 4
195
+ summary: Authentication via JWT (with encryption)
196
+ test_files: []