Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to customize Token Introspection response #1195

Merged
merged 1 commit into from Jan 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Expand Up @@ -7,6 +7,7 @@ User-visible changes worth mentioning.

## master

- [#1195] Allow to customize Token Introspection response (fix [#1194]).
- [#1189] Option to set `token_reuse_limit`.
- [#1191] Try to load bcrypt for hashing of application secrets, but add fallback.

Expand Down
15 changes: 10 additions & 5 deletions lib/doorkeeper/config.rb
Expand Up @@ -261,11 +261,16 @@ def option(name, options = {})

nil
end)
option :before_successful_authorization, default: ->(_context) {}
option :after_successful_authorization, default: ->(_context) {}
option :before_successful_strategy_response, default: ->(_request) {}
option :after_successful_strategy_response,
default: ->(_request, _response) {}

# Hooks for authorization
option :before_successful_authorization, default: ->(_context) {}
option :after_successful_authorization, default: ->(_context) {}
# Hooks for strategies responses
option :before_successful_strategy_response, default: ->(_request) {}
option :after_successful_strategy_response, default: ->(_request, _response) {}
nbulaj marked this conversation as resolved.
Show resolved Hide resolved
# Allows to customize Token Introspection response
option :custom_introspection_response, default: ->(_token, _context) { {} }
nbulaj marked this conversation as resolved.
Show resolved Hide resolved

option :skip_authorization, default: ->(_routes) {}
option :access_token_expires_in, default: 7200
option :custom_access_token_expires_in, default: ->(_context) { nil }
Expand Down
17 changes: 15 additions & 2 deletions lib/doorkeeper/oauth/token_introspection.rb
Expand Up @@ -60,14 +60,14 @@ def authorized_token

# 2.2. Introspection Response
def success_response
{
customize_response(
active: true,
scope: @token.scopes_string,
client_id: @token.try(:application).try(:uid),
token_type: @token.token_type,
exp: @token.expires_at.to_i,
iat: @token.created_at.to_i
}
)
end

# If the introspection call is properly authorized but the token is not
Expand Down Expand Up @@ -125,6 +125,19 @@ def authorized_for_client?
true
end
end

# Allows to customize introspection response.
# Provides context (controller) and token for generating developer-specific
nbulaj marked this conversation as resolved.
Show resolved Hide resolved
# response.
def customize_response(response)
customized_response = Doorkeeper.configuration.custom_introspection_response.call(
nbulaj marked this conversation as resolved.
Show resolved Hide resolved
token,
server.context
)
return response if customized_response.blank?

response.merge(customized_response)
end
end
end
end
16 changes: 16 additions & 0 deletions lib/generators/doorkeeper/templates/initializer.rb
Expand Up @@ -219,6 +219,22 @@
#
# handle_auth_errors :raise

# Customize token introspection response.
# Allows to add your own fields to the introspection response
# (like `sub`, `aud` and so on). Can be a proc, lambda or any object responds
# to `.call` method).
#
# custom_introspection_response do |token, context|
# {
# "sub": "Z5O3upPC88QrAjx00dis",
# "aud": "https://protected.example.net/resource",
# }
# end
#
# or
#
# custom_introspection_response CustomIntrospectionResponder

# Specify what grant flows are enabled in array of Strings. The valid
# strings and the flows they enable are:
#
Expand Down
27 changes: 27 additions & 0 deletions spec/controllers/tokens_controller_spec.rb
Expand Up @@ -159,6 +159,33 @@
end
end

context 'using custom introspection response' do
let(:client) { FactoryBot.create(:application) }
let(:access_token) { FactoryBot.create(:access_token, application: client) }

before do
Doorkeeper.configure do
orm DOORKEEPER_ORM
custom_introspection_response do |_token, _context|
{
sub: 'Z5O3upPC88QrAjx00dis',
aud: 'https://protected.example.net/resource'
}
end
end
end

it 'responds with full token introspection' do
request.headers['Authorization'] = "Bearer #{access_token.token}"

post :introspect, params: { token: access_token.token }

expect(json_response).to include('client_id', 'token_type', 'exp', 'iat', 'sub', 'aud')
should_have_json 'sub', 'Z5O3upPC88QrAjx00dis'
should_have_json 'aud', 'https://protected.example.net/resource'
end
end

context 'public access token' do
let(:client) { FactoryBot.create(:application) }
let(:access_token) { FactoryBot.create(:access_token, application: nil) }
Expand Down