-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
182 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
3.3.0 | ||
3.3.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
module Mysql2 | ||
module AwsRdsIam | ||
module AuthToken | ||
class ExpirableToken | ||
# By default token is valid for up to 15 minutes, here we expire it after 14 minutes | ||
DEFAULT_EXPIRE_AT = (15 * 60) # 15 minutes | ||
EXPIRATION_THRESHOLD = (1 * 60) # 1 minute | ||
EXPIRE_HEADER = 'x-amz-expires' | ||
|
||
def initialize(token) | ||
@token = token | ||
@created_at = now | ||
@expire_at = parse_expiration || DEFAULT_EXPIRE_AT | ||
end | ||
|
||
def value | ||
token unless expired? | ||
end | ||
|
||
private | ||
|
||
attr_reader :token, :created_at, :expire_at | ||
|
||
def expired? | ||
(now - created_at) > (expire_at - EXPIRATION_THRESHOLD) | ||
end | ||
|
||
def now | ||
Process.clock_gettime(Process::CLOCK_MONOTONIC) | ||
end | ||
|
||
def parse_expiration | ||
query = URI.parse("https://#{token}").query | ||
|
||
return nil unless query | ||
|
||
URI.decode_www_form(query) | ||
.filter_map { |(key, value)| Integer(value) if key.downcase == EXPIRE_HEADER } | ||
.first | ||
rescue StandardError | ||
nil | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require 'test_helper' | ||
|
||
module Mysql2 | ||
module AwsRdsIam | ||
module AuthToken | ||
class TestExpirableToken < Minitest::Test | ||
def setup | ||
@valid_token = "https://example.com?x-amz-expires=900" | ||
@no_expiration_token = "https://example.com?other=test" | ||
@malformed_token = "https://example.com?x-amz-expires=test" | ||
@no_query_token = "https://example.com" | ||
end | ||
|
||
def test_that_token_is_valid_when_not_expired | ||
token = ExpirableToken.new(@valid_token) | ||
Process.stub(:clock_gettime, token.send(:created_at) + 60) do | ||
assert_equal @valid_token, token.value | ||
end | ||
end | ||
|
||
def test_that_tokenis_valid_when_expiry_is_missing | ||
token = ExpirableToken.new(@no_expiration_token) | ||
Process.stub(:clock_gettime, token.send(:created_at) + 840) do | ||
assert_equal @no_expiration_token, token.value | ||
end | ||
end | ||
|
||
def test_that_tokenis_valid_when_expiry_is_invalid | ||
token = ExpirableToken.new(@malformed_token) | ||
Process.stub(:clock_gettime, token.send(:created_at) + 840) do | ||
assert_equal @malformed_token, token.value | ||
end | ||
end | ||
|
||
def test_that_tokenis_valid_when_no_query | ||
token = ExpirableToken.new(@no_query_token) | ||
Process.stub(:clock_gettime, token.send(:created_at) + 840) do | ||
assert_equal @no_query_token, token.value | ||
end | ||
end | ||
|
||
def test_that_token_is_invalid_when_expired | ||
token = ExpirableToken.new(@valid_token) | ||
Process.stub(:clock_gettime, token.send(:created_at) + 900) do | ||
assert_nil token.value | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters