This repository has been archived by the owner on Jul 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FIX: store last_refresh and aal in database per session
- Loading branch information
Showing
7 changed files
with
103 additions
and
110 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module MozillaIAM | ||
class SessionData < ActiveRecord::Base | ||
|
||
TOKEN_COOKIE = Auth::DefaultCurrentUserProvider::TOKEN_COOKIE | ||
|
||
def self.find_or_create(session, cookies) | ||
auth_token = cookies[TOKEN_COOKIE] | ||
user_token = UserAuthToken.lookup(auth_token) | ||
|
||
if session[:mozilla_iam] | ||
session_data = create!( | ||
user_auth_token_id: user_token.id, | ||
last_refresh: session[:mozilla_iam][:last_refresh], | ||
aal: session[:mozilla_iam][:aal] | ||
) | ||
|
||
session.delete(:mozilla_iam) | ||
session_data | ||
else | ||
user_token.mozilla_iam_session_data | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
UserAuthToken.class_eval do | ||
has_one :mozilla_iam_session_data, | ||
class_name: "MozillaIAM::SessionData", | ||
dependent: :destroy | ||
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,9 @@ | ||
class CreateSessionData < ActiveRecord::Migration[5.0] | ||
def change | ||
create_table :mozilla_iam_session_data do |t| | ||
t.integer :user_auth_token_id, null: false | ||
t.datetime :last_refresh | ||
t.string :aal | ||
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
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,49 @@ | ||
require_relative '../iam_helper' | ||
|
||
describe MozillaIAM::SessionData do | ||
it "is destroyed when associated auth token is destroyed" do | ||
|
||
end | ||
|
||
describe ".find_or_create" do | ||
let!(:last_refresh) { Time.now.round } | ||
let(:aal) { "MEDIUM" } | ||
let(:cookies) { { described_class::TOKEN_COOKIE => "12345" } } | ||
let(:user_auth_token) { UserAuthToken.generate!(user_id: Fabricate(:user).id) } | ||
before { UserAuthToken.expects(:lookup).with("12345").returns(user_auth_token) } | ||
|
||
context "with session[:mozilla_iam]" do | ||
it "should create SessionData record and remove session[:mozilla_iam]" do | ||
session = { | ||
mozilla_iam: { | ||
last_refresh: last_refresh, | ||
aal: aal | ||
} | ||
} | ||
|
||
session_data = described_class.find_or_create(session, cookies) | ||
|
||
expect(user_auth_token.mozilla_iam_session_data).to eq session_data | ||
expect(session_data.last_refresh).to eq last_refresh | ||
expect(session_data.aal).to eq aal | ||
expect(session[:mozilla_iam]).to be_nil | ||
end | ||
end | ||
|
||
context "without session[:mozilla_iam]" do | ||
it "should find SessionData record" do | ||
described_class.create!( | ||
user_auth_token_id: user_auth_token.id, | ||
last_refresh: last_refresh, | ||
aal: aal | ||
) | ||
|
||
session_data = described_class.find_or_create({}, cookies) | ||
|
||
expect(user_auth_token.mozilla_iam_session_data).to eq session_data | ||
expect(session_data.last_refresh).to eq last_refresh | ||
expect(session_data.aal).to eq aal | ||
end | ||
end | ||
end | ||
end |