-
Notifications
You must be signed in to change notification settings - Fork 480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LTI account linking controller and service #58464
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ba1dcfc
WIP
carl-codeorg b3a911b
WIP tests
carl-codeorg 8cfdf2b
tests
carl-codeorg 246134b
add trailing newline
carl-codeorg fc7e04f
PR feedback round 1
carl-codeorg 8b4343e
use attr_reader in service
carl-codeorg b6e1a2b
PR feedback round 2
carl-codeorg 99b2406
Update dashboard/lib/services/lti/account_linker.rb
carl-codeorg 61f7355
Update dashboard/app/views/lti/v1/account_linking/existing_account.ht…
carl-codeorg 8ec4d6f
Update dashboard/app/views/lti/v1/account_linking/existing_account.ht…
carl-codeorg 9215909
consolidate setup blocks
carl-codeorg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
dashboard/app/controllers/lti/v1/account_linking_controller.rb
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,34 @@ | ||
module Lti | ||
module V1 | ||
class AccountLinkingController < ApplicationController | ||
before_action :lti_account_linking_enabled? | ||
|
||
# GET /lti/v1/account_linking/landing | ||
def landing | ||
end | ||
|
||
# GET /lti/v1/account_linking/existing_account | ||
def existing_account | ||
@user = User.new_with_session(ActionController::Parameters.new, session) | ||
end | ||
|
||
# POST /lti/v1/account_linking/link_email | ||
def link_email | ||
head :bad_request unless PartialRegistration.in_progress?(session) | ||
params.require([:email, :password]) | ||
existing_user = User.find_by_email_or_hashed_email(params[:email]) | ||
if existing_user&.valid_password?(params[:password]) | ||
Services::Lti::AccountLinker.call(user: existing_user, session: session) | ||
sign_in existing_user | ||
redirect_to home_path | ||
else | ||
head :unauthorized | ||
end | ||
end | ||
|
||
private def lti_account_linking_enabled? | ||
head :not_found unless DCDO.get('lti_account_linking_enabled', false) | ||
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
30 changes: 30 additions & 0 deletions
30
dashboard/app/views/lti/v1/account_linking/existing_account.html.haml
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,30 @@ | ||
!!! | ||
%html{lang: "en"} | ||
%body | ||
%h1 Connect Existing Account | ||
|
||
= form_with(url: lti_v1_account_linking_link_email_path, html: {class: "signup"}) do |f| | ||
.row | ||
.span3.signup-field-label | ||
= f.label :email do | ||
= succeed ' *'.html_safe do | ||
= User.human_attribute_name(:email) | ||
- if @user.errors[:email].present? | ||
%p.error= @user.errors[:email]&.first | ||
.span4 | ||
= f.email_field :email, maxlength: 255, required: true | ||
|
||
/ Password | ||
.row | ||
.span3.signup-field-label | ||
= f.label :password | ||
- if @user.errors[:password].present? | ||
%p.error= @user.errors[:password]&.first | ||
.span4 | ||
= f.password_field :password, maxlength: 255, required: true | ||
|
||
/ Submit | ||
.row | ||
.span8 | ||
%button.submit{:id => 'connect_form_submit'}= 'Connect Existing Account' |
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,7 @@ | ||
!!! | ||
%html{lang: "en"} | ||
%body | ||
%h1 Welcome to Code.org! | ||
%p Looks like this is your first time logging in from Canvas. Seclect one of the options below to continue. | ||
%a.btn.btn-primary{href: new_user_registration_url} I am new to Code.org | ||
%a.btn.btn-primary{href: lti_v1_account_linking_existing_account_path} I have an existing Code.org account |
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,25 @@ | ||
module Services | ||
module Lti | ||
class AccountLinker < Services::Base | ||
attr_reader :user, :session | ||
|
||
def initialize(user:, session:) | ||
@user = user | ||
@session = session | ||
end | ||
|
||
def call | ||
ActiveRecord::Base.transaction do | ||
user.authentication_options << rehydrated_user.authentication_options.first | ||
Services::Lti.create_lti_user_identity(user) | ||
user.lti_roster_sync_enabled = true if user.teacher? | ||
PartialRegistration.delete(session) | ||
end | ||
end | ||
|
||
private def rehydrated_user | ||
@rehydrated_user ||= ::User.new_with_session(ActionController::Parameters.new, session) | ||
end | ||
end | ||
end | ||
end |
51 changes: 51 additions & 0 deletions
51
dashboard/test/controllers/lti/v1/account_linking_controller_test.rb
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' | ||
|
||
class Lti::V1::AccountLinkingControllerTest < ActionController::TestCase | ||
setup do | ||
@user = create(:teacher, email: 'test@lti.com') | ||
@lti_integration = create :lti_integration | ||
DCDO.stubs(:get) | ||
DCDO.stubs(:get).with('lti_account_linking_enabled', false).returns(true) | ||
end | ||
|
||
test 'links an LTI login to an existing account' do | ||
partial_lti_teacher = create :teacher | ||
fake_id_token = {iss: @lti_integration.issuer, aud: @lti_integration.client_id, sub: 'foo'} | ||
auth_id = Services::Lti::AuthIdGenerator.new(fake_id_token).call | ||
ao = AuthenticationOption.new( | ||
authentication_id: auth_id, | ||
credential_type: AuthenticationOption::LTI_V1, | ||
email: @user.email, | ||
) | ||
partial_lti_teacher.authentication_options = [ao] | ||
PartialRegistration.persist_attributes session, partial_lti_teacher | ||
User.any_instance.stubs(:valid_password?).returns(true) | ||
|
||
post :link_email, params: {email: @user.email, password: 'password'} | ||
assert_redirected_to home_path | ||
assert Policies::Lti.lti?(@user) | ||
end | ||
|
||
test 'fails if the password is wrong' do | ||
PartialRegistration.stubs(:in_progress?).returns(true) | ||
|
||
post :link_email, params: {email: @user.email, password: 'password'} | ||
assert_response :unauthorized | ||
end | ||
|
||
test 'blocks access if lti_account_linking_enabled is false' do | ||
sign_in @user | ||
DCDO.expects(:get).with('lti_account_linking_enabled', false).times(3).returns(false) | ||
get :existing_account | ||
assert_response :not_found | ||
get :landing | ||
assert_response :not_found | ||
post :link_email, params: {email: @user.email, password: 'password'} | ||
assert_response :not_found | ||
end | ||
|
||
test 'allows access if lti_account_linking_enabled is true' do | ||
get :existing_account | ||
assert_response :ok | ||
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,25 @@ | ||
require 'test_helper' | ||
|
||
class Services::Lti::AccountLinkerTest < ActiveSupport::TestCase | ||
setup do | ||
@user = create :teacher | ||
@session = {} | ||
@lti_integration = create :lti_integration | ||
end | ||
|
||
test 'reassigns an auth option from a partial registration to an existing user' do | ||
partial_lti_teacher = create :teacher | ||
ao = create :lti_authentication_option | ||
fake_id_token = {iss: @lti_integration.issuer, aud: @lti_integration.client_id, sub: 'foo'} | ||
auth_id = Services::Lti::AuthIdGenerator.new(fake_id_token).call | ||
ao.update(authentication_id: auth_id) | ||
partial_lti_teacher.authentication_options = [ao] | ||
PartialRegistration.persist_attributes @session, partial_lti_teacher | ||
assert_equal 1, @user.authentication_options.count | ||
refute Policies::Lti.lti?(@user) | ||
|
||
Services::Lti::AccountLinker.call(user: @user, session: @session) | ||
assert_equal 2, @user.reload.authentication_options.count | ||
assert Policies::Lti.lti?(@user) | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If these params are missing, it will throw an exception. Do we care about handling that exception and letting the user know they have a missing param, or are we ok with just returning
:unauthorized
in theelse
block?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah we'll definitely want to notify the user, but I was thinking we can be added later when we finalize the UI page that will post to this route. The front-end should be able to handle this with the usual "X field is required" messaging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll also need to gracefully handle unauthorized responses for incorrect passwords