Skip to content

Commit

Permalink
Respond with JSON-formatted error when in api_only mode
Browse files Browse the repository at this point in the history
Fixes #1121
  • Loading branch information
Justin Bull committed Jul 13, 2018
1 parent 66d8e18 commit 769da0a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions NEWS.md
Expand Up @@ -14,6 +14,7 @@ User-visible changes worth mentioning.
hitting the `AuthorizedApplicationController#destroy` route.
- [#1114] Make token info endpoint's attributes consistent with token creation
- [#1119] Fix token revocation for OAuth apps using "implicit" grant flow
- [#1122] Fix AuthorizationsController#new error response to be in JSON format

## 5.0.0.rc1

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/doorkeeper/authorizations_controller.rb
Expand Up @@ -33,7 +33,7 @@ def render_success

def render_error
if Doorkeeper.configuration.api_only
render json: pre_auth.error_response.body[:error_description],
render json: pre_auth.error_response.body,
status: :bad_request
else
render :error
Expand Down
27 changes: 27 additions & 0 deletions spec/controllers/authorizations_controller_spec.rb
Expand Up @@ -387,6 +387,33 @@ def translated_error_message(key)
end
end

describe 'GET #new in API mode with errors' do
let(:response_json_body) { JSON.parse(response.body) }

before do
default_scopes_exist :public
allow(Doorkeeper.configuration).to receive(:api_only).and_return(true)
get :new, params: { an_invalid: 'request' }
end

it 'should render bad request' do
expect(response).to have_http_status(:bad_request)
end

it 'includes error in body' do
expect(response_json_body['error']).to eq('unsupported_response_type')
end

it 'includes error description in body' do
expect(response_json_body['error_description']).to eq(translated_error_message(:unsupported_response_type))
end

it 'does not issue any token' do
expect(Doorkeeper::AccessGrant.count).to eq 0
expect(Doorkeeper::AccessToken.count).to eq 0
end
end

describe 'GET #new with callbacks' do
after do
client.update_attribute :redirect_uri, 'urn:ietf:wg:oauth:2.0:oob'
Expand Down

0 comments on commit 769da0a

Please sign in to comment.