garage-jwt 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d2f84ca42c260e1f8b86567cab7ccf79e5c25358
4
+ data.tar.gz: 79637ad570d302e411fe5ea58f6a29df910211cb
5
+ SHA512:
6
+ metadata.gz: d9489f57ec2759f93140a82e13e1da6e122d43e61be4d20aaa9ab239f42b7040833c479168eefab73a1152c1e7f3e3367eabba6f4ed1e63cb1a8d9b3b93dd54a
7
+ data.tar.gz: daac7867ea8900bd68d7f685ba4dcb9a55b09df1af5579ffabd5601a677bc947ca406ead6ac0976276faa751ce855aa919ef3dd12e01e58dd9ab656ee1414ba8
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /vendor/bundle
11
+ /spec/dummy/db/*.sqlite3
12
+ /spec/dummy/log
13
+ /spec/dummy/tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
6
+ addons:
7
+ code_climate:
8
+ repo_token: 4cbf96c5cce0c4405306632c7c53221e7b743d4562a97632419557eb7c61606a
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at masayuki@izumin.info. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,24 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem "kaminari"
6
+
7
+ group :development, :test do
8
+ gem "database_rewinder"
9
+ gem "factory_girl_rails"
10
+ gem "sqlite3"
11
+ gem "rspec-rails"
12
+ end
13
+
14
+ group :test do
15
+ gem "rspec-its"
16
+ gem "rspec-parameterized"
17
+ gem "rspec-request_describer"
18
+ gem "timecop"
19
+ gem "ffaker"
20
+
21
+ gem "codeclimate-test-reporter", require: nil
22
+ end
23
+
24
+ gem 'byebug', group: [:development, :test]
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 izumin5210
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,85 @@
1
+ # Garage::Jwt
2
+ [![Build Status](https://travis-ci.org/izumin5210/garage-jwt.svg?branch=master)](https://travis-ci.org/izumin5210/garage-jwt)
3
+ [![Test Coverage](https://codeclimate.com/github/izumin5210/garage-jwt/badges/coverage.svg)](https://codeclimate.com/github/izumin5210/garage-jwt/coverage)
4
+ [![Code Climate](https://codeclimate.com/github/izumin5210/garage-jwt/badges/gpa.svg)](https://codeclimate.com/github/izumin5210/garage-jwt)
5
+
6
+ Garage extension to use JWT as authentication strategy.
7
+
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'garage-jwt'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install garage-jwt
24
+
25
+ ## Usage
26
+
27
+ In `config/initializer/garage.rb`:
28
+
29
+ ```ruby
30
+ Garage.configuration.strategy = Garage::Strategy::Jwt
31
+
32
+ Garage::Jwt.configure do |c|
33
+ c.algorithm = Garage::Jwt::Algorithm.hs256
34
+ c.common_key = "secret key"
35
+
36
+ # c.private_key = OpenSSL::PKey::RSA.generate 2048
37
+ # c.public_key = c.private_key.public_key
38
+ end
39
+ ```
40
+
41
+ The following cryptographic signing algorithms are available:
42
+
43
+ - NONE
44
+ - `Garage::Jwt::Algorithm.none`
45
+ - HMAC
46
+ - `Garage::Jwt::Algorithm.hs256` - HMAC using SHA-256 hash algorithm
47
+ - `Garage::Jwt::Algorithm.hs384` - HMAC using SHA-384 hash algorithm
48
+ - `Garage::Jwt::Algorithm.hs512` - HMAC using SHA-512 hash algorithm
49
+ - RSA
50
+ - `Garage::Jwt::Algorithm.rs256` - RSA using SHA-256 hash algorithm
51
+ - `Garage::Jwt::Algorithm.rs384` - RSA using SHA-384 hash algorithm
52
+ - `Garage::Jwt::Algorithm.rs512` - RSA using SHA-512 hash algorithm
53
+ - ECDSA
54
+ - `Garage::Jwt::Algorithm.rs256` - ECDSA using SHA-256 hash algorithm
55
+ - `Garage::Jwt::Algorithm.rs384` - ECDSA using SHA-384 hash algorithm
56
+ - `Garage::Jwt::Algorithm.rs512` - ECDSA using SHA-512 hash algorithm
57
+
58
+
59
+ ### Generate token
60
+
61
+ ```ruby
62
+ Garage::Jwt::Utils.encode(
63
+ resource_owner_id: user.di,
64
+ application_id: 128,
65
+ scope: "read write",
66
+ expired_at: Time.zone.now + 15.minutes
67
+ )
68
+ ```
69
+
70
+
71
+ ## Development
72
+
73
+ 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.
74
+
75
+ 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).
76
+
77
+ ## Contributing
78
+
79
+ Bug reports and pull requests are welcome on GitHub at https://github.com/izumin5210/garage-jwt. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
80
+
81
+
82
+ ## License
83
+
84
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
85
+
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Garage::Jwt'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'rspec/core/rake_task'
18
+
19
+ RSpec::Core::RakeTask.new(:spec)
20
+ task :default => :spec
21
+
22
+ require 'bundler/gem_tasks'
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "garage/jwt"
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
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,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'garage/jwt/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "garage-jwt"
8
+ spec.version = Garage::Jwt::VERSION
9
+ spec.authors = ["izumin5210"]
10
+ spec.email = ["masayuki@izumin.info"]
11
+
12
+ spec.summary = %q{Garage extension to use JWT as authentication strategy.}
13
+ spec.description = spec.summary
14
+ spec.homepage = "https://github.com/izumin5210/garage-jwt.git"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_runtime_dependency "jwt", "~> 1.0"
23
+ spec.add_runtime_dependency "the_garage", "~> 2.0"
24
+ spec.add_development_dependency "bundler", "~> 1.12"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ end
data/lib/garage/jwt.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "jwt"
2
+ require "garage"
3
+
4
+ require "garage/jwt/version"
5
+ require "garage/jwt/algorithm"
6
+ require "garage/jwt/config"
7
+ require "garage/jwt/error"
8
+ require "garage/jwt/utils"
9
+ require "garage/strategy/jwt"
@@ -0,0 +1,61 @@
1
+ module Garage
2
+ module Jwt
3
+ class Algorithm
4
+ # https://github.com/jwt/ruby-jwt#algorithms-and-usage
5
+ TYPES = {
6
+ # NONE
7
+ none: "none",
8
+
9
+ # HMAC
10
+ hs256: "HS256",
11
+ hs384: "HS384",
12
+ hs512: "HS512",
13
+
14
+ # RSA
15
+ rs256: "RS256",
16
+ rs384: "RS384",
17
+ rs512: "RS512",
18
+
19
+ # ECDSA
20
+ es256: "ES256",
21
+ es384: "ES384",
22
+ es512: "ES512",
23
+ }.freeze
24
+
25
+ class << self
26
+
27
+ TYPES.each do |key, _|
28
+ define_method key do
29
+ Algorithm.new(key)
30
+ end
31
+ end
32
+ end
33
+
34
+ def need_common_key?
35
+ %i(hs256 hs384 hs512).include?(@type_key)
36
+ end
37
+
38
+ def need_public_key?
39
+ %i(rs256 rs384 rs512 es256 es384 es512).include?(@type_key)
40
+ end
41
+
42
+ def need_private_key?
43
+ need_public_key?
44
+ end
45
+
46
+ def none?
47
+ @type_key == :none
48
+ end
49
+
50
+ def type
51
+ TYPES[@type_key]
52
+ end
53
+
54
+ private
55
+
56
+ def initialize(type_key)
57
+ @type_key = type_key
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,40 @@
1
+ module Garage
2
+ module Jwt
3
+ def self.configure(&block)
4
+ @config = Config::Builder.new(&block).build
5
+ end
6
+
7
+ def self.configuration
8
+ @config ||= configure {}
9
+ end
10
+
11
+ class Config
12
+ attr_accessor :common_key, :public_key, :private_key, :algorithm
13
+
14
+ class Builder
15
+ def initialize(&block)
16
+ @config = Config.new
17
+ block.call(@config)
18
+ end
19
+
20
+ def valid?
21
+ @config.algorithm.present? &&
22
+ @config.algorithm.is_a?(Garage::Jwt::Algorithm) &&
23
+ (
24
+ (!@config.algorithm.need_common_key? || @config.common_key.present?) &&
25
+ (!@config.algorithm.need_public_key? || @config.public_key.present?) &&
26
+ (!@config.algorithm.need_private_key? || @config.private_key.present?)
27
+ )
28
+ end
29
+
30
+ def build
31
+ if valid?
32
+ @config
33
+ else
34
+ fail Garage::Jwt::InitializeError
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,5 @@
1
+ module Garage
2
+ module Jwt
3
+ class InitializeError < StandardError; end
4
+ end
5
+ end
@@ -0,0 +1,61 @@
1
+ module Garage
2
+ module Jwt
3
+ module Utils
4
+ class << self
5
+ def encode(resource_owner_id:, application_id:, expired_at:, scope:)
6
+ payload = {
7
+ sub: resource_owner_id,
8
+ aud: application_id,
9
+ exp: expired_at.to_i,
10
+ scope: (scope.is_a?(Array) ? scope.join(" ") : scope)
11
+ }
12
+ JWT.encode(payload, private_key, algorithm.type)
13
+ end
14
+
15
+ def decode(token, token_type)
16
+ payload, _ = JWT.decode(token, public_key, verify?, decoding_options)
17
+ { token: token,
18
+ token_type: token_type,
19
+ scope: payload["scope"],
20
+ application_id: payload["aud"],
21
+ resource_owner_id: payload["sub"],
22
+ expired_at: payload["exp"],
23
+ revoked_at: nil
24
+ }
25
+ rescue JWT::DecodeError => e
26
+ nil
27
+ end
28
+
29
+ private
30
+
31
+ def configuration
32
+ Garage::Jwt.configuration
33
+ end
34
+
35
+ def algorithm
36
+ configuration.algorithm
37
+ end
38
+
39
+ def public_key
40
+ algorithm.need_public_key? ? configuration.public_key : common_key
41
+ end
42
+
43
+ def private_key
44
+ algorithm.need_public_key? ? configuration.private_key : common_key
45
+ end
46
+
47
+ def common_key
48
+ algorithm.need_common_key? ? configuration.common_key : nil
49
+ end
50
+
51
+ def decoding_options
52
+ { algorithm: algorithm.type, verify_expiration: false }
53
+ end
54
+
55
+ def verify?
56
+ !algorithm.none?
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,5 @@
1
+ module Garage
2
+ module Jwt
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+ module Garage
2
+ module Strategy
3
+ module Jwt
4
+ extend ActiveSupport::Concern
5
+ included do
6
+ before_action :verify_auth, if: -> (_) { verify_permission? }
7
+ end
8
+
9
+ def verify_permission?
10
+ true
11
+ end
12
+
13
+ def access_token
14
+ if defined?(@access_token)
15
+ @access_token
16
+ else
17
+ token_type, token = request.authorization.try { |h| h.split(/\s+/) }
18
+ decoded_token = Garage::Jwt::Utils.decode(token, token_type)
19
+ if decoded_token.present?
20
+ @access_token = Garage::Strategy::AccessToken.new(decoded_token)
21
+ else
22
+ nil
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: garage-jwt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - izumin5210
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-10-02 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: '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: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: the_garage
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :runtime
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: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description: Garage extension to use JWT as authentication strategy.
70
+ email:
71
+ - masayuki@izumin.info
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - garage-jwt.gemspec
87
+ - lib/garage/jwt.rb
88
+ - lib/garage/jwt/algorithm.rb
89
+ - lib/garage/jwt/config.rb
90
+ - lib/garage/jwt/error.rb
91
+ - lib/garage/jwt/utils.rb
92
+ - lib/garage/jwt/version.rb
93
+ - lib/garage/strategy/jwt.rb
94
+ homepage: https://github.com/izumin5210/garage-jwt.git
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.5.1
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Garage extension to use JWT as authentication strategy.
118
+ test_files: []