Skip to content

Commit

Permalink
Merge pull request #2411 from alphagov/fix-sso-push-credential
Browse files Browse the repository at this point in the history
Ignore expired & revoked tokens in SSOPushCredential.credentials
  • Loading branch information
floehopper committed Oct 6, 2023
2 parents 086e205 + 0a718b9 commit 4a30c14
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/sso_push_credential.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def credentials(application)
user.grant_application_permissions(application, PERMISSIONS)

user.authorisations
.not_expired
.create_with(expires_in: 10.years)
.find_or_create_by!(application_id: application.id).token
end
Expand Down
35 changes: 32 additions & 3 deletions test/lib/sso_push_credential_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ class SSOPushCredentialTest < ActiveSupport::TestCase

context "given an already authorised application" do
setup do
authorisation = @user.authorisations.create!(application_id: @application.id)
authorisation.update!(token: "foo")
@authorisation = @user.authorisations.create!(application_id: @application.id)
end

should "return the bearer token for an already-authorized application" do
bearer_token = SSOPushCredential.credentials(@application)
assert_equal "foo", bearer_token
assert_equal @authorisation.token, bearer_token
end

should "create required application permissions if they do not already exist" do
Expand All @@ -38,6 +37,36 @@ class SSOPushCredentialTest < ActiveSupport::TestCase
end
end

context "given an application with a revoked authorisation" do
setup do
@user.authorisations.create!(application_id: @application.id, revoked_at: Time.current)
end

should "create a new authorisation to replace the revoked one" do
bearer_token = SSOPushCredential.credentials(@application)

new_authorisation = @user.authorisations.find_by(token: bearer_token)
assert_nil new_authorisation.revoked_at
assert_equal @application.id, new_authorisation.application_id
end
end

context "given an application with an expired authorisation" do
setup do
travel(-1.day) do
@user.authorisations.create!(application_id: @application.id, expires_in: 0)
end
end

should "create a new authorisation to replace the expired one" do
bearer_token = SSOPushCredential.credentials(@application)

new_authorisation = @user.authorisations.find_by(token: bearer_token)
assert new_authorisation.expires_at > Time.current
assert_equal @application.id, new_authorisation.application_id
end
end

should "create an authorisation if one does not already exist" do
assert_equal 0, @user.authorisations.count

Expand Down

0 comments on commit 4a30c14

Please sign in to comment.