rails-param_cryptable 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ef8bfbf13fe2db87fdcb77a0a5da3c8bb8bb00156b3d652790a6b83b61835fef
4
+ data.tar.gz: e9b2a82072fdd71409c5f02282e390dba194a6a0beaa860da98cbfb1d07e5226
5
+ SHA512:
6
+ metadata.gz: 641eb57f5f2108cfad5ad5f03685559f0f0bd8667975b7ee37904aeb7b4a71505edba9775617ed472b484e7be0a94687a79697ee9387b10fade61882a1493ffe
7
+ data.tar.gz: 07b8d56c7e0b2d5b31209936d684679646ef1c65be5c084821007d62a20554fcaaa5a428db1d7d78de6bd7530971c25817b4b53df09dfe15149f368e2b9ffeac
@@ -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,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.2
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,7 @@
1
+ # Changelog
2
+
3
+ ## Master
4
+
5
+ ## v0.1.0
6
+
7
+ - 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 hi@pelle.codes. 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,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rails-param_cryptable.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Pelle ten Cate
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,67 @@
1
+ # Rails::ParamCryptable
2
+
3
+ A simple gem for Rails >= 5.2, that can encrypt URL parameters, intentionally meant to help you obfuscate
4
+ ordinary IDs without having to rely on a separate database-stored secret token.
5
+
6
+ Turns your
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'rails-param_cryptable'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle install
19
+
20
+ ## Usage
21
+
22
+ In this example, we will encrypt the ID of our OrdersController, turning **example.com/orders/123** into **example.com/orders/203e39202d326d13**
23
+
24
+ In your credentials (`rails credentials:edit --environment=production`), add a 16-bytes long value for param_cryptable_key.
25
+
26
+ ```ruby
27
+ param_cryptable_key: CTYcQtYN3ELVM5Lu
28
+ ```
29
+
30
+ In your controller:
31
+
32
+ ```ruby
33
+ class OrdersController < ApplicationController
34
+ include ParamCryptable
35
+
36
+ crypt_param :id
37
+ ```
38
+
39
+ This will automagically expect the `:id` param to be encrypted, and decrypt it for you.
40
+
41
+ And wherever you want to create an URL:
42
+
43
+ ```ruby
44
+ # From within the controller
45
+ orders_path(encrypt(@order.id))
46
+
47
+ # From everywhere else
48
+ module OrdersHelper
49
+ def my_link_href_helper
50
+ order_path(ParamCryptable.encrypt(@order.id))
51
+ end
52
+ end
53
+ ```
54
+
55
+ ## Development
56
+
57
+ 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.
58
+
59
+ 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).
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rails-param_cryptable. 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]/rails-param_cryptable/blob/master/CODE_OF_CONDUCT.md).
64
+
65
+ ## License
66
+
67
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).s
@@ -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 "rails/param_cryptable"
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,86 @@
1
+ require "param_cryptable/version"
2
+
3
+ module ParamCryptable
4
+ extend ActiveSupport::Concern
5
+
6
+ class Error < StandardError; end
7
+
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ delegate :crypt_params, to: :class
12
+ end
13
+
14
+ # Encrypt any value for use by crypt_params
15
+ #
16
+ # @param [Object] text The text to encrypt. This method calls `.to_s` on it.
17
+ # @return [String] The encrypted representation
18
+ def self.encrypt(text)
19
+ cipher = OpenSSL::Cipher.new('blowfish').encrypt
20
+ cipher.key = Rails.application.credentials.param_cryptable_key
21
+
22
+ s = cipher.update(text.to_s) + cipher.final
23
+ s.unpack1('H*')
24
+ end
25
+
26
+ class_methods do
27
+ # attr_accessor really needs a default / initial value setting...
28
+ #
29
+ # @return [Array] Crypt Params stored in class instance variable.
30
+ def crypt_params
31
+ @crypt_params ||= []
32
+ end
33
+
34
+ # Expect this param to be encrypted, if present, and attempt to decrypt it before accessing
35
+ #
36
+ # @param [Symbol, Hash] param The parameter to expect to be crypted
37
+ # @param [Array] tree Internally used for recursion
38
+ def crypt_param(param, tree = [])
39
+ if param.is_a?(Hash)
40
+ key, value = param.first
41
+ crypt_param(value, tree + [key])
42
+ else
43
+ crypt_params << tree + [param]
44
+ end
45
+ end
46
+ end
47
+
48
+ # Encrypt any value for use by crypt_params
49
+ #
50
+ # @param [Object] text The text to encrypt. This method calls `.to_s` on it.
51
+ # @return [String] The encrypted representation
52
+ def encrypt(text)
53
+ ParamCryptable.encrypt(text)
54
+ end
55
+
56
+ private
57
+
58
+ # Override for ActionController#params that decrypts all registered crypt_params if they are
59
+ # provided as strings in the request.
60
+ #
61
+ # @return [ActionController::Parameters]
62
+ def params
63
+ @params ||= super.tap do |params|
64
+ crypt_params.each do |crypt_param|
65
+ local_params =
66
+ if crypt_param.size > 1
67
+ params.dig(*crypt_param[0..-2])
68
+ else
69
+ params
70
+ end
71
+ if local_params[crypt_param.last].is_a? String
72
+ local_params[crypt_param.last] = decrypt(local_params[crypt_param.last])
73
+ end
74
+ end
75
+ end
76
+ end
77
+
78
+ def decrypt(code)
79
+ cipher = OpenSSL::Cipher.new('blowfish').decrypt
80
+ cipher.key = Rails.application.credentials.param_cryptable_key
81
+ s = [code].pack('H*').unpack('C*').pack('c*')
82
+
83
+ cipher.update(s) + cipher.final
84
+ end
85
+
86
+ end
@@ -0,0 +1,3 @@
1
+ module ParamCryptable
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,29 @@
1
+ require_relative 'lib/param_cryptable/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "rails-param_cryptable"
5
+ spec.version = ParamCryptable::VERSION
6
+ spec.authors = ["Pelle ten Cate"]
7
+ spec.email = ["hi@pelle.codes"]
8
+
9
+ spec.summary = %q{Encrypted URL parameters for Rails}
10
+ spec.description = %q{Simple encryption for URL parameters in rails, meant as a drop-in obfuscation layer for IDs}
11
+ spec.homepage = "https://github.com/pelletencate/rails-param_cryptable"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = spec.homepage
17
+ spec.metadata["changelog_uri"] = "https://github.com/pelletencate/rails-param_cryptable/blob/main/CHANGELOG.md"
18
+
19
+ spec.add_dependency 'rails', '>= 5.2.0'
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-param_cryptable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pelle ten Cate
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-01-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.0
27
+ description: Simple encryption for URL parameters in rails, meant as a drop-in obfuscation
28
+ layer for IDs
29
+ email:
30
+ - hi@pelle.codes
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".gitignore"
36
+ - ".rspec"
37
+ - ".travis.yml"
38
+ - CHANGELOG.md
39
+ - CODE_OF_CONDUCT.md
40
+ - Gemfile
41
+ - LICENSE.txt
42
+ - README.md
43
+ - Rakefile
44
+ - bin/console
45
+ - bin/setup
46
+ - lib/param_cryptable.rb
47
+ - lib/param_cryptable/version.rb
48
+ - rails-param_cryptable.gemspec
49
+ homepage: https://github.com/pelletencate/rails-param_cryptable
50
+ licenses:
51
+ - MIT
52
+ metadata:
53
+ homepage_uri: https://github.com/pelletencate/rails-param_cryptable
54
+ source_code_uri: https://github.com/pelletencate/rails-param_cryptable
55
+ changelog_uri: https://github.com/pelletencate/rails-param_cryptable/blob/main/CHANGELOG.md
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 2.3.0
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubygems_version: 3.1.4
72
+ signing_key:
73
+ specification_version: 4
74
+ summary: Encrypted URL parameters for Rails
75
+ test_files: []