ckeditor-webhook 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2622d4d8d3296347733a4b641fb7a0d0c8df34db4d9f4d1cc8ab7aa2b691e9ac
4
+ data.tar.gz: 0bc9768e5ee06e28299eea28dc11d78910d03b3ec3b16ca3abe9bc149899d562
5
+ SHA512:
6
+ metadata.gz: 4480ed9e0a2155a00ce04ab2d1c05230de91cf6f9fe33961e8004cbe3c3ee76aa9edce8b494018b6d37b48c18645c02aef5ae760a4fa606c20ef7db4c876583f
7
+ data.tar.gz: 471e38cde6d23de664319753c36009a8b477c64bb8335c425c3e312133ed79cc52b63b3475d180f4840113bacc9b6b1b680f99fb8386f05f5895665fe7fb802a
@@ -0,0 +1,40 @@
1
+ version: 2.1
2
+ orbs:
3
+ ruby: circleci/ruby@0.1.2
4
+
5
+ jobs:
6
+ build:
7
+ docker:
8
+ - image: circleci/ruby:2.7.2
9
+ executor: ruby/default
10
+ steps:
11
+ - checkout
12
+ - run:
13
+ name: Update bundler
14
+ command: gem install bundler
15
+ - run:
16
+ name: Which bundler?
17
+ command: bundle -v
18
+ # Restore bundle cache
19
+ # Read about caching dependencies: https://circleci.com/docs/2.0/caching/
20
+ - restore_cache:
21
+ key: vendor-bundle-{{ checksum "Gemfile.lock" }}
22
+ # Install Ruby dependencies.
23
+ - run:
24
+ name: Bundle install
25
+ command: bundle check --path=vendor/bundle || bundle install --path=vendor/bundle
26
+ # Store bundle cache for Ruby dependencies.
27
+ - save_cache:
28
+ key: vendor-bundle-{{ checksum "Gemfile.lock" }}
29
+ paths:
30
+ - vendor/bundle
31
+ - run:
32
+ name: Run rspec in parallel
33
+ command: |
34
+ bundle exec rspec --profile 10 \
35
+ --format RspecJunitFormatter \
36
+ --out test_results/rspec.xml \
37
+ --format progress \
38
+ $(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)
39
+ - store_test_results:
40
+ path: test_results
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,10 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## 0.1.0 - 2020-10-30
9
+
10
+ - Initial release
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at clayjs0@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in ckeditor-webhook.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+
8
+ group :test do
9
+ gem "rspec", "~> 3.0"
10
+ gem "rspec_junit_formatter", "~>0.4"
11
+ end
@@ -0,0 +1,37 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ckeditor-webhook (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.4.4)
10
+ rake (12.3.3)
11
+ rspec (3.9.0)
12
+ rspec-core (~> 3.9.0)
13
+ rspec-expectations (~> 3.9.0)
14
+ rspec-mocks (~> 3.9.0)
15
+ rspec-core (3.9.2)
16
+ rspec-support (~> 3.9.3)
17
+ rspec-expectations (3.9.2)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.9.0)
20
+ rspec-mocks (3.9.1)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.9.0)
23
+ rspec-support (3.9.3)
24
+ rspec_junit_formatter (0.4.1)
25
+ rspec-core (>= 2, < 4, != 2.12.0)
26
+
27
+ PLATFORMS
28
+ ruby
29
+
30
+ DEPENDENCIES
31
+ ckeditor-webhook!
32
+ rake (~> 12.0)
33
+ rspec (~> 3.0)
34
+ rspec_junit_formatter (~> 0.4)
35
+
36
+ BUNDLED WITH
37
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Jack Clayton
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,90 @@
1
+ [![CircleCI](https://circleci.com/gh/jahuty/ckeditor-webhook.svg?style=svg)](https://circleci.com/gh/jahuty/ckeditor-webhook)
2
+
3
+ # Ckeditor Webhook
4
+
5
+ A gem for handling [webhooks](https://ckeditor.com/docs/cs/latest/guides/webhooks/overview.html#webhook-format) from CKEditor Cloud Services.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'ckeditor-webhook'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ ```
18
+ $ bundle install
19
+ ```
20
+
21
+ Or install it yourself as:
22
+
23
+ ```
24
+ $ gem install ckeditor-webhook
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ Call `Ckeditor::Webhook::construct_event` with the following keyword arguments to create a verified webhook event (if the webhook is invalid, a `Ckedior::Webhook::SignatureVerificationError` will be raised):
30
+
31
+ - `secret` (`String`), the CKEditor Cloud Services [API secret](https://ckeditor.com/docs/cs/latest/guides/security/api-secret.html).
32
+ - `payload` (`Hash`), the webhook's payload
33
+ - `signature` (`String`), the request's `X-CS-Signature` header
34
+ - `timestamp` (`Integer`), the request's `X-CS-Timestamp` header
35
+ - `url` (`String`), the request's url
36
+ - `method` (`String`), the request's case-insensitive method (defaults to `"POST"`)
37
+
38
+ For example:
39
+
40
+ ```ruby
41
+ # Store your CKEditor Cloud Services API key safely.
42
+ secret = "SECRET"
43
+
44
+ payload = JSON.parse(request.body.read, symbolize_names: true)
45
+ # => { event: "foo", environment_id: "bar", payload: { baz: "qux" }, sent_at: Time.now.utc }
46
+
47
+ url = request.original_url
48
+ # => "http://demo.example.com/webhook?a=1"
49
+
50
+ signature = request.env['X-CS-Signature']
51
+ # => "880558bfda70c698099ca1184a0c5c5114e5d91cc254d2532470eecf44819b94"
52
+
53
+ timestamp = request.env['X-CS-Timestamp']
54
+ # => 1563276169752
55
+
56
+ begin
57
+ event = CKEditor::Webhook.construct_event(
58
+ secret: secret,
59
+ payload: payload,
60
+ url: url,
61
+ signature: signature,
62
+ timestamp: timestamp
63
+ )
64
+
65
+ event.type # => "foo"
66
+ event.environment_id # => "bar"
67
+ event.payload # => { baz: "qux" }
68
+ event.sent_at # => Time
69
+ rescue Ckeditor::Webhook::SignatureVerificationError => e
70
+ # Reject the webhook! The signature did not match.
71
+ end
72
+ ```
73
+
74
+ ## Development
75
+
76
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
77
+
78
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
79
+
80
+ ## Contributing
81
+
82
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ckeditor-webhook. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/ckeditor-webhook/blob/master/CODE_OF_CONDUCT.md).
83
+
84
+ ## License
85
+
86
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
87
+
88
+ ## Code of Conduct
89
+
90
+ Everyone interacting in the Ckeditor::Webhook project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ckeditor-webhook/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ckeditor/webhook"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,28 @@
1
+ require_relative 'lib/ckeditor/webhook/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "ckeditor-webhook"
5
+ spec.version = Ckeditor::Webhook::VERSION
6
+ spec.authors = ["Jack Clayton"]
7
+ spec.email = ["jack@jahuty.com"]
8
+
9
+ spec.summary = %q{Handle webhooks from CKEditor Cloud Services.}
10
+ spec.homepage = "https://github.com/jahuty/ckeditor-webhook"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/jahuty/ckeditor-webhook"
18
+ spec.metadata["changelog_uri"] = "https://github.com/jahuty/ckeditor-webhook/blob/master/CHANGELOG.md"
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+ end
@@ -0,0 +1,59 @@
1
+ require "ckeditor/webhook/version"
2
+
3
+ require "ckeditor/webhook/event"
4
+
5
+ require "json"
6
+ require "openssl"
7
+ require "uri"
8
+
9
+ module Ckeditor
10
+ module Webhook
11
+ class Error < StandardError; end
12
+ class SignatureVerificationError < StandardError; end
13
+
14
+ class << self
15
+ # Returns an Event if the webhook signature is valid.
16
+ #
17
+ # @param secret [String] the CKEditor Cloud Services API secret
18
+ # @param payload [Hash] the webhook's payload as a hash
19
+ # @param signature [String] the request's `X-CS-Signature` header
20
+ # @param timestamp [Integer] the request's `X-CS-Timestamp` header
21
+ # @param method [String] the request's method (defaults to "POST")
22
+ # @param url [String] the request's url
23
+ #
24
+ # @raise [SignatureVerificationError] if signature's don't match
25
+ #
26
+ # @return [Event]
27
+ #
28
+ def construct_event(secret:, payload:, signature:, timestamp:, url:, method: "POST")
29
+ event = message(method: method, url: url, timestamp: timestamp, payload: payload)
30
+
31
+ raise SignatureVerificationError if signature != message_signature(message: event, secret: secret)
32
+
33
+ Event.new(payload)
34
+ end
35
+
36
+ private
37
+
38
+ # Returns the event's message for signing following CKEditor's rules.
39
+ #
40
+ # @see https://ckeditor.com/docs/cs/latest/examples/security/request-signature-nodejs.html
41
+ def message(method:, url:, timestamp:, payload:)
42
+ uri = URI.parse(url)
43
+ path = "#{uri.path}?#{uri.query}"
44
+ method = method.upcase
45
+ body = payload.to_json
46
+
47
+ "#{method}#{path}#{timestamp}#{body}"
48
+ end
49
+
50
+ def message_signature(message:, secret:)
51
+ ::OpenSSL::HMAC.hexdigest(
52
+ "SHA256",
53
+ secret,
54
+ message
55
+ )
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,39 @@
1
+ require "time"
2
+
3
+ # A generic webhook notification from Ckeditor Cloud Services.
4
+ #
5
+ # @see https://ckeditor.com/docs/cs/latest/guides/webhooks/overview.html
6
+ module Ckeditor
7
+ module Webhook
8
+ class InvalidPayload < StandardError; end
9
+
10
+ class Event
11
+ PROPERTIES = %i[event environment_id payload sent_at]
12
+
13
+ # @raise [InvalidPayload] raised if required properties are missing
14
+ def initialize(payload)
15
+ raise InvalidPayload.new(
16
+ "Expected a hash with :#{PROPERTIES.join(", :")} keys"
17
+ ) unless PROPERTIES.all? { |s| payload.key? s }
18
+
19
+ @payload = payload
20
+ end
21
+
22
+ def environment_id
23
+ @payload[:environment_id]
24
+ end
25
+
26
+ def payload
27
+ @payload[:payload]
28
+ end
29
+
30
+ def sent_at
31
+ ::Time.parse @payload[:sent_at]
32
+ end
33
+
34
+ def type
35
+ @payload[:event]
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,5 @@
1
+ module Ckeditor
2
+ module Webhook
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ckeditor-webhook
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jack Clayton
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-10-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - jack@jahuty.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".circleci/config.yml"
21
+ - ".gitignore"
22
+ - ".rspec"
23
+ - CHANGELOG.md
24
+ - CODE_OF_CONDUCT.md
25
+ - Gemfile
26
+ - Gemfile.lock
27
+ - LICENSE.txt
28
+ - README.md
29
+ - Rakefile
30
+ - bin/console
31
+ - bin/setup
32
+ - ckeditor-webhook.gemspec
33
+ - lib/ckeditor/webhook.rb
34
+ - lib/ckeditor/webhook/event.rb
35
+ - lib/ckeditor/webhook/version.rb
36
+ homepage: https://github.com/jahuty/ckeditor-webhook
37
+ licenses:
38
+ - MIT
39
+ metadata:
40
+ allowed_push_host: https://rubygems.org
41
+ homepage_uri: https://github.com/jahuty/ckeditor-webhook
42
+ source_code_uri: https://github.com/jahuty/ckeditor-webhook
43
+ changelog_uri: https://github.com/jahuty/ckeditor-webhook/blob/master/CHANGELOG.md
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 2.3.0
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.1.4
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Handle webhooks from CKEditor Cloud Services.
63
+ test_files: []