auth0_current_user 0.1.0.5 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f3976912c1050c725f7a44db0f79a09f12d6f901ac54f36750ba4365c36e6a32
4
- data.tar.gz: b75106e90c7821d950eb8377397ba435a6476f29718bfff16ed154e0f5a896f0
3
+ metadata.gz: b17d5fc0d9110bac480f41841b521209c9050301345637a5bd094be02be98515
4
+ data.tar.gz: 585b48bd66b46d4e5fbd6a12cd8fd634800131acead3c235692dc081733a18f1
5
5
  SHA512:
6
- metadata.gz: 03fd71995e1171097cfbd31b6c28d55dcaebeff04acbeafc0897ebcfc76192fd88431c8f929096f1a8083f4197c9c122a0f192ee6fde971c0e048c5421989035
7
- data.tar.gz: 255a900f591f6ae70d41de1bd3640d69ab23e9abe6d16cb416995133d7db8372a59ac3b4fe81e9886ba03d1af643716d9df15bff983ba7ee982e78b724e3a585
6
+ metadata.gz: 8c68d89ce3c6866e8dc10ac6043d2ad34895ea551080700089012e7ec43bb084108efc8a945c7a279a8b89ab345384b2a362677e9d3e7e326b2be59bdb64d297
7
+ data.tar.gz: 0cc0ddb2ef2b808b3eecc282577dfb81c26622e2b72928fa68947b02b29c42ba32e8a656e4596e21ff58602965989ed3acf92a68c207da7b1be8c3f47b9f5808
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'jwt'
4
+ require 'request_store'
5
+ require 'auth0_current_user/json_web_token'
6
+ require 'auth0_current_user/configuration'
7
+
8
+ module ApiSecured
9
+ extend ActiveSupport::Concern
10
+
11
+ included do
12
+ before_action :authenticate_request!
13
+ end
14
+
15
+ private
16
+
17
+ def authenticate_request!
18
+ token = auth_token
19
+ set_current_user(token)
20
+
21
+ token
22
+ rescue JWT::VerificationError, JWT::DecodeError
23
+ render json: { errors: ['Not Authenticated'] }, status: :unauthorized
24
+ end
25
+
26
+ def http_token
27
+ if request.headers['Authorization'].present?
28
+ request.headers['Authorization'].split(' ').last
29
+ end
30
+ end
31
+
32
+ def auth_token
33
+ ::JsonWebToken.verify(http_token)
34
+ end
35
+
36
+ def get_email(token)
37
+ ::JsonWebToken.get_claim(token, 'email')
38
+ end
39
+
40
+ def set_current_user(token)
41
+ email = get_email(token)
42
+ RequestStore.store[:current_user] ||= Kernel.const_get(authenticated_klass).find_by(email: email)
43
+ end
44
+
45
+ def current_user
46
+ @current_user ||= RequestStore.store[:current_user]
47
+ end
48
+
49
+ def authenticated_klass
50
+ unless configuration.authenticated_klass
51
+ raise NotImplementedError, 'You must define the #authenitcated_klass in config/initializers/auth0_current_user'
52
+ return
53
+ end
54
+
55
+ @authenticated_klass ||= configuration.authenticated_klass.to_s.classify
56
+ rescue StandardError => e
57
+ Rails.logger.error(e.message)
58
+ end
59
+
60
+ def configuration
61
+ @configuration ||= Configuration.new
62
+ end
63
+
64
+ end
65
+
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'net/http'
3
4
  require 'uri'
4
5
  require 'jwt'
@@ -13,8 +14,8 @@ class JsonWebToken
13
14
  verify_iss: true,
14
15
  aud: configuration.auth0_audience,
15
16
  verify_aud: true) do |header|
16
- jwks_hash[header['kid']]
17
- end
17
+ jwks_hash[header['kid']]
18
+ end
18
19
  end
19
20
 
20
21
  def self.jwks_hash
@@ -22,7 +23,7 @@ class JsonWebToken
22
23
  jwks_keys = Array(JSON.parse(jwks_raw)['keys'])
23
24
  Hash[
24
25
  jwks_keys
25
- .map do |k|
26
+ .map do |k|
26
27
  [
27
28
  k['kid'],
28
29
  OpenSSL::X509::Certificate.new(
@@ -41,3 +42,4 @@ class JsonWebToken
41
42
  @configuration ||= Auth0CurrentUser::Configuration.new
42
43
  end
43
44
  end
45
+
@@ -1,3 +1,3 @@
1
1
  module Auth0CurrentUser
2
- VERSION = "0.1.0.5"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -0,0 +1,43 @@
1
+ module Auth0CurrentUser
2
+ module WebSecured
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ helper_method :current_user
7
+ before_action :logged_in_using_omniauth?
8
+ end
9
+
10
+ def current_user
11
+ @_current_user ||= RequestStore.store[:current_user] ||= Kernel.const_get(authenticated_klass).find_by(email: email)
12
+ end
13
+
14
+ private
15
+
16
+ def authenticated_klass
17
+ unless configuration.authenticated_klass
18
+ raise NotImplementedError, 'You must define the #authenitcated_klass in config/initializers/auth0_current_user'
19
+ return
20
+ end
21
+
22
+ @authenticated_klass ||= configuration.authenticated_klass.to_s.classify
23
+ rescue NameError => e
24
+ Rails.logger.error("You must create a #{authenticated_klass} model/migration")
25
+ rescue StandardError => e
26
+ Rails.logger.error(e.message)
27
+ end
28
+
29
+ def configuration
30
+ @configuration ||= Configuration.new
31
+ end
32
+
33
+ def email
34
+ @_email ||= session.dig(:userinfo, :email)
35
+ end
36
+
37
+ def logged_in_using_omniauth?
38
+ redirect_to '/' unless current_user || session[:userinfo].present?
39
+ end
40
+
41
+ end
42
+ end
43
+
@@ -1,6 +1,7 @@
1
1
  require 'auth0_current_user/version'
2
2
  require 'auth0_current_user/configuration'
3
- require 'auth0_current_user/secured'
3
+ require 'auth0_current_user/api_secured'
4
+ require 'auth0_current_user/web_secured'
4
5
 
5
6
  module Auth0CurrentUser
6
7
  class Error < StandardError; end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: auth0_current_user
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.5
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Heft
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-08 00:00:00.000000000 Z
11
+ date: 2021-12-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -72,10 +72,11 @@ files:
72
72
  - bin/console
73
73
  - bin/setup
74
74
  - lib/auth0_current_user.rb
75
+ - lib/auth0_current_user/api_secured.rb
75
76
  - lib/auth0_current_user/configuration.rb
76
77
  - lib/auth0_current_user/json_web_token.rb
77
- - lib/auth0_current_user/secured.rb
78
78
  - lib/auth0_current_user/version.rb
79
+ - lib/auth0_current_user/web_secured.rb
79
80
  - lib/generators/auth0_current_user/install_generator.rb
80
81
  - lib/generators/templates/auth0_current_user.rb
81
82
  homepage: https://github.com/mikeyduece/auth0_current_user
@@ -85,7 +86,7 @@ metadata:
85
86
  homepage_uri: https://github.com/mikeyduece/auth0_current_user
86
87
  source_code_uri: https://github.com/mikeyduece/auth0_current_user
87
88
  changelog_uri: https://github.com/mikeyduece/auth0_current_user
88
- post_install_message:
89
+ post_install_message:
89
90
  rdoc_options: []
90
91
  require_paths:
91
92
  - lib
@@ -101,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
101
102
  version: '0'
102
103
  requirements: []
103
104
  rubygems_version: 3.1.2
104
- signing_key:
105
+ signing_key:
105
106
  specification_version: 4
106
107
  summary: Implements Auth0's setup for authentication/authorization along with setting
107
108
  a current_user method.
@@ -1,66 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'jwt'
4
- require 'request_store'
5
- require 'auth0_current_user/json_web_token'
6
- require 'auth0_current_user/configuration'
7
-
8
- module Auth0CurrentUser
9
- module Secured
10
- extend ActiveSupport::Concern
11
-
12
- included do
13
- before_action :authenticate_request!
14
- end
15
-
16
- private
17
-
18
- def authenticate_request!
19
- token = auth_token
20
- set_current_user(token)
21
-
22
- token
23
- rescue JWT::VerificationError, JWT::DecodeError
24
- render json: { errors: ['Not Authenticated'] }, status: :unauthorized
25
- end
26
-
27
- def http_token
28
- if request.headers['Authorization'].present?
29
- request.headers['Authorization'].split(' ').last
30
- end
31
- end
32
-
33
- def auth_token
34
- JsonWebToken.verify(http_token)
35
- end
36
-
37
- def get_email(token)
38
- JsonWebToken.get_claim(token, 'email')
39
- end
40
-
41
- def set_current_user(token)
42
- email = get_email(token)
43
- RequestStore.store[:current_user] ||= Kernel.const_get(authenticated_klass).find_by(email: email)
44
- end
45
-
46
- def current_user
47
- @current_user ||= RequestStore.store[:current_user]
48
- end
49
-
50
- def authenticated_klass
51
- unless configuration.authenticated_klass
52
- raise NotImplementedError, 'You must define the #authenitcated_klass in config/initializers/auth0_current_user'
53
- return
54
- end
55
-
56
- @authenticated_klass ||= configuration.authenticated_klass.to_s.classify
57
- rescue StandardError => e
58
- Rails.logger.error(e.message)
59
- end
60
-
61
- def configuration
62
- @configuration ||= Configuration.new
63
- end
64
-
65
- end
66
- end