cerber 0.1.2

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: d6ac4737932be54cfd41bd26988d676958d6c23699cdcf1d8605c24eaaf3cb01
4
+ data.tar.gz: 91ea2db3722302c6dc08c5d6679fa49df5bc1c7cea8d4dc5b90931b830fc20c0
5
+ SHA512:
6
+ metadata.gz: 8414d8b2b3531b82e7d4eb414aa0fc73c742dfcdd3263a566a061b226299bd89d777ab3e1a9b30ee7e36449fa5604b757cba4056f681d557ee6163386c781f5d
7
+ data.tar.gz: 6658bcc5f259d5befd6c4b9757c2477a7d4878953bf0cb9caed3376e7a2cc6d92d8414be51d44875da611806dc4a54e78043cec39d6b64e23d838a0e5cbbeeb7
@@ -0,0 +1,36 @@
1
+ name: Run tests and build the Ruby Gem
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - v*
7
+
8
+ jobs:
9
+ build:
10
+ name: Build + Publish
11
+ runs-on: ubuntu-latest
12
+
13
+ steps:
14
+ - uses: actions/checkout@master
15
+ - name: Set up Ruby 2.6
16
+ uses: actions/setup-ruby@v1
17
+ with:
18
+ ruby-version: 2.6.x
19
+
20
+ - name: Run RSpec Tests
21
+ run: |
22
+ gem install bundler
23
+ bundle install --jobs 4 --retry 3
24
+ bundle exec rspec
25
+
26
+ - name: Publish to GPR
27
+ run: |
28
+ mkdir -p $HOME/.gem
29
+ touch $HOME/.gem/credentials
30
+ chmod 0600 $HOME/.gem/credentials
31
+ printf -- "---\n:github: Bearer ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
32
+ gem build *.gemspec
33
+ gem push --KEY github --host https://rubygems.pkg.github.com/${OWNER} *.gem
34
+ env:
35
+ GEM_HOST_API_KEY: ${{secrets.GPR_AUTH_TOKEN}}
36
+ OWNER: username
@@ -0,0 +1,22 @@
1
+ name: Run RSpec Tests
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ build:
7
+
8
+ runs-on: ubuntu-latest
9
+
10
+ steps:
11
+ - uses: actions/checkout@v1
12
+
13
+ - name: Set up Ruby 2.6
14
+ uses: actions/setup-ruby@v1
15
+ with:
16
+ ruby-version: 2.6.x
17
+
18
+ - name: Run RSpec Tests
19
+ run: |
20
+ gem install bundler
21
+ bundle install --jobs 4 --retry 3
22
+ bundle exec rspec
@@ -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,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ before_install: gem install bundler -v 2.0.2
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in cerberus.gemspec
4
+ gemspec
5
+
6
+ group :development, :test do
7
+ gem "rack"
8
+ gem "rack-test"
9
+ gem "rspec-its"
10
+ end
@@ -0,0 +1,46 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cerberus (0.1.2)
5
+ jwt (= 2.1.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.3)
11
+ jwt (2.1.0)
12
+ rack (2.0.7)
13
+ rack-test (1.1.0)
14
+ rack (>= 1.0, < 3)
15
+ rake (10.5.0)
16
+ rspec (3.9.0)
17
+ rspec-core (~> 3.9.0)
18
+ rspec-expectations (~> 3.9.0)
19
+ rspec-mocks (~> 3.9.0)
20
+ rspec-core (3.9.0)
21
+ rspec-support (~> 3.9.0)
22
+ rspec-expectations (3.9.0)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.9.0)
25
+ rspec-its (1.3.0)
26
+ rspec-core (>= 3.0.0)
27
+ rspec-expectations (>= 3.0.0)
28
+ rspec-mocks (3.9.0)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.9.0)
31
+ rspec-support (3.9.0)
32
+
33
+ PLATFORMS
34
+ ruby
35
+
36
+ DEPENDENCIES
37
+ bundler (~> 2.0)
38
+ cerberus!
39
+ rack
40
+ rack-test
41
+ rake (~> 10.0)
42
+ rspec (~> 3.0)
43
+ rspec-its
44
+
45
+ BUNDLED WITH
46
+ 2.0.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 krzyczak
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
+ # Cerberus
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cerberus`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ ![](https://github.com/krzyczak/cerberus/workflows/Run%20RSpec%20Tests/badge.svg)
6
+ ![](https://github.com/krzyczak/cerberus/workflows/Run%20tests%20and%20build%20the%20Ruby%20Gem/badge.svg)
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem "cerberus"
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install cerberus
23
+
24
+ ## Usage
25
+
26
+ Configure Cerberus with required information:
27
+
28
+ ```ruby
29
+ Cerberus.configure do |config|
30
+ config.jwt = {
31
+ rsa_public: ENV["JWT_RSA_PUBLIC"],
32
+ algorithm: "RS256",
33
+ issuer: "org.website.project",
34
+ enabled: ->(env) { Rails.env.production? }
35
+ }.freeze
36
+
37
+ config.basic = {
38
+ username_digest: ::Digest::SHA256.hexdigest("test"),
39
+ password_digest: ::Digest::SHA256.hexdigest("test"),
40
+ enabled: ->(env) { env["PATH_INFO"].start_with?("/protected") }
41
+ }.freeze
42
+ end
43
+
44
+ require "cerberus/jwt"
45
+ require "cerberus/basic"
46
+ ```
47
+
48
+ And then just inject a middleware:
49
+
50
+ ```ruby
51
+ config.middleware.use Cerberus::Jwt
52
+ config.middleware.use Cerberus::Basic
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]/cerberus.
64
+
65
+ ## License
66
+
67
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -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 "cerberus"
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,36 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "cerberus/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "cerber"
7
+ spec.version = Cerberus::VERSION
8
+ spec.authors = ["krzyczak"]
9
+ spec.email = ["krzyczak@users.noreply.github.com"]
10
+
11
+ spec.summary = %q{Various authentication rack middlewares}
12
+ spec.description = %q{A collection of easily plugable rack authentication middlewares}
13
+ spec.homepage = "https://github.com/krzyczak/cerberus"
14
+ spec.license = "MIT"
15
+
16
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/krzyczak/cerberus"
20
+ spec.metadata["changelog_uri"] = "https://github.com/krzyczak/cerberus/CHANGELOG.md"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_dependency "jwt", "= 2.1.0"
32
+
33
+ spec.add_development_dependency "bundler", "~> 2.0"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.0"
36
+ end
@@ -0,0 +1,29 @@
1
+ require "ostruct"
2
+ require "jwt"
3
+ # Bundler.require(:default, :development)
4
+ require "cerberus/version"
5
+
6
+ module Cerberus
7
+ class Error < StandardError; end
8
+ # Your code goes here...
9
+
10
+ class << self
11
+ attr_accessor :config
12
+ end
13
+
14
+ def self.configure
15
+ self.config ||= Config.new
16
+ yield(config)
17
+ self.config.jwt = OpenStruct.new(self.config.jwt || {})
18
+ self.config.jwt.skip_middleware_unless ||= -> { false }
19
+
20
+ self.config.basic = OpenStruct.new(self.config.basic || {})
21
+ self.config.basic.enabled ||= -> { true }
22
+ end
23
+
24
+ class Config
25
+ attr_accessor(:jwt, :basic)
26
+ end
27
+
28
+ self.configure {}
29
+ end
@@ -0,0 +1,31 @@
1
+ require "cerberus"
2
+
3
+ module Cerberus
4
+ class Basic
5
+ def initialize(app)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ if Cerberus.config.basic.enabled.call(env)
11
+ auth = Rack::Auth::Basic.new(@app) do |username, password|
12
+ username_digest = Cerberus.config.basic.username_digest
13
+ password_digest = Cerberus.config.basic.password_digest
14
+
15
+ eql?(username, username_digest) & eql?(password, password_digest)
16
+ end
17
+
18
+ auth.call(env)
19
+ else
20
+ @app.call(env)
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def eql?(value, comparable)
27
+ Rack::Utils.secure_compare(::Digest::SHA256.hexdigest(value), comparable)
28
+ end
29
+ end
30
+ end
31
+
@@ -0,0 +1,66 @@
1
+ require "cerberus"
2
+
3
+ module Cerberus
4
+ class Jwt
5
+ JWT_OPTIONS = {
6
+ algorithm: Cerberus.config.jwt.algorithm || ENV["JWT_ALGORITHM"],
7
+ iss: Cerberus.config.jwt.issuer || ENV["JWT_ISSUER"],
8
+ verify_iss: true,
9
+ verify_iat: true
10
+ }.freeze
11
+
12
+ JWT_ERRORS = {
13
+ JWT::ExpiredSignature => {
14
+ status: 403, body: "The token has expired."
15
+ },
16
+ JWT::InvalidIssuerError => {
17
+ status: 403, body: "The token does not have a valid issuer."
18
+ },
19
+ JWT::InvalidIatError => {
20
+ status: 403, body: "The token does not have a valid 'issued at' time."
21
+ },
22
+ JWT::DecodeError => {
23
+ status: 401, body: "A valid token must be passed."
24
+ },
25
+ KeyError => {
26
+ status: 401, body: "HTTP_AUTHORIZATION header must be present."
27
+ }
28
+ }.freeze
29
+
30
+ class EmptyJWT < StandardError
31
+ DEFAULT_ERROR_MESSAGE = "Cerberus CONFIG: config.jwt.rsa_public option needs to be set."
32
+
33
+ def initialize(error_message = DEFAULT_ERROR_MESSAGE)
34
+ super(error_message)
35
+ end
36
+ end
37
+
38
+ raise EmptyJWT("Cerberus CONFIG: config.jwt is empty.") if Cerberus.config.jwt.empty?
39
+ raise EmptyJWT if Cerberus.config.jwt.rsa_public.to_s.strip.empty?
40
+
41
+ JWT_RSA_PUBLIC = OpenSSL::PKey::RSA.new(Base64.decode64(Cerberus.config.jwt.rsa_public))
42
+
43
+ def initialize(app)
44
+ @app = app
45
+ end
46
+
47
+ def call(env)
48
+ if Cerberus.config.jwt.enabled.call(env)
49
+ auth_token = env.fetch("HTTP_AUTHORIZATION").gsub(/bearer /i, "")
50
+ _payload, _header = JWT.decode(auth_token, JWT_RSA_PUBLIC, true, JWT_OPTIONS)
51
+ end
52
+
53
+ @app.call(env)
54
+ rescue StandardError => error
55
+ if JWT_ERRORS.keys.include?(error.class)
56
+ self.class.respond_with(*JWT_ERRORS.fetch(error.class).values)
57
+ else
58
+ raise error
59
+ end
60
+ end
61
+
62
+ def self.respond_with(status_code, message)
63
+ [status_code, { "Content-Type" => "text/plain" }, [message]]
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,3 @@
1
+ module Cerberus
2
+ VERSION = "0.1.2"
3
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cerber
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - krzyczak
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-10-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jwt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 2.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: A collection of easily plugable rack authentication middlewares
70
+ email:
71
+ - krzyczak@users.noreply.github.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".github/workflows/gempush.yml"
77
+ - ".github/workflows/test.yml"
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".travis.yml"
81
+ - Gemfile
82
+ - Gemfile.lock
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - bin/console
87
+ - bin/setup
88
+ - cerberus.gemspec
89
+ - lib/cerberus.rb
90
+ - lib/cerberus/basic.rb
91
+ - lib/cerberus/jwt.rb
92
+ - lib/cerberus/version.rb
93
+ homepage: https://github.com/krzyczak/cerberus
94
+ licenses:
95
+ - MIT
96
+ metadata:
97
+ homepage_uri: https://github.com/krzyczak/cerberus
98
+ source_code_uri: https://github.com/krzyczak/cerberus
99
+ changelog_uri: https://github.com/krzyczak/cerberus/CHANGELOG.md
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubygems_version: 3.0.4
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Various authentication rack middlewares
119
+ test_files: []