solidus_jwt 1.1.0 → 1.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.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.gem_release.yml +2 -2
  3. data/.rubocop.yml +0 -17
  4. data/Gemfile +2 -0
  5. data/README.md +13 -0
  6. data/app/decorators/{solidus_jwt → controllers/solidus_jwt}/spree/api/base_controller_decorator.rb +3 -1
  7. data/app/decorators/{solidus_jwt → models/solidus_jwt}/spree/user_decorator.rb +22 -0
  8. data/app/models/solidus_jwt/base_record.rb +2 -0
  9. data/app/models/solidus_jwt/token.rb +2 -0
  10. data/bin/r +9 -0
  11. data/bin/rails +4 -14
  12. data/bin/rails-engine +15 -0
  13. data/bin/rails-sandbox +17 -0
  14. data/bin/rake +7 -0
  15. data/bin/sandbox +4 -1
  16. data/bin/sandbox_rails +9 -0
  17. data/config/routes.rb +2 -0
  18. data/db/migrate/20190222220038_create_solidus_jwt_tokens.rb +2 -0
  19. data/db/migrate/20191212083655_add_foreign_key_to_users_table.rb +2 -0
  20. data/{app/controllers → lib/controllers/api}/spree/api/oauths_controller.rb +2 -0
  21. data/lib/generators/solidus_jwt/install/install_generator.rb +2 -0
  22. data/lib/solidus_jwt.rb +2 -0
  23. data/lib/solidus_jwt/concerns/decodeable.rb +2 -0
  24. data/lib/solidus_jwt/concerns/encodeable.rb +2 -0
  25. data/lib/solidus_jwt/config.rb +2 -0
  26. data/lib/solidus_jwt/devise_strategies/base.rb +2 -0
  27. data/lib/solidus_jwt/devise_strategies/password.rb +2 -0
  28. data/lib/solidus_jwt/devise_strategies/refresh_token.rb +2 -0
  29. data/lib/solidus_jwt/distributor/devise.rb +2 -0
  30. data/lib/solidus_jwt/engine.rb +3 -1
  31. data/lib/solidus_jwt/factories.rb +2 -0
  32. data/lib/solidus_jwt/preferences.rb +2 -0
  33. data/lib/solidus_jwt/version.rb +3 -1
  34. data/solidus_jwt.gemspec +3 -1
  35. data/spec/lib/solidus_jwt/config_spec.rb +2 -0
  36. data/spec/lib/solidus_jwt/devise_strategies/password_spec.rb +2 -0
  37. data/spec/lib/solidus_jwt/devise_strategies/refresh_token_spec.rb +2 -0
  38. data/spec/lib/solidus_jwt/preferences_spec.rb +2 -0
  39. data/spec/lib/solidus_jwt_spec.rb +2 -0
  40. data/spec/models/solidus_jwt/token_spec.rb +2 -0
  41. data/spec/requests/spree/api/json_web_tokens_spec.rb +4 -2
  42. data/spec/requests/spree/api/oauths_spec.rb +2 -0
  43. data/spec/spec_helper.rb +2 -0
  44. data/spec/support/shared_examples/decodeable_examples.rb +2 -0
  45. data/spec/support/shared_examples/encodeable_examples.rb +2 -0
  46. metadata +12 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0f3f9ec2c12488185b2ee9bff66409693f0d92ab52bde2adb67cc47df182a82a
4
- data.tar.gz: a77951e23f7d2352c5a2dde5a2742997f38bfb8e07ac31f1ae4f8be7b6bfc12c
3
+ metadata.gz: 5069a0d35cb2f777cae543265907fdd284e8e42ccb0e86c8bc4775e17beb6a88
4
+ data.tar.gz: 1019df1fc49a245eb54d5b64e1f52956a405c4f4f1003a043d65d81b871d672a
5
5
  SHA512:
6
- metadata.gz: '058c6e5d8bcc54fa1b623b68ab46848f9e9ffb6feee2e97f048c1047ed6a5c11ce78990aef67b6432d051471e718c09ef00b30717b0419edd88d95cbf0ca8412'
7
- data.tar.gz: 95993510aecd67280bc85dc283798edae88d9c8388c1997190f8420d2ea4351d6e88273d7f5236449df52ff035e276725c924a5a835144c10dc458146bbe6bb3
6
+ metadata.gz: b042053167cadec496c99b501b12ada4a3090ea7163afa15718a768b9b01ab822ff8a6f139ffd8c93377525d26b0c83917acb19944ad485b161dd5162a14f1bf
7
+ data.tar.gz: c8266151ccbd5d50042c289e1ccd2993854dc305e364576489d08a38ab0a0b42166a93a1ac454aac438f306cb46785055cca600003c50bbdf1f8eadd288e72fa
@@ -1,5 +1,5 @@
1
1
  bump:
2
2
  recurse: false
3
3
  file: 'lib/solidus_jwt/version.rb'
4
- message: Bump SolidusJwt to %{version}
5
- branch: true
4
+ message: Bump SolidusDemo1 to %{version}
5
+ tag: true
@@ -6,26 +6,9 @@ AllCops:
6
6
  - sandbox/**/*
7
7
  - spec/dummy/**/*
8
8
 
9
- Style/FrozenStringLiteralComment:
10
- Enabled: false
11
-
12
- Naming/PredicateName:
13
- Exclude:
14
- - app/decorators/solidus_kits/spree/stock/availability_validator_decorator.rb
15
-
16
9
  Metrics/LineLength:
17
10
  Enabled: false
18
11
 
19
- Rails/SkipsModelValidations:
20
- Exclude:
21
- - 'spec/**/*'
22
-
23
- RSpec/BeforeAfterAll:
24
- Enabled: false
25
-
26
- RSpec/ContextWording:
27
- Enabled: false
28
-
29
12
  RSpec/MultipleExpectations:
30
13
  Enabled: false
31
14
 
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
  git_source(:github) { |repo| "https://github.com/#{repo}.git" }
3
5
 
data/README.md CHANGED
@@ -111,6 +111,19 @@ You can now use the `access_token` to authentication with the
111
111
  [Solidus API](https://github.com/solidusio/solidus/tree/master/api) in place
112
112
  of the `spree_api_key`.
113
113
 
114
+ #### Matching token to a user
115
+
116
+ By default, the token matches a user using the `Spree::User.for_jwt` method. This methods
117
+ Finds a user by id using the subject claim of the token. If you want to customize how the
118
+ subject claim is interpreted you can override this method
119
+
120
+ ```ruby
121
+ def self.for_jwt(sub)
122
+ # find_by(id: sub)
123
+ find_by(my_external_id: sub)
124
+ end
125
+ ```
126
+
114
127
  ### Obtain a refresh token
115
128
 
116
129
  To refresh your access token, instead of re-authenticating you can send
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SolidusJwt
2
4
  module Spree
3
5
  module Api
@@ -16,7 +18,7 @@ module SolidusJwt
16
18
  return super if json_web_token.blank?
17
19
 
18
20
  # rubocop:disable Naming/MemoizedInstanceVariableName
19
- @current_api_user ||= ::Spree.user_class.find_by(id: json_web_token['id'])
21
+ @current_api_user ||= ::Spree.user_class.for_jwt(json_web_token['sub'] || json_web_token['id'])
20
22
  # rubocop:enable Naming/MemoizedInstanceVariableName
21
23
  end
22
24
 
@@ -1,10 +1,32 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SolidusJwt
2
4
  module Spree
3
5
  module UserDecorator
4
6
  def self.prepended(base)
7
+ base.extend ClassMethods
5
8
  base.has_many :auth_tokens, class_name: 'SolidusJwt::Token'
6
9
  end
7
10
 
11
+ module ClassMethods
12
+ ##
13
+ # Find user based on subject claim in
14
+ # our json web token
15
+ # @see https://tools.ietf.org/html/rfc7519#section-4.1.2
16
+ #
17
+ # @example get user token
18
+ # payload = SolidusJwt.decode(token).first
19
+ # user = Spree::User.for_jwt(payload['sub'])
20
+ #
21
+ # @param sub [string] The subject claim of jwt
22
+ # @return [Spree.user_class, NilClass] If a match is found, returns the user,
23
+ # otherwise, returns nil
24
+ #
25
+ def for_jwt(sub)
26
+ find_by(id: sub)
27
+ end
28
+ end
29
+
8
30
  ##
9
31
  # Generate a json web token
10
32
  # @see https://github.com/jwt/ruby-jwt
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SolidusJwt
2
4
  base_class = defined?(::ApplicationRecord) ? ::ApplicationRecord : ActiveRecord::Base
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SolidusJwt
2
4
  class Token < BaseRecord
3
5
  attr_readonly :token
data/bin/r ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ warn %{
5
+ DEPRECATION: bin/r has been replaced by bin/rails-engine, please use that
6
+ command instead.
7
+ }.strip
8
+
9
+ exec "#{__dir__}/rails-engine"
data/bin/rails CHANGED
@@ -1,18 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
-
3
2
  # frozen_string_literal: true
4
3
 
5
- app_root = 'sandbox'
6
-
7
- unless File.exist? "#{app_root}/bin/rails"
8
- warn 'Creating the sandbox app...'
9
- Dir.chdir "#{__dir__}/.." do
10
- system "#{__dir__}/sandbox" or begin # rubocop:disable Style/AndOr
11
- warn 'Automatic creation of the sandbox app failed'
12
- exit 1
13
- end
14
- end
4
+ if %w[g generate].include? ARGV.first
5
+ exec "#{__dir__}/rails-engine", *ARGV
6
+ else
7
+ exec "#{__dir__}/rails-sandbox", *ARGV
15
8
  end
16
-
17
- Dir.chdir app_root
18
- exec 'bin/rails', *ARGV
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # This command will automatically be run when you run "rails" with Rails gems
5
+ # installed from the root of your application.
6
+
7
+ ENGINE_ROOT = File.expand_path('..', __dir__)
8
+ ENGINE_PATH = File.expand_path('../lib/solidus_jwt/engine', __dir__)
9
+
10
+ # Set up gems listed in the Gemfile.
11
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
12
+ require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
13
+
14
+ require 'rails/all'
15
+ require 'rails/engine/commands'
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ app_root = 'sandbox'
5
+
6
+ unless File.exist? "#{app_root}/bin/rails"
7
+ warn 'Creating the sandbox app...'
8
+ Dir.chdir "#{__dir__}/.." do
9
+ system "#{__dir__}/sandbox" or begin # rubocop:disable Style/AndOr
10
+ warn 'Automatic creation of the sandbox app failed'
11
+ exit 1
12
+ end
13
+ end
14
+ end
15
+
16
+ Dir.chdir app_root
17
+ exec 'bin/rails', *ARGV
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "rubygems"
5
+ require "bundler/setup"
6
+
7
+ load Gem.bin_path("rake", "rake")
@@ -53,7 +53,9 @@ gem 'solidus', github: 'solidusio/solidus', branch: '$BRANCH'
53
53
  gem 'solidus_auth_devise', '>= 2.1.0'
54
54
  gem 'rails-i18n'
55
55
  gem 'solidus_i18n'
56
+
56
57
  gem '$extension_name', path: '..'
58
+
57
59
  group :test, :development do
58
60
  platforms :mri do
59
61
  gem 'pry-byebug'
@@ -69,6 +71,7 @@ unbundled bundle exec rails generate spree:install \
69
71
  --auto-accept \
70
72
  --user_class=Spree::User \
71
73
  --enforce_available_locales=true \
74
+ --with-authentication=false \
72
75
  $@
73
76
 
74
77
  unbundled bundle exec rails generate solidus:auth:install
@@ -78,4 +81,4 @@ echo "🚀 Sandbox app successfully created for $extension_name!"
78
81
  echo "🚀 Using $RAILSDB and Solidus $BRANCH"
79
82
  echo "🚀 Use 'export DB=[postgres|mysql|sqlite]' to control the DB adapter"
80
83
  echo "🚀 Use 'export SOLIDUS_BRANCH=<BRANCH-NAME>' to control the Solidus version"
81
- echo "🚀 This app is intended for test purposes."
84
+ echo "🚀 This app is intended for test purposes."
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ warn %{
5
+ DEPRECATION: bin/sandbox_rails has been replaced by bin/rails-sandbox, please
6
+ use that command instead.
7
+ }.strip
8
+
9
+ exec "#{__dir__}/rails-engine"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Spree::Core::Engine.routes.draw do
2
4
  # Add your extension routes here
3
5
  post 'oauth/token', to: 'api/oauths#token'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class CreateSolidusJwtTokens < ActiveRecord::Migration[5.2]
2
4
  def change
3
5
  create_table :solidus_jwt_tokens do |t|
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class AddForeignKeyToUsersTable < ActiveRecord::Migration[5.2]
2
4
  def change
3
5
  add_foreign_key :solidus_jwt_tokens, Spree.user_class.table_name, column: :user_id, on_delete: :cascade
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Spree
2
4
  module Api
3
5
  class OauthsController < BaseController
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SolidusJwt
2
4
  module Generators
3
5
  class InstallGenerator < Rails::Generators::Base
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'jwt'
2
4
 
3
5
  require 'solidus_core'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SolidusJwt
2
4
  module Decodeable
3
5
  ##
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SolidusJwt
2
4
  module Encodeable
3
5
  ##
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'solidus_jwt/preferences'
2
4
 
3
5
  SolidusJwt::Config = SolidusJwt::Preferences.new
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SolidusJwt
2
4
  module DeviseStrategies
3
5
  class Base < Devise::Strategies::Authenticatable
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SolidusJwt
2
4
  module DeviseStrategies
3
5
  class Password < Base
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SolidusJwt
2
4
  module DeviseStrategies
3
5
  class RefreshToken < Base
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SolidusJwt
2
4
  module Distributor
3
5
  module Devise
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spree/core'
2
4
 
3
5
  module SolidusJwt
4
6
  class Engine < Rails::Engine
5
- include SolidusSupport::EngineExtensions::Decorators
7
+ include SolidusSupport::EngineExtensions
6
8
 
7
9
  isolate_namespace ::Spree
8
10
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  FactoryBot.define do
2
4
  # Define your Spree extensions Factories within this file to enable applications, and other extensions to use and override them.
3
5
  #
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spree/preferences/configuration'
2
4
 
3
5
  module SolidusJwt
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module SolidusJwt
2
- VERSION = '1.1.0'
4
+ VERSION = '1.2.0'
3
5
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'lib/solidus_jwt/version'
2
4
 
3
5
  Gem::Specification.new do |s|
@@ -29,7 +31,7 @@ Gem::Specification.new do |s|
29
31
  s.add_dependency 'jwt'
30
32
  s.add_dependency 'solidus_auth_devise'
31
33
  s.add_dependency 'solidus_core', ['>= 2.0.0', '< 3']
32
- s.add_dependency 'solidus_support', '~> 0.4.0'
34
+ s.add_dependency 'solidus_support', '~> 0.5.0'
33
35
 
34
36
  s.add_development_dependency 'byebug'
35
37
  s.add_development_dependency 'solidus_dev_support'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  RSpec.describe SolidusJwt::Config do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'spree/testing_support/factories/user_factory'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'spree/testing_support/factories/user_factory'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  RSpec.describe SolidusJwt::Preferences do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  RSpec.describe SolidusJwt do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
 
3
5
  RSpec.describe SolidusJwt::Token, type: :model do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'spree/testing_support/factories/user_factory'
3
5
 
@@ -46,11 +48,11 @@ RSpec.describe 'SolidusJwt Authentication', type: :request do
46
48
  end
47
49
 
48
50
  context 'when spree api key is not allowed' do
49
- before(:all) do
51
+ before do
50
52
  SolidusJwt::Config.allow_spree_api_key = false
51
53
  end
52
54
 
53
- after(:all) do
55
+ after do
54
56
  SolidusJwt::Config.allow_spree_api_key = true
55
57
  end
56
58
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
2
4
  require 'spree/testing_support/factories/user_factory'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Configure Rails Environment
2
4
  ENV['RAILS_ENV'] = 'test'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  RSpec.shared_examples 'Decodeable Examples' do
2
4
  describe 'decode' do
3
5
  let(:token) do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  RSpec.shared_examples 'Encodeable Examples' do
2
4
  describe 'encode' do
3
5
  let(:payload) { { user_id: 1 } }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solidus_jwt
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Taylor Scott
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-03-16 00:00:00.000000000 Z
11
+ date: 2020-06-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jwt
@@ -64,14 +64,14 @@ dependencies:
64
64
  requirements:
65
65
  - - "~>"
66
66
  - !ruby/object:Gem::Version
67
- version: 0.4.0
67
+ version: 0.5.0
68
68
  type: :runtime
69
69
  prerelease: false
70
70
  version_requirements: !ruby/object:Gem::Requirement
71
71
  requirements:
72
72
  - - "~>"
73
73
  - !ruby/object:Gem::Version
74
- version: 0.4.0
74
+ version: 0.5.0
75
75
  - !ruby/object:Gem::Dependency
76
76
  name: byebug
77
77
  requirement: !ruby/object:Gem::Requirement
@@ -120,19 +120,24 @@ files:
120
120
  - README.md
121
121
  - Rakefile
122
122
  - _config.yml
123
- - app/controllers/spree/api/oauths_controller.rb
124
- - app/decorators/solidus_jwt/spree/api/base_controller_decorator.rb
125
- - app/decorators/solidus_jwt/spree/user_decorator.rb
123
+ - app/decorators/controllers/solidus_jwt/spree/api/base_controller_decorator.rb
124
+ - app/decorators/models/solidus_jwt/spree/user_decorator.rb
126
125
  - app/models/solidus_jwt/base_record.rb
127
126
  - app/models/solidus_jwt/token.rb
128
127
  - bin/console
128
+ - bin/r
129
129
  - bin/rails
130
+ - bin/rails-engine
131
+ - bin/rails-sandbox
132
+ - bin/rake
130
133
  - bin/sandbox
134
+ - bin/sandbox_rails
131
135
  - bin/setup
132
136
  - config/locales/en.yml
133
137
  - config/routes.rb
134
138
  - db/migrate/20190222220038_create_solidus_jwt_tokens.rb
135
139
  - db/migrate/20191212083655_add_foreign_key_to_users_table.rb
140
+ - lib/controllers/api/spree/api/oauths_controller.rb
136
141
  - lib/generators/solidus_jwt/install/install_generator.rb
137
142
  - lib/solidus_jwt.rb
138
143
  - lib/solidus_jwt/concerns/decodeable.rb