solidus_jwt 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +15 -2
- data/app/controllers/spree/api/base_controller/json_web_tokens.rb +7 -4
- data/app/controllers/spree/api/base_controller_decorator.rb +14 -4
- data/app/models/spree/user_decorator.rb +26 -12
- data/lib/solidus_jwt/concerns/decodeable.rb +1 -1
- data/lib/solidus_jwt/concerns/encodeable.rb +9 -7
- data/lib/solidus_jwt/engine.rb +3 -1
- data/lib/solidus_jwt/version.rb +2 -2
- metadata +55 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0cf6dd72fda604868c1589819000a6d1607a8078fa642ce2a17cb071695b26f1
|
4
|
+
data.tar.gz: ca122c197a880c21398f6bcf28a2e8dd7709f90613e0fe41c4f24ea166802e09
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c1a2929d69915b28aec1df8c2a4fa450191ae0c465c57e5b7992938a88c9fb6363ae71282930b4dcea644add482d54c3406eb471a93a0c74a520ad8e11e9e98e
|
7
|
+
data.tar.gz: b7938c410ed7e14bc8098aa7314490beaff2bfc29b86249c72c9a2411f48b3a2b71441b6c17a159fff864caaf1923e348ba5b01c5acfcc570be3c60c1e75a571
|
data/README.md
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
SolidusJwt
|
2
2
|
==========
|
3
3
|
|
4
|
+
[![CircleCI](https://circleci.com/gh/skukx/solidus_jwt.svg?style=svg)](https://circleci.com/gh/skukx/solidus_jwt)
|
5
|
+
[![Gem Version](https://badge.fury.io/rb/solidus_jwt.svg)](https://badge.fury.io/rb/solidus_jwt)
|
6
|
+
|
4
7
|
This gem gives [Solidus](https://github.com/solidusio/solidus) stores the ability to authenticate API requests with
|
5
8
|
JSON Web Tokens.
|
6
9
|
|
@@ -64,10 +67,20 @@ end
|
|
64
67
|
|
65
68
|
user = Spree::User.new email: 'email@example.com', id: 1
|
66
69
|
token = user.generate_jwt_token(expires_in: 1.hour.to_i) # Expiration is time in seconds
|
67
|
-
# eyJhbGciOiJIUzI1NiJ9.
|
70
|
+
# eyJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwiZW1haWwiOiJlbWFpbEBleGFtcGxlLmNvbSIsInN1YiI6MSwiZXhwIjoxNTcyNTg2NTA3LCJpYXQiOjE1NzI1ODI5MDcsImlzcyI6InNvbGlkdXMifQ.UEmPLClCmOii_5-Qa6fB_ToGavIJYY6PAyfhARitMwI
|
68
71
|
|
69
72
|
SolidusJwt.decode(token)
|
70
|
-
# [
|
73
|
+
# [
|
74
|
+
# {
|
75
|
+
# "id"=>1,
|
76
|
+
# "email"=>"email@example.com",
|
77
|
+
# "sub"=>1,
|
78
|
+
# "exp"=>1572586507,
|
79
|
+
# "iat"=>1572582907,
|
80
|
+
# "iss"=>"solidus"
|
81
|
+
# },
|
82
|
+
# {"alg"=>"HS256"}
|
83
|
+
# ]
|
71
84
|
```
|
72
85
|
|
73
86
|
### Distributing a Token Using 'solidus_auth_devise':
|
@@ -1,7 +1,13 @@
|
|
1
|
+
##
|
2
|
+
# Prepend for Spree::Api::BaseController methods
|
3
|
+
#
|
1
4
|
module Spree::Api::BaseController::JsonWebTokens
|
2
5
|
def load_user
|
3
6
|
return super unless json_web_token.present?
|
7
|
+
|
8
|
+
# rubocop:disable Naming/MemoizedInstanceVariableName
|
4
9
|
@current_api_user ||= Spree.user_class.find_by(id: json_web_token['id'])
|
10
|
+
# rubocop:enable Naming/MemoizedInstanceVariableName
|
5
11
|
end
|
6
12
|
|
7
13
|
def json_web_token
|
@@ -10,10 +16,7 @@ module Spree::Api::BaseController::JsonWebTokens
|
|
10
16
|
# Allow spree to try and authenticate if we still allow it. Otherwise
|
11
17
|
# raise an error
|
12
18
|
return if SolidusJwt::Config.allow_spree_api_key
|
13
|
-
raise
|
14
|
-
end
|
15
19
|
|
16
|
-
|
17
|
-
api_key.split('.').size != 3
|
20
|
+
raise
|
18
21
|
end
|
19
22
|
end
|
@@ -1,7 +1,17 @@
|
|
1
|
-
Spree
|
2
|
-
|
1
|
+
module Spree
|
2
|
+
module Api
|
3
|
+
module BaseControllerDecorator
|
4
|
+
extend ActiveSupport::Concern
|
3
5
|
|
4
|
-
|
5
|
-
|
6
|
+
included do
|
7
|
+
prepend Spree::Api::BaseController::JsonWebTokens
|
8
|
+
|
9
|
+
rescue_from JWT::DecodeError do
|
10
|
+
render "spree/api/errors/invalid_api_key", status: 401
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
Spree::Api::BaseController.include self
|
15
|
+
end
|
6
16
|
end
|
7
17
|
end
|
@@ -1,16 +1,30 @@
|
|
1
|
-
Spree
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
1
|
+
module Spree
|
2
|
+
module UserDecorator
|
3
|
+
##
|
4
|
+
# Generate a json web token
|
5
|
+
# @see https://github.com/jwt/ruby-jwt
|
6
|
+
# @return [String]
|
7
|
+
#
|
8
|
+
def generate_jwt(expires_in: nil)
|
9
|
+
SolidusJwt.encode(payload: as_jwt_payload, expires_in: expires_in)
|
10
|
+
end
|
11
|
+
alias generate_jwt_token generate_jwt
|
12
|
+
|
13
|
+
##
|
14
|
+
# Serializes user attributes to hash and applies
|
15
|
+
# the sub jwt claim.
|
16
|
+
#
|
17
|
+
# @return [Hash] The payload for json web token
|
18
|
+
#
|
19
|
+
def as_jwt_payload
|
20
|
+
options = SolidusJwt::Config.jwt_options
|
21
|
+
claims = { sub: id }
|
10
22
|
|
11
|
-
|
23
|
+
as_json(options)
|
24
|
+
.merge(claims)
|
25
|
+
.as_json
|
26
|
+
end
|
12
27
|
|
13
|
-
|
14
|
-
as_json(only: %i[email id])
|
28
|
+
Spree.user_class.prepend self
|
15
29
|
end
|
16
30
|
end
|
@@ -10,15 +10,17 @@ module SolidusJwt
|
|
10
10
|
# @return [String]
|
11
11
|
#
|
12
12
|
def encode(payload:, expires_in: nil)
|
13
|
-
|
13
|
+
jwt_payload = payload.dup.with_indifferent_access
|
14
|
+
|
14
15
|
current_time = Time.current.to_i
|
15
|
-
extras = {}
|
16
|
-
extras['exp'] = current_time + expires_in if expires_in.present?
|
17
|
-
extras['iat'] = current_time
|
18
16
|
|
19
|
-
|
20
|
-
|
21
|
-
|
17
|
+
# @see https://github.com/jwt/ruby-jwt#support-for-reserved-claim-names
|
18
|
+
jwt_payload[:exp] ||= current_time + expires_in if expires_in.present?
|
19
|
+
jwt_payload[:iat] ||= current_time
|
20
|
+
jwt_payload[:iss] ||= 'solidus'
|
21
|
+
|
22
|
+
JWT.encode(jwt_payload, SolidusJwt::Config.jwt_secret,
|
23
|
+
SolidusJwt::Config.jwt_algorithm)
|
22
24
|
end
|
23
25
|
end
|
24
26
|
end
|
data/lib/solidus_jwt/engine.rb
CHANGED
@@ -10,7 +10,9 @@ module SolidusJwt
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def self.activate
|
13
|
-
|
13
|
+
decorator_pattern = File.join(__dir__, '../../app/**/*_decorator*.rb')
|
14
|
+
|
15
|
+
Dir.glob(decorator_pattern) do |c|
|
14
16
|
Rails.configuration.cache_classes ? require(c) : load(c)
|
15
17
|
end
|
16
18
|
end
|
data/lib/solidus_jwt/version.rb
CHANGED
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: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Taylor Scott
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-11-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: solidus_core
|
@@ -107,7 +107,7 @@ dependencies:
|
|
107
107
|
- !ruby/object:Gem::Version
|
108
108
|
version: '0'
|
109
109
|
- !ruby/object:Gem::Dependency
|
110
|
-
name:
|
110
|
+
name: coffee-rails
|
111
111
|
requirement: !ruby/object:Gem::Requirement
|
112
112
|
requirements:
|
113
113
|
- - ">="
|
@@ -121,7 +121,7 @@ dependencies:
|
|
121
121
|
- !ruby/object:Gem::Version
|
122
122
|
version: '0'
|
123
123
|
- !ruby/object:Gem::Dependency
|
124
|
-
name:
|
124
|
+
name: database_cleaner
|
125
125
|
requirement: !ruby/object:Gem::Requirement
|
126
126
|
requirements:
|
127
127
|
- - ">="
|
@@ -135,7 +135,7 @@ dependencies:
|
|
135
135
|
- !ruby/object:Gem::Version
|
136
136
|
version: '0'
|
137
137
|
- !ruby/object:Gem::Dependency
|
138
|
-
name:
|
138
|
+
name: factory_bot
|
139
139
|
requirement: !ruby/object:Gem::Requirement
|
140
140
|
requirements:
|
141
141
|
- - ">="
|
@@ -149,7 +149,7 @@ dependencies:
|
|
149
149
|
- !ruby/object:Gem::Version
|
150
150
|
version: '0'
|
151
151
|
- !ruby/object:Gem::Dependency
|
152
|
-
name:
|
152
|
+
name: ffaker
|
153
153
|
requirement: !ruby/object:Gem::Requirement
|
154
154
|
requirements:
|
155
155
|
- - ">="
|
@@ -163,7 +163,7 @@ dependencies:
|
|
163
163
|
- !ruby/object:Gem::Version
|
164
164
|
version: '0'
|
165
165
|
- !ruby/object:Gem::Dependency
|
166
|
-
name:
|
166
|
+
name: gem-release
|
167
167
|
requirement: !ruby/object:Gem::Requirement
|
168
168
|
requirements:
|
169
169
|
- - ">="
|
@@ -177,7 +177,7 @@ dependencies:
|
|
177
177
|
- !ruby/object:Gem::Version
|
178
178
|
version: '0'
|
179
179
|
- !ruby/object:Gem::Dependency
|
180
|
-
name:
|
180
|
+
name: poltergeist
|
181
181
|
requirement: !ruby/object:Gem::Requirement
|
182
182
|
requirements:
|
183
183
|
- - ">="
|
@@ -218,6 +218,48 @@ dependencies:
|
|
218
218
|
- - ">="
|
219
219
|
- !ruby/object:Gem::Version
|
220
220
|
version: '0'
|
221
|
+
- !ruby/object:Gem::Dependency
|
222
|
+
name: rubocop-rspec
|
223
|
+
requirement: !ruby/object:Gem::Requirement
|
224
|
+
requirements:
|
225
|
+
- - ">="
|
226
|
+
- !ruby/object:Gem::Version
|
227
|
+
version: '0'
|
228
|
+
type: :development
|
229
|
+
prerelease: false
|
230
|
+
version_requirements: !ruby/object:Gem::Requirement
|
231
|
+
requirements:
|
232
|
+
- - ">="
|
233
|
+
- !ruby/object:Gem::Version
|
234
|
+
version: '0'
|
235
|
+
- !ruby/object:Gem::Dependency
|
236
|
+
name: sass-rails
|
237
|
+
requirement: !ruby/object:Gem::Requirement
|
238
|
+
requirements:
|
239
|
+
- - ">="
|
240
|
+
- !ruby/object:Gem::Version
|
241
|
+
version: '0'
|
242
|
+
type: :development
|
243
|
+
prerelease: false
|
244
|
+
version_requirements: !ruby/object:Gem::Requirement
|
245
|
+
requirements:
|
246
|
+
- - ">="
|
247
|
+
- !ruby/object:Gem::Version
|
248
|
+
version: '0'
|
249
|
+
- !ruby/object:Gem::Dependency
|
250
|
+
name: selenium-webdriver
|
251
|
+
requirement: !ruby/object:Gem::Requirement
|
252
|
+
requirements:
|
253
|
+
- - ">="
|
254
|
+
- !ruby/object:Gem::Version
|
255
|
+
version: '0'
|
256
|
+
type: :development
|
257
|
+
prerelease: false
|
258
|
+
version_requirements: !ruby/object:Gem::Requirement
|
259
|
+
requirements:
|
260
|
+
- - ">="
|
261
|
+
- !ruby/object:Gem::Version
|
262
|
+
version: '0'
|
221
263
|
- !ruby/object:Gem::Dependency
|
222
264
|
name: simplecov
|
223
265
|
requirement: !ruby/object:Gem::Requirement
|
@@ -236,16 +278,16 @@ dependencies:
|
|
236
278
|
name: sqlite3
|
237
279
|
requirement: !ruby/object:Gem::Requirement
|
238
280
|
requirements:
|
239
|
-
- - "
|
281
|
+
- - ">="
|
240
282
|
- !ruby/object:Gem::Version
|
241
|
-
version:
|
283
|
+
version: '0'
|
242
284
|
type: :development
|
243
285
|
prerelease: false
|
244
286
|
version_requirements: !ruby/object:Gem::Requirement
|
245
287
|
requirements:
|
246
|
-
- - "
|
288
|
+
- - ">="
|
247
289
|
- !ruby/object:Gem::Version
|
248
|
-
version:
|
290
|
+
version: '0'
|
249
291
|
description: Add Json Web Tokens to Solidus API
|
250
292
|
email: t.skukx@gmail.com
|
251
293
|
executables: []
|
@@ -293,8 +335,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
293
335
|
- !ruby/object:Gem::Version
|
294
336
|
version: '0'
|
295
337
|
requirements: []
|
296
|
-
|
297
|
-
rubygems_version: 2.6.13
|
338
|
+
rubygems_version: 3.0.3
|
298
339
|
signing_key:
|
299
340
|
specification_version: 4
|
300
341
|
summary: Add Json Web Tokens to Solidus API
|