access_token_agent 3.1.0 → 3.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f927f5c7b9e229be4e384f66103ac838b240945f
4
- data.tar.gz: a8913390a677bd3e6d8f8c8e38f9456c8d4eec74
3
+ metadata.gz: dc26e506a5e724e2f3fb483ffb8a8b7b74447e35
4
+ data.tar.gz: 31cce846d3c53a66737d2f5cb603dcf269ea3119
5
5
  SHA512:
6
- metadata.gz: 05f95b2828d254c18f8c2873f9c11177a48a290847738d50389bdb02c540a50e03574e7bcb2a9acfedfd377334b8b42b1c1538588775cdbbe95133b60545a400
7
- data.tar.gz: 7fd13b9d23077fe8b5de3d5e42fd86e9903c0c2fe5fe46b6d626c08ad466f52b1ba5be23e021f82b59e15f21227f3594b1ceceea1a181107c97e9070796b5784
6
+ metadata.gz: 560a1ce6c00cf31738b7d3c2d6ae7ee2ef0b9b9857e01aa620d299197f8cdba74daaf031a2cb5b7eee60d4f6c77e5f581ed86ed9e1a1279fb5ac83132d9a91a1
7
+ data.tar.gz: e5afd37637915dbe189fe7630b3b092e8cb588262f8c64167685711f9e2e0dc644bbbd08fc2f5d02a9333c35566dc5da2f36e29e17e607bdb20acd808cc17af5
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ Gemfile.lock
2
+ coverage/
3
+ tmp/
4
+
5
+ .ruby-gemset
6
+ access_token_agent-*.gem
data/.rubocop.yml ADDED
@@ -0,0 +1,44 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.1
3
+ Exclude:
4
+ - 'db/schema.rb'
5
+ - 'doc/**/*'
6
+ - 'Gemfile'
7
+ - '*.gemspec'
8
+
9
+ Style/AsciiComments:
10
+ Enabled: false
11
+
12
+ Style/Documentation:
13
+ Enabled: false
14
+
15
+ # Encoding comments are not neccessary in all 2.x versions of ruby, since
16
+ # UTF-8 has become the default encoding.
17
+ Style/Encoding:
18
+ EnforcedStyle: never
19
+ Enabled: true
20
+
21
+ # This cop tries to make you use module_funtion instead of extend self
22
+ # This is bad because both have their own use-case and should not be used
23
+ # and sometimes cannot be used to do the same thing
24
+ Style/ModuleFunction:
25
+ Enabled: false
26
+ # While it is very often useful to separate numbers after every three digits
27
+ # for readability, this mostly doesn't make sense if the number doesn't
28
+ # represent an amount but rather an identifier. Thus the use of underscores
29
+ # every three digits is recommended but not enforced.
30
+ Style/NumericLiterals:
31
+ Enabled: false
32
+
33
+ # Do not force the same one letter variable names for all occurences of inject
34
+ Style/SingleLineBlockParams:
35
+ Enabled: false
36
+
37
+ # No significant improvement in speed or memory usage apparent. Readability is
38
+ # atrocious.
39
+ Performance/Casecmp:
40
+ Enabled: false
41
+
42
+ # Not safe in a rails context, since Relation.count is != Enumerable.count
43
+ Performance/Count:
44
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.10
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ script:
3
+ - bundle exec rspec
4
+ - bundle exec rubocop
data/CHANGELOG.md ADDED
@@ -0,0 +1,25 @@
1
+ ## 3.1.1
2
+
3
+ - Fix broken gem release (missing files)
4
+
5
+ ## 3.1.0
6
+
7
+ - Raise `AccessTokenAgent::ConnectionError` if the auth service could not be reached.
8
+
9
+ ## 3.0.0
10
+
11
+ - Rename fake_authenticate parameter to fake_auth
12
+ - This is compatible with the file format that AuthConnector already expects
13
+
14
+ ## 2.0.1
15
+
16
+ - Remove obsolete class Credentials
17
+
18
+ ## 2.0.0
19
+
20
+ - Rename base_uri parameter to host
21
+ - This is compatible with the file format that AuthConnector already expects
22
+
23
+ ## 1.0.0
24
+
25
+ - initial Release
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # encoding: UTF-8
2
+ source 'https://rubygems.org'
3
+
4
+ # Specify your gem's dependencies in auth_connector.gemspec
5
+ gemspec
data/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016 Beko Käuferportal 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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ [![Build Status](https://travis-ci.org/kaeuferportal/access_token_agent.svg?branch=master)](https://travis-ci.org/kaeuferportal/access_token_agent)
2
+
3
+ # AccessTokenAgent
4
+
5
+ Handles authentication against an OAuth2 provider.
6
+
7
+ Retrieves an access token from the authentication server using the
8
+ OAuth2 [client credentials flow](https://tools.ietf.org/html/rfc6749#section-4.4).
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'access_token_agent', '~> 3.1'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ ## Configuration
23
+
24
+ Create an instance of AccessTokenAgent::Connector with the desired
25
+ configuration and use that instance to authenticate.
26
+
27
+ Needs the following parameters:
28
+
29
+ * `host` - the server address where the auth provider is running.
30
+ * `client_id` - the client_id of the application using this gem.
31
+ * `client_secret` - the client_secret of the application using this gem.
32
+
33
+ Optional parameters:
34
+
35
+ * `fake_auth` - if true, do not connect to the auth service and return
36
+ an empty access token (`nil`).
37
+
38
+ ### Example
39
+
40
+ ```ruby
41
+ AccessTokenAgent::Connector.new(host: 'https://auth.kaeuferportal.de',
42
+ client_id: 'my_client',
43
+ client_secret: 'very_secure_and_secret')
44
+ ```
45
+
46
+ ## Usage
47
+
48
+ Setup an AcccessTokenAgent::Connector instance (see Configuration) and call
49
+ authenticate on it to receive your access_token.
50
+
51
+ ```
52
+ @access_token_agent.authenticate
53
+ ```
54
+
55
+ When no valid AccessToken is present a call to authenticate returns one of the
56
+ following:
57
+ - a Bearer Token if the credentials are valid (auth response code 200)
58
+ - raises an UnauthorizedError if the credentials are invalid (auth response
59
+ code 401)
60
+ - raises an Error if the auth response code is neither 200 nor 401
61
+
62
+ As long as a valid AccessToken is present a call to authenticate simply returns
63
+ that AccessToken. An AccessToken is valid for a limited time. The exact value is
64
+ determined by the auth response which contains an `expires_at` parameter.
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'access_token_agent'
5
+ s.version = '3.1.1'
6
+ s.date = '2016-04-08'
7
+ s.summary = 'Handles authentication against an OAuth2 provider'
8
+ s.homepage = 'https://github.com/kaeuferportal/access_token_agent'
9
+ s.description = 'Retrieves an access token from an OAuth2 provider' \
10
+ 'using the supplied credentials.'
11
+ s.authors = ['Beko Käuferportal GmbH']
12
+ s.email = 'oss@kaeuferportal.de'
13
+ s.license = 'MIT'
14
+ s.files = `git ls-files -z`.split("\x0")
15
+ .reject { |f| f.match(%r{^spec/}) }
16
+
17
+ s.add_development_dependency 'bundler', '~> 1.11'
18
+ s.add_development_dependency 'rspec', '~> 3.4'
19
+ s.add_development_dependency 'pry', '~> 0.10'
20
+ s.add_development_dependency 'rubocop', '~> 0.39'
21
+ s.add_development_dependency 'vcr', '~> 3.0'
22
+ s.add_development_dependency 'webmock', '~> 1.24'
23
+ s.add_development_dependency 'simplecov', '~> 0.11'
24
+ end
@@ -0,0 +1,7 @@
1
+ module AccessTokenAgent
2
+ class ConnectionError < Error
3
+ def initialize
4
+ super('Could not connect to the auth service.')
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,53 @@
1
+ require 'net/http'
2
+
3
+ module AccessTokenAgent
4
+ class Connector
5
+ def initialize(host:,
6
+ client_id:,
7
+ client_secret:,
8
+ fake_auth: false)
9
+ @host = host
10
+ @client_id = client_id
11
+ @client_secret = client_secret
12
+ @fake_auth = fake_auth
13
+ end
14
+
15
+ def authenticate
16
+ return if @fake_auth
17
+ fetch_token unless @known_token && @known_token.valid?
18
+ @known_token.value
19
+ end
20
+
21
+ def fetch_token
22
+ @known_token = Token.new(from_auth)
23
+ end
24
+
25
+ def from_auth
26
+ response = request
27
+ case response.code
28
+ when '200' then JSON.parse(response.body)
29
+ when '401' then raise UnauthorizedError
30
+ else
31
+ raise Error, "status: #{response.code}, body: #{response.body}"
32
+ end
33
+ rescue Errno::ECONNREFUSED
34
+ raise ConnectionError
35
+ end
36
+
37
+ def request
38
+ request = Net::HTTP::Post.new(auth_uri)
39
+ request.basic_auth @client_id, @client_secret
40
+ request.form_data = { 'grant_type' => 'client_credentials' }
41
+ use_tls = auth_uri.scheme == 'https'
42
+ Net::HTTP.start(auth_uri.hostname,
43
+ auth_uri.port,
44
+ use_ssl: use_tls) do |http|
45
+ http.request(request)
46
+ end
47
+ end
48
+
49
+ def auth_uri
50
+ @auth_uri ||= URI("#{@host}/oauth/token")
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,2 @@
1
+ class Error < StandardError
2
+ end
@@ -0,0 +1,5 @@
1
+ class InvalidTokenTypeError < Error
2
+ def initialize(token_type)
3
+ super("Expected token_type to be 'bearer', but was '#{token_type}'.")
4
+ end
5
+ end
@@ -0,0 +1,21 @@
1
+ require 'access_token_agent/invalid_token_type_error'
2
+
3
+ module AccessTokenAgent
4
+ class Token
5
+ attr_reader :value, :expires_at
6
+
7
+ EXPIRY_MARGIN = 60 # seconds
8
+
9
+ def initialize(auth_response)
10
+ unless auth_response['token_type'] == 'bearer'
11
+ raise InvalidTokenTypeError, auth_response['token_type']
12
+ end
13
+ @value = auth_response['access_token']
14
+ @expires_at = Time.now + auth_response['expires_in']
15
+ end
16
+
17
+ def valid?
18
+ @expires_at - EXPIRY_MARGIN > Time.now
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,7 @@
1
+ module AccessTokenAgent
2
+ class UnauthorizedError < Error
3
+ def initialize
4
+ super('The credentials are invalid.')
5
+ end
6
+ end
7
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: access_token_agent
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Beko Käuferportal GmbH
@@ -114,7 +114,22 @@ executables: []
114
114
  extensions: []
115
115
  extra_rdoc_files: []
116
116
  files:
117
+ - ".gitignore"
118
+ - ".rubocop.yml"
119
+ - ".ruby-version"
120
+ - ".travis.yml"
121
+ - CHANGELOG.md
122
+ - Gemfile
123
+ - LICENSE.md
124
+ - README.md
125
+ - access_token_agent.gemspec
117
126
  - lib/access_token_agent.rb
127
+ - lib/access_token_agent/connection_error.rb
128
+ - lib/access_token_agent/connector.rb
129
+ - lib/access_token_agent/error.rb
130
+ - lib/access_token_agent/invalid_token_type_error.rb
131
+ - lib/access_token_agent/token.rb
132
+ - lib/access_token_agent/unauthorized_error.rb
118
133
  homepage: https://github.com/kaeuferportal/access_token_agent
119
134
  licenses:
120
135
  - MIT
@@ -135,7 +150,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
150
  version: '0'
136
151
  requirements: []
137
152
  rubyforge_project:
138
- rubygems_version: 2.4.8
153
+ rubygems_version: 2.2.5
139
154
  signing_key:
140
155
  specification_version: 4
141
156
  summary: Handles authentication against an OAuth2 provider