compeon-access_token 0.2.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
+ SHA256:
3
+ metadata.gz: faae0c48728f4b0d7eb662200b67810cc8ebd96dc6966e1facf87382250fa224
4
+ data.tar.gz: a598675a7e638cb3d0dfc40e2f22fecd172699d0ab4c49fffd3a06e95e4ddb54
5
+ SHA512:
6
+ metadata.gz: cec38a83ded44a217f7c399e24ea8a64de86bb1cbd5d54a65786dbb739d92fc8e5d24e3f8ff29a67bace1f4f71689dcd00a4625abc9d66add6343b3db4fb1306
7
+ data.tar.gz: c7243383dd55edd5267b21a11b40357c24612411abd2b1a3014f711c52bd7cf605f68adb4d3b7b0959c1b96ed5918f68856327af3967ca959d96f95cbe2eea02
@@ -0,0 +1,17 @@
1
+ workflow "CI" {
2
+ on = "push"
3
+ resolves = ["CI - All"]
4
+ }
5
+
6
+ action "CI - All" {
7
+ uses = "actions/bin/filter@d820d56839906464fb7a57d1b4e1741cf5183efa"
8
+ needs = [
9
+ "CI - Test"
10
+ ]
11
+ }
12
+
13
+ action "CI - Test" {
14
+ uses = "docker://lambci/lambda:build-ruby2.5"
15
+ runs = "bash"
16
+ args = ["-c", "bundle && bundle exec rake test"]
17
+ }
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.2
7
+ before_install: gem install bundler -v 2.0.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in compeon-access_token.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,24 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ compeon-access_token (0.1.0)
5
+ jwt (~> 2.1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ jwt (2.1.0)
11
+ minitest (5.11.3)
12
+ rake (10.5.0)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ bundler (~> 2.0)
19
+ compeon-access_token!
20
+ minitest (~> 5.0)
21
+ rake (~> 10.0)
22
+
23
+ BUNDLED WITH
24
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 COMPEON GmbH
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,76 @@
1
+ # Compeon::AccessToken
2
+
3
+ TODO: Delete this and the text above, and describe your gem
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'compeon-access_token'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install compeon-access_token
20
+
21
+ ## Usage
22
+
23
+ Encode a token
24
+
25
+ ```ruby
26
+ token = Compeon::Token::Access.new(
27
+ client_id: 'compeon-auth',
28
+ role: 'customer',
29
+ user_id: '123'
30
+ )
31
+
32
+ token.aud = 'audience'
33
+ token.iat = Time.now.to_i
34
+ token.exp = Time.now.to_i + 3600 # 1 Expiry time is required and must be in the future.
35
+ token.iss = 'issuer'
36
+ token.sub = 'subject'
37
+
38
+ token.encode(key: OpenSSL::PKey::RSA.new(private_key_string))
39
+ # => eyJhbGciOiJIUzI1NiIsInR5cC...
40
+ ```
41
+
42
+ Decode a token
43
+
44
+ ```ruby
45
+ token = Compeon::Token::Access.decode(
46
+ encoded_token: 'eyJhbGciOiJIUzI1NiIsInR5cC...',
47
+ key: OpenSSL::PKey::RSA.new(private_key_string).public_key
48
+ )
49
+
50
+ token.client_id # => 'compeon-auth'
51
+ token.role # => 'customer'
52
+ token.user_id # => '123'
53
+ token.aud # => 'audience'
54
+ token.iss # => 'issuer'
55
+ # etc.
56
+
57
+ ```
58
+
59
+ Decode a token and verify reserved claims
60
+
61
+ ```ruby
62
+ token = Compeon::Token::Access.decode(
63
+ # The `exp` claim is validated by default and is not needed here
64
+ claim_verifications: { aud: 'audience', iat: true, iss: 'issuer', sub: 'subject' },
65
+ encoded_token: 'eyJhbGciOiJIUzI1NiIsInR5cC...',
66
+ key: OpenSSL::PKey::RSA.new(private_key_string).public_key
67
+ )
68
+ ```
69
+
70
+ ## Contributing
71
+
72
+ Bug reports and pull requests are welcome on GitHub at https://github.com/compeon/compeon-access_token.
73
+
74
+ ## License
75
+
76
+ 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,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'compeon/access_token'
5
+ require 'compeon/token'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ 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,28 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'compeon/access_token/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'compeon-access_token'
7
+ spec.version = Compeon::AccessToken::VERSION
8
+ spec.authors = ['Timo Schilling']
9
+ spec.email = ['timo@schilling.io']
10
+
11
+ spec.summary = "Helper for handling COMPEON AccessToken's"
12
+ spec.description = "Helper for handling COMPEON AccessToken's"
13
+ spec.homepage = 'https://github.com/COMPEON/compeon-access_token'
14
+ spec.license = 'MIT'
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_dependency 'jwt', '~> 2.1'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 2.0'
26
+ spec.add_development_dependency 'minitest', '~> 5.0'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ end
@@ -0,0 +1,5 @@
1
+ module Compeon
2
+ class AccessToken
3
+ VERSION = '0.2.0'.freeze
4
+ end
5
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'compeon/access_token/version'
4
+
5
+ require 'jwt'
6
+ require 'open-uri'
7
+
8
+ module Compeon
9
+ class AccessToken
10
+ class ParseError < RuntimeError; end
11
+
12
+ def initialize(role:, user_id:, kind:, client_id:, token:)
13
+ @role = role
14
+ @user_id = user_id
15
+ @kind = kind
16
+ @client_id = client_id
17
+ @token = token
18
+ end
19
+
20
+ attr_reader :role, :user_id, :kind, :client_id, :token
21
+
22
+ class << self
23
+ attr_writer :environment
24
+
25
+ def environment
26
+ @environment ||
27
+ ENV['ENVIRONMENT'] ||
28
+ raise("`#{self}.environment` or `ENV['ENVIRONMENT']` must be set")
29
+ end
30
+
31
+ def parse(token)
32
+ data, _header = JWT.decode(token, public_key, false, algorithm: 'RS256')
33
+
34
+ role = data.fetch('role')
35
+ user_id = data.fetch('uid')
36
+ kind = data.fetch('knd')
37
+ client_id = data.fetch('cid')
38
+
39
+ new(role: role, user_id: user_id, kind: kind, client_id: client_id, token: token)
40
+ rescue JWT::DecodeError
41
+ raise ParseError
42
+ end
43
+
44
+ def public_key
45
+ @public_key ||= OpenSSL::PKey::RSA.new(public_key_string)
46
+ end
47
+
48
+ def public_key_string=(value)
49
+ @public_key = nil
50
+ @public_key_string = value
51
+ end
52
+
53
+ def public_key_string
54
+ @public_key_string ||= begin
55
+ env_subdomain = environment != 'production' ? ".#{environment}" : nil
56
+ URI.parse("https://login#{env_subdomain}.compeon.de/public-key").read
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Compeon
4
+ module Token
5
+ class Access < Base
6
+ class << self
7
+ def attributes_mapping
8
+ {
9
+ client_id: :cid,
10
+ role: :role,
11
+ user_id: :uid
12
+ }.freeze
13
+ end
14
+
15
+ def jwt_algorithm
16
+ 'RS256'
17
+ end
18
+
19
+ def kind
20
+ 'access'
21
+ end
22
+ end
23
+
24
+ attr_accessor :client_id, :role, :user_id
25
+
26
+ def initialize(client_id:, role:, user_id:, **claims)
27
+ super(claims)
28
+ @client_id = client_id
29
+ @role = role
30
+ @user_id = user_id
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Compeon
4
+ module Token
5
+ class Base
6
+ attr_accessor :audience, :expires_at, :issued_at, :issuer, :not_before, :subject
7
+
8
+ class << self
9
+ def attributes
10
+ @attributes ||= attributes_mapping.keys.freeze
11
+ end
12
+
13
+ def registered_claims_mapping
14
+ {
15
+ audience: :aud,
16
+ expires_at: :exp,
17
+ issued_at: :iat,
18
+ issuer: :iss,
19
+ not_before: :nbf,
20
+ subject: :sub
21
+ }.freeze
22
+ end
23
+
24
+ def decode(claim_verifications: {}, encoded_token:, key:)
25
+ Compeon::Token::Decoder.new(
26
+ claim_verifications: claim_verifications,
27
+ encoded_token: encoded_token,
28
+ key: key,
29
+ token_klass: self
30
+ ).decode
31
+ end
32
+ end
33
+
34
+ def initialize(audience: nil, expires_at: nil, issued_at: nil, issuer: nil, not_before: nil, subject: nil)
35
+ @audience = audience
36
+ @expires_at = expires_at
37
+ @issued_at = issued_at
38
+ @issuer = issuer
39
+ @not_before = not_before
40
+ @subject = subject
41
+ end
42
+
43
+ def encode(key:)
44
+ Compeon::Token::Encoder.new(
45
+ key: key,
46
+ token: self
47
+ ).encode
48
+ end
49
+
50
+ def registered_claims
51
+ self
52
+ .class
53
+ .registered_claims_mapping
54
+ .invert
55
+ .transform_values { |claim| public_send(claim) }
56
+ .compact
57
+ end
58
+
59
+ def valid?
60
+ expires_at_valid? && attributes_valid?
61
+ end
62
+
63
+ def expires_at_valid?
64
+ !expires_at.nil? && expires_at > Time.now.to_i
65
+ end
66
+
67
+ def attributes_valid?
68
+ self.class.attributes.none? { |accessor| public_send(accessor).nil? }
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'jwt'
4
+
5
+ module Compeon
6
+ module Token
7
+ class DecodeError < StandardError; end
8
+
9
+ class Decoder
10
+ def initialize(claim_verifications: {}, encoded_token:, key:, token_klass:)
11
+ @claim_verifications = claim_verifications
12
+ @encoded_token = encoded_token
13
+ @key = key
14
+ @token_klass = token_klass
15
+ end
16
+
17
+ def decode
18
+ raise DecodeError if decoded_token[:knd] != token_klass.kind
19
+
20
+ attributes = decoded_token_attributes
21
+ attributes.delete(:knd)
22
+
23
+ token_klass.new(attributes)
24
+ end
25
+
26
+ private
27
+
28
+ attr_reader :claim_verifications, :encoded_token, :key, :token_klass
29
+
30
+ def decoded_token
31
+ @decoded_token ||= JWT.decode(
32
+ encoded_token,
33
+ key,
34
+ true,
35
+ algorithm: token_klass.jwt_algorithm,
36
+ **compiled_claim_verifications
37
+ )[0].transform_keys(&:to_sym)
38
+ rescue JWT::DecodeError
39
+ raise DecodeError
40
+ end
41
+
42
+ def decoded_token_attributes
43
+ attributes_mapping = token_klass.attributes_mapping
44
+ registered_claims_mapping = token_klass.registered_claims_mapping
45
+
46
+ decoded_token.transform_keys do |attribute|
47
+ attributes_mapping.key(attribute) ||
48
+ registered_claims_mapping.key(attribute) ||
49
+ attribute
50
+ end
51
+ end
52
+
53
+ def compiled_claim_verifications
54
+ {}.tap do |verifications|
55
+ %i[aud iss sub].each do |claim|
56
+ next unless claim_verifications[claim]
57
+
58
+ verifications[claim] = claim_verifications[claim]
59
+ verifications[:"verify_#{claim}"] = true
60
+ end
61
+
62
+ verifications[:verify_iat] = true if claim_verifications[:iat]
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'jwt'
4
+
5
+ module Compeon
6
+ module Token
7
+ class EncodeError < StandardError; end
8
+
9
+ class Encoder
10
+ def initialize(key:, token:)
11
+ @token = token
12
+ @key = key
13
+
14
+ raise 'No key given.' if @key.nil?
15
+ raise 'Token is invalid.' unless @token.valid?
16
+ end
17
+
18
+ def encode
19
+ JWT.encode(
20
+ {
21
+ **attributes,
22
+ **token.registered_claims,
23
+ knd: token.class.kind
24
+ },
25
+ key,
26
+ token.class.jwt_algorithm
27
+ )
28
+ rescue JWT::EncodeError
29
+ raise EncodeError
30
+ end
31
+
32
+ private
33
+
34
+ attr_reader :token, :key
35
+
36
+ def attributes
37
+ token
38
+ .class
39
+ .attributes_mapping
40
+ .invert
41
+ .transform_values { |attribute| token.public_send(attribute) }
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'compeon/token/base'
4
+ require 'compeon/token/access'
5
+ require 'compeon/token/decoder'
6
+ require 'compeon/token/encoder'
7
+
8
+ module Compeon
9
+ module Token
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: compeon-access_token
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Timo Schilling
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-09-09 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'
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'
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: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
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: Helper for handling COMPEON AccessToken's
70
+ email:
71
+ - timo@schilling.io
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".github/main.workflow"
77
+ - ".gitignore"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - Gemfile.lock
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - compeon-accesss_token.gemspec
87
+ - lib/compeon/access_token.rb
88
+ - lib/compeon/access_token/version.rb
89
+ - lib/compeon/token.rb
90
+ - lib/compeon/token/access.rb
91
+ - lib/compeon/token/base.rb
92
+ - lib/compeon/token/decoder.rb
93
+ - lib/compeon/token/encoder.rb
94
+ homepage: https://github.com/COMPEON/compeon-access_token
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
+ rubygems_version: 3.0.3
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: Helper for handling COMPEON AccessToken's
117
+ test_files: []