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

Enable PKCE extension in GDS OmniAuth Strategy #283

Merged
merged 1 commit into from
Aug 24, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Unreleased

* Enable [OAuth2 PKCE extension](https://datatracker.ietf.org/doc/html/rfc7636) in the GDS OAuth2 OmniAuth Strategy. The [PKCE extension was enabled in Signon in PR 2312](https://github.com/alphagov/signon/pull/2312).

# 18.0.0

* Drop support for Ruby 2.7. (#277)
Expand Down
2 changes: 2 additions & 0 deletions lib/omniauth/strategies/gds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
class OmniAuth::Strategies::Gds < OmniAuth::Strategies::OAuth2
uid { user["uid"] }

option :pkce, true

info do
{
name: user["name"],
Expand Down
36 changes: 36 additions & 0 deletions spec/system/authentication_and_authorisation_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,42 @@
require "spec_helper"

RSpec.describe "Authenication and authorisation" do
context "omniauth request phase" do
let(:redirect_url) { URI.parse(page.response_headers["Location"]) }
let(:authorize_params) { Rack::Utils.parse_query(redirect_url.query) }

before do
visit "/auth/gds"
end

it "includes pkce code_challenge_method in request for /oauth/authorize" do
expect(redirect_url.path).to eql("/oauth/authorize")
expect(authorize_params["code_challenge_method"]).to eq("S256")
end

it "includes pkce code_challenge in request for /oauth/authorize" do
expect(redirect_url.path).to eql("/oauth/authorize")
expect(authorize_params["code_challenge"]).to be_present
end
end

context "omniauth callback phase" do
it "includes pkce code_verifier in request for /oauth/access_token" do
visit "/auth/gds"

redirect_url = URI.parse(page.response_headers["Location"])
expect(redirect_url.path).to eql("/oauth/authorize")
state = Rack::Utils.parse_query(redirect_url.query)["state"]

stub_request(:post, "http://signon/oauth/access_token")

visit "/auth/gds/callback?state=#{state}"

expect(WebMock).to have_requested(:post, "http://signon/oauth/access_token")
.with(body: hash_including({ "code_verifier" => /.*/ }))
end
end

context "when accessing a route that doesn't require permissions or authentication" do
it "allows access" do
visit "/not-restricted"
Expand Down