openstax_auth 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c94d632ecd0b4eb33521d4ea151a1e4ad441904d27b36da6da1c152fe8d5ef68
4
+ data.tar.gz: 9c59c1264d2fc58800a6dfe8ea6d65d3ecf32467466be038a61719f1e5711243
5
+ SHA512:
6
+ metadata.gz: ea52d1b0e469d251152aa0fac49df44d666a073f59d1b15eee9570d7fe9126da4e014578c5a07643fe52156e3a2f9365875e28a119ee7e3dbdc0aa947acc2567
7
+ data.tar.gz: 594a4c58eb9ba9ddf08253929b18ad493925d19462e380e5d57168b364cf803a5f88f81866bb9c7219baf3c4b3b6bff48565429057e43548803eb56a92c94ed8
@@ -0,0 +1,31 @@
1
+ name: Tests
2
+
3
+ on:
4
+ pull_request:
5
+ push:
6
+ branches:
7
+ - main
8
+
9
+ jobs:
10
+ tests:
11
+ timeout-minutes: 10
12
+ runs-on: ubuntu-18.04
13
+
14
+ steps:
15
+ - uses: actions/checkout@v2
16
+ - uses: actions/setup-ruby@v1
17
+ with:
18
+ ruby-version: 2.7
19
+ - uses: actions/cache@v2
20
+ with:
21
+ path: vendor/bundle
22
+ key: ${{ runner.os }}-gems-pr-${{ hashFiles('**/Gemfile.lock') }}
23
+ restore-keys: |
24
+ ${{ runner.os }}-gems-pr-
25
+ - name: Test
26
+ run: |
27
+ gem install bundler --force --no-document --version 2.1.4
28
+ bundle config path vendor/bundle
29
+ bundle config jobs 2
30
+ bundle install
31
+ bundle exec rspec
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ .bundle/
2
+ log/*.log
3
+ pkg/
4
+ *.gem
5
+ Gemfile.lock
6
+ spec/dummy/log/*.log
7
+ spec/dummy/tmp/
8
+ spec/dummy/db/
9
+ spec/dummy/.sass-cache
10
+ .byebug_history
11
+ .DS_Store
12
+ .rspec_status
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in openstax_auth.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Rice University
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.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # openstax_auth
2
+
3
+ **This is a fork of openstax/auth-rails that eliminates the ActiveSupport dependency as well as the no-longer-used "strategy 1".**
4
+
5
+ Provides utilities to get user information from cookies within OpenStax rack-based apps.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'openstax_auth', git: 'https://github.com/openstax/auth-ruby.git', ref: 'some_commit_sha_here'
13
+ ```
14
+
15
+ (this gem is not published to rubygems)
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ ## Usage
22
+
23
+ This gem contains strategies for extracting user information from cookies. As OpenStax authentication cookies evolve over time, new strategies will be added and old ones will no longer be used.
24
+
25
+ ### Strategy 2
26
+
27
+ The only strategy in this gem.
28
+
29
+ Configure the `strategy_2` settings:
30
+
31
+ ```ruby
32
+ OpenStax::Auth.configure do |config|
33
+ config.strategy2.signature_public_key = "blah"
34
+ config.strategy2.encryption_private_key = "blah"
35
+ config.strategy2.cookie_name = "blah"
36
+ config.strategy2.encryption_algorithm = 'dir'
37
+ config.strategy2.encryption_method = 'A256GCM'
38
+ config.strategy2.signature_algorithm = 'RS256'
39
+ end
40
+ ```
41
+
42
+ And then `require 'openstax/auth/strategy_2'` in the code that needs it.
43
+
44
+ ## Development
45
+
46
+ 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.
47
+
48
+ 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).
49
+
50
+ ## License
51
+
52
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "openstax_auth"
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__)
data/bin/setup ADDED
@@ -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,56 @@
1
+ module OpenStax
2
+ module Auth
3
+ class Configuration
4
+ attr_reader :strategy2
5
+
6
+ class ConfigFields
7
+ def self.attr_accessor(*vars, required: false)
8
+ super(*vars)
9
+ vars.each do |v|
10
+ define_singleton_method(v) do
11
+ val = instance_variable_get "@#{v.to_s}".to_sym
12
+ raise "#{v} not set" if (val.nil? || val == "") && required
13
+ val
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ class Strategy2 < ConfigFields
20
+ attr_accessor :signature_public_key, required: true
21
+ attr_accessor :encryption_private_key, required: true
22
+ attr_accessor :cookie_name, required: true
23
+ attr_accessor :encryption_algorithm, required: true
24
+ attr_accessor :encryption_method, required: true
25
+ attr_accessor :signature_algorithm, required: true
26
+ end
27
+
28
+ def initialize
29
+ @strategy2 = Strategy2.new
30
+ end
31
+ end
32
+
33
+ class << self
34
+ ###########################################################################
35
+ #
36
+ # Configuration machinery.
37
+ #
38
+ # To configure OpenStax Auth, put the following code in your
39
+ # application's initialization logic
40
+ # (eg. in the config/initializers in a Rails app)
41
+ #
42
+ # OpenStax::Auth.configure do |config|
43
+ # config.<parameter name> = <parameter value>
44
+ # ...
45
+ # end
46
+ #
47
+ def configure
48
+ yield configuration
49
+ end
50
+
51
+ def configuration
52
+ @configuration ||= Configuration.new
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,68 @@
1
+ require 'json/jwt'
2
+ require 'forwardable'
3
+
4
+ module OpenStax
5
+ module Auth
6
+ module Strategy2
7
+ extend Forwardable
8
+ extend self
9
+
10
+ def_delegators :configuration,
11
+ :cookie_name,
12
+ :signature_public_key,
13
+ :signature_algorithm,
14
+ :encryption_private_key,
15
+ :encryption_algorithm,
16
+ :encryption_method
17
+
18
+ def user_uuid(request)
19
+ (decrypt(request) || {}).dig('sub', 'uuid')
20
+ end
21
+
22
+ def decrypt(request)
23
+ auth_header = request.headers['Authorization'] || ''
24
+ cookie = auth_header.start_with?('Bearer ') ?
25
+ auth_header.sub('Bearer ', '') : request.cookies[cookie_name]
26
+ return if cookie.nil? || cookie.empty?
27
+
28
+ begin
29
+ # Decoding is the reverse of what accounts does to encode a cookie:
30
+ # accounts first asymmetric encodes the cookie w/ the signature private
31
+ # key, then it next symmetric encodes that result w/ the encryption
32
+ # private key.
33
+
34
+ # Step 1: symmetric decode the cookie with the encryption private key
35
+ encrypted = JSON::JWT.decode(
36
+ cookie,
37
+ rsa_encryption_private_key,
38
+ encryption_algorithm.to_s,
39
+ encryption_method.to_s
40
+ ).plain_text
41
+
42
+ # Step 2: asymmetric decode the previous step with the signature public key
43
+ JSON::JWT.decode(
44
+ encrypted,
45
+ rsa_signature_public_key,
46
+ signature_algorithm.to_sym
47
+ )
48
+ rescue JSON::JWT::Exception, OpenSSL::Cipher::CipherError
49
+ nil
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ def configuration
56
+ OpenStax::Auth.configuration.strategy2
57
+ end
58
+
59
+ def rsa_signature_public_key
60
+ OpenSSL::PKey::RSA.new signature_public_key
61
+ end
62
+
63
+ def rsa_encryption_private_key
64
+ OpenSSL::PKey::RSA.new encryption_private_key rescue encryption_private_key
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,5 @@
1
+ module OpenStax
2
+ module Auth
3
+ VERSION = "0.2.0"
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ require "openstax/auth/version"
2
+ require "openstax/auth/configuration"
3
+
4
+ module OpenStax
5
+ module Auth
6
+ end
7
+ end
@@ -0,0 +1,33 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "openstax/auth/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "openstax_auth"
7
+ spec.version = OpenStax::Auth::VERSION
8
+ spec.authors = ["JP Slavinsky"]
9
+ spec.email = ["jps@rice.edu"]
10
+
11
+ spec.summary = %q{Provides utilities to get user information from cookies within OpenStax Ruby apps.}
12
+ spec.description = %q{Provides utilities to get user information from cookies within OpenStax Ruby apps.}
13
+ spec.homepage = "https://github.com/openstax/auth-ruby"
14
+ spec.license = "MIT"
15
+
16
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
17
+
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/openstax/auth-ruby"
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.require_paths = ["lib"]
27
+
28
+ spec.add_dependency "json-jwt"
29
+
30
+ spec.add_development_dependency "bundler", "~> 2.0"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec", "~> 3.0"
33
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: openstax_auth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - JP Slavinsky
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-08-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json-jwt
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '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: Provides utilities to get user information from cookies within OpenStax
70
+ Ruby apps.
71
+ email:
72
+ - jps@rice.edu
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".github/workflows/tests.yml"
78
+ - ".gitignore"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - lib/openstax/auth/configuration.rb
86
+ - lib/openstax/auth/strategy_2.rb
87
+ - lib/openstax/auth/version.rb
88
+ - lib/openstax_auth.rb
89
+ - openstax_auth.gemspec
90
+ homepage: https://github.com/openstax/auth-ruby
91
+ licenses:
92
+ - MIT
93
+ metadata:
94
+ allowed_push_host: https://rubygems.org
95
+ homepage_uri: https://github.com/openstax/auth-ruby
96
+ source_code_uri: https://github.com/openstax/auth-ruby
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubygems_version: 3.1.4
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Provides utilities to get user information from cookies within OpenStax Ruby
116
+ apps.
117
+ test_files: []