Skip to content

Commit

Permalink
add tests for expired/invalid sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekg committed Feb 27, 2025
1 parent 91d6845 commit 2710894
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
25 changes: 25 additions & 0 deletions features/api/v1/tokens/sessions.feature
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,31 @@ Feature: Token sessions
And the response headers should not contain "Set-Cookie"
And the current account should have 1 "session"

# expiry
Scenario: User reads their profile via session authentication
Given the current account is "test1"
And the current account has 1 "user"
And I am a user of account "test1"
And I authenticate with a session
When I send a GET request to "/accounts/test1/me"
Then the response status should be "200"

Scenario: User reads their profile via expired session authentication
Given the current account is "test1"
And the current account has 1 "user"
And I am a user of account "test1"
And I authenticate with an expired session
When I send a GET request to "/accounts/test1/me"
Then the response status should be "401"

Scenario: User creates a license via invalid session authentication
Given the current account is "test1"
And the current account has 1 "user"
And I am a user of account "test1"
And I authenticate with an invalid session
When I send a GET request to "/accounts/test1/me"
Then the response status should be "401"

# envs
Scenario: License validates itself via session authentication (isolated license in isolated env)
Given the current account is "test1"
Expand Down
49 changes: 49 additions & 0 deletions features/step_definitions/authentication_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,52 @@

header "Cookie", %(session_id=#{esc})
end

Given /^I authenticate with an expired session$/ do
@token = @bearer.tokens.first_or_create!(account: @bearer.account, bearer: @bearer)
@session = @token.sessions.create!(
expiry: 1.hour.ago,
user_agent: 'keygen/test',
ip: '127.0.0.1',
)

app = Rails.application
config = app.config
keygen = app.key_generator
salt = config.action_dispatch.authenticated_encrypted_cookie_salt
cipher = config.action_dispatch.encrypted_cookie_cipher
key_len = ActiveSupport::MessageEncryptor.key_len(cipher)
key = keygen.generate_key(salt, key_len)
encryptor = ActiveSupport::MessageEncryptor.new(key,
serializer: ActiveSupport::MessageEncryptor::NullSerializer,
cipher:,
)

dec = JSON.dump(@session.id)
enc = encryptor.encrypt_and_sign(dec, purpose: 'cookie.session_id')
esc = CGI.escape(enc)

header "Cookie", %(session_id=#{esc})
end

Given /^I authenticate with an invalid session$/ do
@token = @bearer.tokens.first_or_create!(account: @bearer.account, bearer: @bearer)

app = Rails.application
config = app.config
keygen = app.key_generator
salt = config.action_dispatch.authenticated_encrypted_cookie_salt
cipher = config.action_dispatch.encrypted_cookie_cipher
key_len = ActiveSupport::MessageEncryptor.key_len(cipher)
key = keygen.generate_key(salt, key_len)
encryptor = ActiveSupport::MessageEncryptor.new(key,
serializer: ActiveSupport::MessageEncryptor::NullSerializer,
cipher:,
)

dec = JSON.dump(SecureRandom.uuid)
enc = encryptor.encrypt_and_sign(dec, purpose: 'cookie.session_id')
esc = CGI.escape(enc)

header "Cookie", %(session_id=#{esc})
end

0 comments on commit 2710894

Please sign in to comment.