Skip to content

Commit

Permalink
feature: Support prompt=none parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
toupeira committed Oct 26, 2016
1 parent 64f7173 commit c775d8b
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 4 deletions.
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ en:
doorkeeper:
scopes:
openid: 'Authenticate your account'
errors:
messages:
login_required: 'The authorization server requires end-user authentication'
openid_connect:
errors:
messages:
Expand Down
16 changes: 12 additions & 4 deletions lib/doorkeeper/openid_connect.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'doorkeeper/openid_connect/version'
require 'doorkeeper/openid_connect/engine'

require 'doorkeeper/openid_connect/helpers/controller'

require 'doorkeeper/openid_connect/models/id_token'
require 'doorkeeper/openid_connect/models/user_info'
require 'doorkeeper/openid_connect/models/claims/claim'
Expand All @@ -15,10 +17,6 @@
require 'json/jwt'

module Doorkeeper
class << self
prepend OpenidConnect::DoorkeeperConfiguration
end

module OpenidConnect
# TODO: make this configurable
SIGNING_ALGORITHM = 'RS256'
Expand All @@ -37,6 +35,16 @@ def self.signing_key
end
end

module Doorkeeper
class << self
prepend ::Doorkeeper::OpenidConnect::DoorkeeperConfiguration
end

module Helpers::Controller
prepend ::Doorkeeper::OpenidConnect::Helpers::Controller
end
end

module Doorkeeper
module OAuth
class PasswordAccessTokenRequest
Expand Down
30 changes: 30 additions & 0 deletions lib/doorkeeper/openid_connect/helpers/controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Doorkeeper
module OpenidConnect
module Helpers
module Controller
private

def authenticate_resource_owner!
owner = super

if prompt_values.include?('none') && (!owner || owner.is_a?(String))
# clear the previous response body to avoid a DoubleRenderError
# TODO: this is currently broken on Rails 5, see
# https://github.com/rails/rails/issues/25106
self.response_body = nil

error = ::Doorkeeper::OAuth::ErrorResponse.new(name: :login_required)
response.headers.merge!(error.headers)
render json: error.body, status: error.status
else
owner
end
end

def prompt_values
@prompt_values ||= params[:prompt].to_s.split(/ +/)
end
end
end
end
end
37 changes: 37 additions & 0 deletions spec/controllers/doorkeeper/authorizations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'rails_helper'

describe Doorkeeper::AuthorizationsController, type: :controller do
describe '#new' do
context 'without a prompt parameter' do
it 'renders the authorization form if logged in' do
get :new, current_user: 'Joe'

expect(response).to be_successful
end

it 'redirects to login form when not logged in' do
get :new

expect(response).to redirect_to '/login'
end
end

context 'with a prompt=none parameter' do
it 'renders the authorization form if logged in' do
get :new, current_user: 'Joe', prompt: 'none'

expect(response).to be_successful
end

it 'returns an error when not logged in' do
get :new, prompt: 'none'

expect(response.status).to eq 401
expect(JSON.parse(response.body)).to eq({
'error' => 'login_required',
'error_description' => 'The authorization server requires end-user authentication'
})
end
end
end
end
7 changes: 7 additions & 0 deletions spec/dummy/config/initializers/doorkeeper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
Doorkeeper.configure do
resource_owner_authenticator do
if params[:current_user]
User.new name: params[:current_user]
else
redirect_to('/login')
end
end
end

0 comments on commit c775d8b

Please sign in to comment.