cm-graphql 0.0.11 → 0.0.12
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 +4 -4
- data/Gemfile.lock +1 -1
- data/app/exceptions/recaptcha_verification_failed.rb +17 -0
- data/app/graphql/cm_graphql/extensions/recaptcha_extension.rb +35 -0
- data/app/graphql/mutations/base_mutation.rb +5 -0
- data/app/services/recaptcha_verifier.rb +44 -0
- data/cm-graphql.gemspec +2 -2
- data/config/initializers/constants.rb +2 -1
- metadata +9 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3adaf1303f53f1360bcb8497b025b1960958b8fc285c7abe2a6547a6be56a620
|
|
4
|
+
data.tar.gz: 857f7282ed8dc874f1f886cba9d93b384cd2be4ee4d4ec84513bf989265f99d1
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9f5d3aa1a49d5fc8922a72366a7ba05b2a821cea8c094994aad1bbd205d8a1cdb11dce7ceb7ad0755547629c140753da12b728a5d84f1c5db0b239fb1222d26e
|
|
7
|
+
data.tar.gz: 70b4a6394d65a12ffd916c1e44f6d1654e40104c7c1ab348a7680c12a3726583bfb49da56d7a9bbe7a062e9542bd32f1a2e0ab200a9dfd442b92af6fe976d89d
|
data/Gemfile.lock
CHANGED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
class RecaptchaVerificationFailed < BaseException
|
|
2
|
+
def initialize(message = nil)
|
|
3
|
+
super(message)
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
def message
|
|
7
|
+
@message || 'Recaptcha verification failed'
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def code
|
|
11
|
+
:unprocessable_entity
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def sub_code
|
|
15
|
+
:recaptcha_verification_failed
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
module CmGraphql
|
|
2
|
+
module Extensions
|
|
3
|
+
class RecaptchaExtension < GraphQL::Schema::FieldExtension
|
|
4
|
+
def apply
|
|
5
|
+
if field.resolver
|
|
6
|
+
field.resolver.argument(:recaptcha_token, String, required: true)
|
|
7
|
+
else
|
|
8
|
+
field.argument(:recaptcha_token, String, required: true)
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def resolve(object:, arguments:, context:)
|
|
13
|
+
args_hash = arguments.to_h
|
|
14
|
+
input_hash = args_hash[:input].respond_to?(:to_h) ? args_hash[:input].to_h : nil
|
|
15
|
+
|
|
16
|
+
recaptcha_token = input_hash ? input_hash[:recaptcha_token] : args_hash[:recaptcha_token]
|
|
17
|
+
|
|
18
|
+
RecaptchaVerifier.verify_v3!(
|
|
19
|
+
token: recaptcha_token,
|
|
20
|
+
action: options[:action] || field.name.to_s,
|
|
21
|
+
remote_ip: context[:request]&.remote_ip,
|
|
22
|
+
minimum_score: options[:minimum_score] || RECAPTCHA_MINIMUM_SCORE
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
if input_hash
|
|
26
|
+
next_input = input_hash.dup
|
|
27
|
+
next_input.delete(:recaptcha_token)
|
|
28
|
+
yield(object, args_hash.merge(input: next_input))
|
|
29
|
+
else
|
|
30
|
+
yield(object, arguments.except(:recaptcha_token))
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require 'net/http'
|
|
2
|
+
require 'json'
|
|
3
|
+
|
|
4
|
+
class RecaptchaVerifier
|
|
5
|
+
VERIFY_URL = URI.parse('https://www.google.com/recaptcha/api/siteverify').freeze
|
|
6
|
+
|
|
7
|
+
def self.verify_v3(token:, action:, remote_ip: nil, minimum_score: 0.5)
|
|
8
|
+
secret = Rails.application.credentials.dig(:gcp, :recaptcha_secret_key)
|
|
9
|
+
raise RecaptchaVerificationFailed, "Please cofigure recaptcha credentials, so it reads Rails.application.credentials.dig(:gcp, :recaptcha_secret_key)." if secret.blank? || token.blank?
|
|
10
|
+
|
|
11
|
+
response = post_verify(secret:, token:, remote_ip:)
|
|
12
|
+
raise RecaptchaVerificationFailed, "Invalid response from recaptcha" unless response.is_a?(Hash)
|
|
13
|
+
|
|
14
|
+
raise RecaptchaVerificationFailed, "Recaptcha verification failed" unless response['success'] == true
|
|
15
|
+
raise RecaptchaVerificationFailed, "Recaptcha action mismatch" if response['action'].present? && response['action'] != action
|
|
16
|
+
|
|
17
|
+
score = response['score']
|
|
18
|
+
return true if score.nil?
|
|
19
|
+
|
|
20
|
+
score.to_f >= minimum_score.to_f
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def self.verify_v3!(token:, action:, remote_ip: nil, minimum_score: 0.5)
|
|
24
|
+
success = verify_v3(token:, action:, remote_ip:, minimum_score:)
|
|
25
|
+
raise RecaptchaVerificationFailed unless success
|
|
26
|
+
|
|
27
|
+
true
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def self.post_verify(secret:, token:, remote_ip: nil)
|
|
31
|
+
request = Net::HTTP::Post.new(VERIFY_URL)
|
|
32
|
+
request.set_form_data({ secret:, response: token }.tap { |h| h[:remoteip] = remote_ip if remote_ip.present? })
|
|
33
|
+
|
|
34
|
+
http = Net::HTTP.new(VERIFY_URL.host, VERIFY_URL.port)
|
|
35
|
+
http.use_ssl = true
|
|
36
|
+
|
|
37
|
+
raw_response = http.request(request)
|
|
38
|
+
JSON.parse(raw_response.body)
|
|
39
|
+
rescue JSON::ParserError, SocketError, Timeout::Error, Errno::ECONNRESET, Errno::ECONNREFUSED
|
|
40
|
+
false
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
private_class_method :post_verify
|
|
44
|
+
end
|
data/cm-graphql.gemspec
CHANGED
|
@@ -2,13 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
Gem::Specification.new do |spec|
|
|
4
4
|
spec.name = 'cm-graphql'
|
|
5
|
-
spec.version = '0.0.
|
|
5
|
+
spec.version = '0.0.12'
|
|
6
6
|
spec.date = '2022-09-14'
|
|
7
7
|
spec.summary = 'A gem to setup grapqhl basics like pagination, file upload'
|
|
8
8
|
spec.description = 'A gem to setup grapqhl basics like pagination, file upload'
|
|
9
9
|
spec.authors = ['Anbazhagan Palani']
|
|
10
10
|
spec.email = ['anbu@commutatus.com']
|
|
11
|
-
spec.homepage = 'https://github.com/commutatus/
|
|
11
|
+
spec.homepage = 'https://github.com/commutatus/cm-graphql'
|
|
12
12
|
spec.license = 'MIT'
|
|
13
13
|
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
14
14
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
metadata
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: cm-graphql
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.12
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Anbazhagan Palani
|
|
8
|
+
autorequire:
|
|
8
9
|
bindir: exe
|
|
9
10
|
cert_chain: []
|
|
10
11
|
date: 2022-09-14 00:00:00.000000000 Z
|
|
@@ -84,6 +85,8 @@ files:
|
|
|
84
85
|
- Gemfile.lock
|
|
85
86
|
- README.md
|
|
86
87
|
- app/exceptions/base_exception.rb
|
|
88
|
+
- app/exceptions/recaptcha_verification_failed.rb
|
|
89
|
+
- app/graphql/cm_graphql/extensions/recaptcha_extension.rb
|
|
87
90
|
- app/graphql/mutations/authenticated_mutation.rb
|
|
88
91
|
- app/graphql/mutations/base_mutation.rb
|
|
89
92
|
- app/graphql/queries/base_query.rb
|
|
@@ -103,6 +106,7 @@ files:
|
|
|
103
106
|
- app/models/concerns/attachable.rb
|
|
104
107
|
- app/models/concerns/paginator.rb
|
|
105
108
|
- app/models/filtered_list.rb
|
|
109
|
+
- app/services/recaptcha_verifier.rb
|
|
106
110
|
- cm-graphql.gemspec
|
|
107
111
|
- config/initializers/active_record_extension.rb
|
|
108
112
|
- config/initializers/constants.rb
|
|
@@ -115,10 +119,11 @@ files:
|
|
|
115
119
|
- lib/generators/cm_graphql/templates/list_type.rb
|
|
116
120
|
- lib/generators/cm_graphql/templates/query_type.rb
|
|
117
121
|
- lib/generators/cm_graphql/templates/record_type.rb
|
|
118
|
-
homepage: https://github.com/commutatus/
|
|
122
|
+
homepage: https://github.com/commutatus/cm-graphql
|
|
119
123
|
licenses:
|
|
120
124
|
- MIT
|
|
121
125
|
metadata: {}
|
|
126
|
+
post_install_message:
|
|
122
127
|
rdoc_options: []
|
|
123
128
|
require_paths:
|
|
124
129
|
- lib
|
|
@@ -133,7 +138,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
133
138
|
- !ruby/object:Gem::Version
|
|
134
139
|
version: '0'
|
|
135
140
|
requirements: []
|
|
136
|
-
rubygems_version: 3.
|
|
141
|
+
rubygems_version: 3.5.9
|
|
142
|
+
signing_key:
|
|
137
143
|
specification_version: 4
|
|
138
144
|
summary: A gem to setup grapqhl basics like pagination, file upload
|
|
139
145
|
test_files: []
|