-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Making the UID not include unsafe characters for routing (#1853)
external uids are the entire email, so we need to make them safe Also makes all exising uids int he database safe fixes #1823
- Loading branch information
1 parent
d476e25
commit 05c51ef
Showing
8 changed files
with
78 additions
and
8 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class SafeUid < ActiveRecord::Migration[6.1] | ||
def change | ||
# update any exisitng user to have a safe uid so that correct user will be found when the user logs in | ||
User.where(" uid like '%@%'").each do |user| | ||
safe_uid = User.safe_uid(user.uid) | ||
user.uid = safe_uid | ||
user.save | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -14,6 +14,17 @@ | |
end | ||
end | ||
|
||
context "a guest cas user" do | ||
it "redirects to home page with success notice" do | ||
allow(User).to receive(:from_cas) { FactoryBot.create(:user, uid: "[email protected]", email: "[email protected]@princeton.edu") } | ||
get :cas | ||
expect(response).to redirect_to(root_path) | ||
expect(flash[:notice]).to eq("Successfully authenticated from Princeton Central Authentication Service account.") | ||
expect(User.first.email).to eq("[email protected]@princeton.edu") | ||
expect(User.first.uid).to eq("test_user_example_com") | ||
end | ||
end | ||
|
||
context "invalid user" do | ||
it "redirects to home page with warning notice" do | ||
allow(User).to receive(:from_cas) { nil } | ||
|
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 |
---|---|---|
|
@@ -19,6 +19,13 @@ | |
expect(response).to render_template("show") | ||
end | ||
|
||
it "renders the show page for external users" do | ||
# Notice that for external users like "[email protected]" Rails splits the ".com" in the URL | ||
sign_in user_external | ||
get :show, params: { id: user_external.friendly_id } | ||
expect(response).to render_template("show") | ||
end | ||
|
||
describe "#edit" do | ||
context "when authenticated and the current user is authorized" do | ||
before do | ||
|
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 |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
let(:access_token) { OmniAuth::AuthHash.new(provider: "cas", uid: "who", extra: { mail: "[email protected]" }) } | ||
let(:access_token_pppl) { OmniAuth::AuthHash.new(provider: "cas", uid: "who", extra: { mail: "[email protected]", departmentnumber: "31000" }) } | ||
let(:access_token_super_admin) { OmniAuth::AuthHash.new(provider: "cas", uid: "fake1", extra: { mail: "[email protected]" }) } | ||
let(:access_token_guest) { OmniAuth::AuthHash.new(provider: "cas", uid: "[email protected]", extra: { mail: "[email protected]@princeton.edu" }) } | ||
|
||
let(:access_token_full_extras) do | ||
OmniAuth::AuthHash.new(provider: "cas", uid: "test123", | ||
|
@@ -72,6 +73,14 @@ | |
expect(user.given_name).to eq "Who" | ||
expect(user.family_name).to eq "Areyou" | ||
end | ||
|
||
context "a guest cas user" do | ||
it "redirects to home page with success notice" do | ||
user = described_class.from_cas(access_token_guest) | ||
expect(user.email).to eq "[email protected]@princeton.edu" | ||
expect(user.uid).to eq "test_user_example_com" | ||
end | ||
end | ||
end | ||
|
||
describe "#super_admin?" do | ||
|
@@ -281,4 +290,17 @@ | |
expect(user.full_name_safe).to eq(user.uid) | ||
end | ||
end | ||
|
||
describe "#uid" do | ||
it "updates the uid" do | ||
user = User.new(uid: "[email protected]@princeton.edu") | ||
expect(user.uid).to eq("test_abc_example_com_princeton_edu") | ||
end | ||
|
||
it "doesn't update the uid after initialize" do | ||
user = User.new | ||
user.uid = "[email protected]@princeton.edu" | ||
expect(user.uid).to eq("[email protected]@princeton.edu") | ||
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