activegraphql 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +36 -0
- data/lib/activegraphql/query.rb +34 -2
- data/lib/activegraphql/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7d7b827f9b88c797862ac466ea064c90a7f1d310
|
4
|
+
data.tar.gz: 84a3cbb3591d9b0578148c035a05497c58b2c141
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 565f09a87675ddf99b4d6408e5fb0479323778f44f14873aa7d19aac493c3c15c81b9533cf2d01b61d8c821eac731d76a70d604e62ae06928cc415ee94582d85
|
7
|
+
data.tar.gz: c63f44e7d151303ef0b3794ca305e9342a84909eb493d842ab18f5c2a5981175c1b5e939966995a334e41f9825c68397244ade7a803b04636a5b6a6ad8d18682
|
data/README.md
CHANGED
@@ -130,3 +130,39 @@ class MyModel < ActiveGraphQL::Model
|
|
130
130
|
max_interval: 1 }
|
131
131
|
end
|
132
132
|
```
|
133
|
+
|
134
|
+
### Authorization
|
135
|
+
It's currently supporting simple bearer authorization using `auth` option.
|
136
|
+
|
137
|
+
It basically offers two configuration params:
|
138
|
+
|
139
|
+
- `strategy`: Currently, `bearer` strategy is the only one available.
|
140
|
+
- `class`: The existing strategy uses your own custom class to encode a token (the class must provide at least an `.encode` class method).
|
141
|
+
|
142
|
+
Your encoder class may look like that:
|
143
|
+
```ruby
|
144
|
+
class YourEncoderClass
|
145
|
+
def self.secret
|
146
|
+
'your-safely-secured-secret'
|
147
|
+
end
|
148
|
+
|
149
|
+
def self.encode
|
150
|
+
# You could have custom stuff here like adding expiration to the payload.
|
151
|
+
payload = { exp: (Time.current.to_i + 100) }
|
152
|
+
JWT.encode(payload, secret)
|
153
|
+
end
|
154
|
+
|
155
|
+
def self.decode(token)
|
156
|
+
JWT.decode(token, secret)
|
157
|
+
end
|
158
|
+
end
|
159
|
+
```
|
160
|
+
|
161
|
+
Then your configuration would look like the next:
|
162
|
+
```ruby
|
163
|
+
class MyModel < ActiveGraphQL::Model
|
164
|
+
configure auth: { strategy: :bearer, class: YourEncoderClass }
|
165
|
+
end
|
166
|
+
```
|
167
|
+
|
168
|
+
|
data/lib/activegraphql/query.rb
CHANGED
@@ -18,12 +18,24 @@ module ActiveGraphQL
|
|
18
18
|
end
|
19
19
|
|
20
20
|
def request_options
|
21
|
-
{
|
22
|
-
opts
|
21
|
+
{}.tap do |opts|
|
22
|
+
opts[:query] = request_params
|
23
|
+
opts[:headers] = request_headers if request_headers.present?
|
23
24
|
opts.merge!(config[:http]) if config[:http].present?
|
24
25
|
end
|
25
26
|
end
|
26
27
|
|
28
|
+
def request_headers
|
29
|
+
{}.tap do |headers|
|
30
|
+
headers['Authorization'] = "Bearer #{auth_token}" if auth_header?
|
31
|
+
headers['Accept-Language'] = locale.to_s if locale.present?
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def request_params
|
36
|
+
{ query: to_s }
|
37
|
+
end
|
38
|
+
|
27
39
|
def response_data
|
28
40
|
return unless response['data']
|
29
41
|
to_snake_case(response['data'][qaction])
|
@@ -73,6 +85,26 @@ module ActiveGraphQL
|
|
73
85
|
|
74
86
|
private
|
75
87
|
|
88
|
+
def auth_header?
|
89
|
+
auth_strategy == :bearer
|
90
|
+
end
|
91
|
+
|
92
|
+
def auth_config
|
93
|
+
@auth_config ||= config[:auth] || {}
|
94
|
+
end
|
95
|
+
|
96
|
+
def auth_strategy
|
97
|
+
@auth_strategy ||= auth_config[:strategy]
|
98
|
+
end
|
99
|
+
|
100
|
+
# ActiveGraphQL currently supports bearer authorization with given class to encode.
|
101
|
+
# So if the "bearer" is not configured or the "class" is not present it's
|
102
|
+
# returning a nil token.
|
103
|
+
def auth_token
|
104
|
+
return if auth_config[:strategy] != :bearer || auth_config[:class].blank?
|
105
|
+
@auth_token ||= auth_config[:class].encode
|
106
|
+
end
|
107
|
+
|
76
108
|
def to_snake_case(value)
|
77
109
|
case value
|
78
110
|
when Array
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activegraphql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wakoopa
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-08-
|
11
|
+
date: 2016-08-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|