access_token_agent 3.1.1 → 3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dc26e506a5e724e2f3fb483ffb8a8b7b74447e35
4
- data.tar.gz: 31cce846d3c53a66737d2f5cb603dcf269ea3119
3
+ metadata.gz: b05e9bb8df48a419db6aa15bf18bca718638a4e8
4
+ data.tar.gz: 405ed7dfca477e3763e8acd6fc12b394813e0ffb
5
5
  SHA512:
6
- metadata.gz: 560a1ce6c00cf31738b7d3c2d6ae7ee2ef0b9b9857e01aa620d299197f8cdba74daaf031a2cb5b7eee60d4f6c77e5f581ed86ed9e1a1279fb5ac83132d9a91a1
7
- data.tar.gz: e5afd37637915dbe189fe7630b3b092e8cb588262f8c64167685711f9e2e0dc644bbbd08fc2f5d02a9333c35566dc5da2f36e29e17e607bdb20acd808cc17af5
6
+ metadata.gz: 19b6c44ddedcfdd3a7dc8a7c67dc0ab73d81c7cf11129c6bb2ccc2bf897d0cd15dee6400bf82fae597cbb49cb6b602a39ed5f56064a929ff1970e253231f7fcf
7
+ data.tar.gz: 90dab0e2595761e707ca502ec1b0b3489cbf8e7d96fdf21a6ca8952715856db351acfd29235b281567aa298d786eefc105fe751555042d77990bca76f5e3899b
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/CHANGELOG.md CHANGED
@@ -1,3 +1,18 @@
1
+ ## master
2
+
3
+ *no unreleased changes*
4
+
5
+ ## 3.2.0
6
+
7
+ - Add `http_auth_header` method to the connector, since this is the most
8
+ common use case
9
+ - Deprecate the `authenticate` method in favor of the new `token` method
10
+ - Allow to configure from which path to get the access token
11
+ - Put all errors into the AccessTokenAgent namespace
12
+ - Actually return a token when faking auth
13
+ - Rename error raised for unsupported token types
14
+ - Ensure that access token response carries an access token
15
+
1
16
  ## 3.1.1
2
17
 
3
18
  - Fix broken gem release (missing files)
data/README.md CHANGED
@@ -19,21 +19,24 @@ And then execute:
19
19
 
20
20
  $ bundle
21
21
 
22
- ## Configuration
22
+ ## Basic Configuration
23
23
 
24
24
  Create an instance of AccessTokenAgent::Connector with the desired
25
25
  configuration and use that instance to authenticate.
26
26
 
27
- Needs the following parameters:
27
+ Needs the following parameters to instantiate:
28
28
 
29
29
  * `host` - the server address where the auth provider is running.
30
30
  * `client_id` - the client_id of the application using this gem.
31
31
  * `client_secret` - the client_secret of the application using this gem.
32
+ * `access_token_path` - Allows to customize the HTTP path where the
33
+ access token needs to be requested.
34
+ **Default:** `/oauth/token`
32
35
 
33
36
  Optional parameters:
34
37
 
35
38
  * `fake_auth` - if true, do not connect to the auth service and return
36
- an empty access token (`nil`).
39
+ a faked access token.
37
40
 
38
41
  ### Example
39
42
 
@@ -1,8 +1,12 @@
1
1
  # coding: utf-8
2
2
 
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'access_token_agent/version'
6
+
3
7
  Gem::Specification.new do |s|
4
8
  s.name = 'access_token_agent'
5
- s.version = '3.1.1'
9
+ s.version = AccessTokenAgent::VERSION
6
10
  s.date = '2016-04-08'
7
11
  s.summary = 'Handles authentication against an OAuth2 provider'
8
12
  s.homepage = 'https://github.com/kaeuferportal/access_token_agent'
data/bin/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'access_token_agent'
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
@@ -3,3 +3,4 @@ require 'access_token_agent/unauthorized_error'
3
3
  require 'access_token_agent/connection_error'
4
4
  require 'access_token_agent/token'
5
5
  require 'access_token_agent/connector'
6
+ require 'access_token_agent/version'
@@ -2,28 +2,44 @@ require 'net/http'
2
2
 
3
3
  module AccessTokenAgent
4
4
  class Connector
5
+ FAKE_TOKEN = 'FakeAuthToken'.freeze
6
+
5
7
  def initialize(host:,
6
8
  client_id:,
7
9
  client_secret:,
8
- fake_auth: false)
10
+ fake_auth: false,
11
+ access_token_path: '/oauth/token')
9
12
  @host = host
10
13
  @client_id = client_id
11
14
  @client_secret = client_secret
12
15
  @fake_auth = fake_auth
16
+ @access_token_path = access_token_path
13
17
  end
14
18
 
15
- def authenticate
16
- return if @fake_auth
17
- fetch_token unless @known_token && @known_token.valid?
19
+ def http_auth_header
20
+ { Authorization: "Bearer #{token}" }
21
+ end
22
+
23
+ def token
24
+ return FAKE_TOKEN if @fake_auth
25
+ @known_token = fetch_token unless @known_token && @known_token.valid?
26
+
18
27
  @known_token.value
19
28
  end
20
29
 
30
+ def authenticate
31
+ warn '[DEPRECATION] `authenticate` is deprecated. Use `token` instead.'
32
+ token
33
+ end
34
+
35
+ private
36
+
21
37
  def fetch_token
22
- @known_token = Token.new(from_auth)
38
+ Token.new(fetch_token_hash)
23
39
  end
24
40
 
25
- def from_auth
26
- response = request
41
+ def fetch_token_hash
42
+ response = perform_request
27
43
  case response.code
28
44
  when '200' then JSON.parse(response.body)
29
45
  when '401' then raise UnauthorizedError
@@ -34,7 +50,7 @@ module AccessTokenAgent
34
50
  raise ConnectionError
35
51
  end
36
52
 
37
- def request
53
+ def perform_request
38
54
  request = Net::HTTP::Post.new(auth_uri)
39
55
  request.basic_auth @client_id, @client_secret
40
56
  request.form_data = { 'grant_type' => 'client_credentials' }
@@ -47,7 +63,7 @@ module AccessTokenAgent
47
63
  end
48
64
 
49
65
  def auth_uri
50
- @auth_uri ||= URI("#{@host}/oauth/token")
66
+ @auth_uri ||= URI("#{@host}#{@access_token_path}")
51
67
  end
52
68
  end
53
69
  end
@@ -1,2 +1,4 @@
1
- class Error < StandardError
1
+ module AccessTokenAgent
2
+ class Error < StandardError
3
+ end
2
4
  end
@@ -0,0 +1,7 @@
1
+ module AccessTokenAgent
2
+ class MissingAccessToken < Error
3
+ def initialize
4
+ super('The access token response did not contain an access token.')
5
+ end
6
+ end
7
+ end
@@ -1,4 +1,5 @@
1
- require 'access_token_agent/invalid_token_type_error'
1
+ require 'access_token_agent/missing_access_token'
2
+ require 'access_token_agent/unsupported_token_type_error'
2
3
 
3
4
  module AccessTokenAgent
4
5
  class Token
@@ -7,9 +8,8 @@ module AccessTokenAgent
7
8
  EXPIRY_MARGIN = 60 # seconds
8
9
 
9
10
  def initialize(auth_response)
10
- unless auth_response['token_type'] == 'bearer'
11
- raise InvalidTokenTypeError, auth_response['token_type']
12
- end
11
+ validate_response(auth_response)
12
+
13
13
  @value = auth_response['access_token']
14
14
  @expires_at = Time.now + auth_response['expires_in']
15
15
  end
@@ -17,5 +17,16 @@ module AccessTokenAgent
17
17
  def valid?
18
18
  @expires_at - EXPIRY_MARGIN > Time.now
19
19
  end
20
+
21
+ private
22
+
23
+ def validate_response(auth_response)
24
+ unless auth_response['token_type'] == 'bearer'
25
+ raise UnsupportedTokenTypeError, auth_response['token_type']
26
+ end
27
+
28
+ token = auth_response['access_token']
29
+ raise MissingAccessToken if token.nil? || token.empty?
30
+ end
20
31
  end
21
32
  end
@@ -0,0 +1,7 @@
1
+ module AccessTokenAgent
2
+ class UnsupportedTokenTypeError < Error
3
+ def initialize(token_type)
4
+ super("Expected token_type to be 'bearer', but was '#{token_type}'.")
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module AccessTokenAgent
2
+ VERSION = '3.2.0'.freeze
3
+ 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.1
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Beko Käuferportal GmbH
@@ -115,6 +115,7 @@ extensions: []
115
115
  extra_rdoc_files: []
116
116
  files:
117
117
  - ".gitignore"
118
+ - ".rspec"
118
119
  - ".rubocop.yml"
119
120
  - ".ruby-version"
120
121
  - ".travis.yml"
@@ -123,13 +124,16 @@ files:
123
124
  - LICENSE.md
124
125
  - README.md
125
126
  - access_token_agent.gemspec
127
+ - bin/console
126
128
  - lib/access_token_agent.rb
127
129
  - lib/access_token_agent/connection_error.rb
128
130
  - lib/access_token_agent/connector.rb
129
131
  - lib/access_token_agent/error.rb
130
- - lib/access_token_agent/invalid_token_type_error.rb
132
+ - lib/access_token_agent/missing_access_token.rb
131
133
  - lib/access_token_agent/token.rb
132
134
  - lib/access_token_agent/unauthorized_error.rb
135
+ - lib/access_token_agent/unsupported_token_type_error.rb
136
+ - lib/access_token_agent/version.rb
133
137
  homepage: https://github.com/kaeuferportal/access_token_agent
134
138
  licenses:
135
139
  - MIT
@@ -1,5 +0,0 @@
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