-
Notifications
You must be signed in to change notification settings - Fork 897
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14947 from imtayadeway/api/add-sql-store-to-token…
…-store Add SQL store option to token store (cherry picked from commit ffd8972) https://bugzilla.redhat.com/show_bug.cgi?id=1460348
- Loading branch information
Showing
8 changed files
with
285 additions
and
84 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
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,2 @@ | ||
:server: | ||
:session_store: memory |
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,39 @@ | ||
class TokenStore | ||
class SqlStore | ||
def initialize(options) | ||
@namespace = options.fetch(:namespace) | ||
end | ||
|
||
def write(token, data, _options = nil) | ||
record = Session.find_or_create_by(:session_id => session_key(token)) | ||
record.data = Base64.encode64(Marshal.dump(data)) | ||
record.save! | ||
end | ||
|
||
def read(token, _options = nil) | ||
record = Session.find_by(:session_id => session_key(token)) | ||
return nil unless record | ||
data = Marshal.load(Base64.decode64(record.data)) | ||
if data[:expires_on] > Time.zone.now | ||
data | ||
else | ||
record.destroy | ||
nil | ||
end | ||
end | ||
|
||
def delete(token) | ||
record = Session.find_by(:session_id => session_key(token)) | ||
return nil unless record | ||
record.destroy! | ||
end | ||
|
||
private | ||
|
||
attr_reader :namespace | ||
|
||
def session_key(token) | ||
"#{namespace}:#{token}" | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
RSpec.describe TokenStore::SqlStore do | ||
around { |example| Timecop.freeze { example.run } } | ||
|
||
describe "#write" do | ||
it "creates a session" do | ||
store = build_sql_store | ||
token = SecureRandom.hex | ||
data = { | ||
:expires_on => 1.hour.from_now, | ||
:token_ttl => 1.hour, | ||
:userid => "alice" | ||
} | ||
|
||
expect { store.write(token, data) }.to change(Session, :count).by(1) | ||
end | ||
|
||
it "updates a session if it exists" do | ||
store = build_sql_store("TEST") | ||
token = SecureRandom.hex | ||
session = FactoryGirl.create( | ||
:session, | ||
:session_id => "TEST:#{token}", | ||
:data => serialize_data(:expires_on => 1.second.from_now) | ||
) | ||
|
||
store.write(token, :expires_on => 1.hour.from_now) | ||
|
||
expect(deserialize_data(session.reload.data)[:expires_on]).to eq(1.hour.from_now) | ||
end | ||
end | ||
|
||
describe "#read" do | ||
it "reads a valid token" do | ||
store = build_sql_store("TEST") | ||
token = SecureRandom.hex | ||
FactoryGirl.create( | ||
:session, | ||
:session_id => "TEST:#{token}", | ||
:data => serialize_data(:userid => "alice", :expires_on => 1.second.from_now) | ||
) | ||
|
||
data = store.read(token) | ||
|
||
expect(data[:userid]).to eq("alice") | ||
end | ||
|
||
it "returns nil for an expired token" do | ||
store = build_sql_store("TEST") | ||
token = SecureRandom.hex | ||
FactoryGirl.create( | ||
:session, | ||
:session_id => "TEST:#{token}", | ||
:data => serialize_data(:userid => "alice", :expires_on => 1.second.ago) | ||
) | ||
|
||
data = store.read(token) | ||
|
||
expect(data).to be_nil | ||
end | ||
|
||
it "returns nil if no token can be found" do | ||
store = build_sql_store("TEST") | ||
token = SecureRandom.hex | ||
|
||
data = store.read(token) | ||
|
||
expect(data).to be_nil | ||
end | ||
end | ||
|
||
describe "#delete" do | ||
it "deletes a token" do | ||
store = build_sql_store("TEST") | ||
token = SecureRandom.hex | ||
FactoryGirl.create( | ||
:session, | ||
:session_id => "TEST:#{token}", | ||
:data => serialize_data(:userid => "alice", :expires_on => 1.hour.from_now) | ||
) | ||
|
||
expect { store.delete(token) }.to change(Session, :count).by(-1) | ||
end | ||
end | ||
|
||
def serialize_data(data) | ||
Base64.encode64(Marshal.dump(data)) | ||
end | ||
|
||
def deserialize_data(data) | ||
Marshal.load(Base64.decode64(data)) | ||
end | ||
|
||
def build_sql_store(namespace = "FOO") | ||
described_class.new(:namespace => namespace) | ||
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
Oops, something went wrong.