openedx / xblock-lti-consumer

GNU Affero General Public License v3.0
28 stars 80 forks source link

`user_id` not set and send as expected #217

Open sroertgen opened 2 years ago

sroertgen commented 2 years ago

Hello 👋,

I am running the tutor distribution of Open Edx (https://docs.tutor.overhang.io/) and trying to add an LTI component.

I followed these steps: https://edx.readthedocs.io/projects/edx-partner-course-staff/en/latest/exercises_tools/lti_component.html#enabling-and-using-lti-advantage-features

For the LTI component I want to pass the edx username to the LTI component. So here https://my-edx.de/admin/xblock_config/courseeditltifieldsenabledflag/ I added the respective course, which gives me the opportunity to set the flag "Request user's username" to True in my course.

Following your guide to test LTI (https://github.com/edx/xblock-lti-consumer#lti-11) I always get user_id=student and not the current edx user. Am I still missing something here? What do I have to configure to get the current user passed on to my LTI component?

Thank you!

giovannicimolin commented 2 years ago

Hi @sroertgen,

Which release of the Open edX platform are you running? On more recent versions, the PII WaffleFlag has been replaced by a unified flag for PII sharing (read more about it on Unified Flag for Enabling Sharing of PII in LTI (ADR)).

Can you try enabling that flag if it's available on your instance?

sroertgen commented 2 years ago

Hello @giovannicimolin ,

thanks for the fast response! I'm running openedx:12.2.0 .

I added my course to CourseEditLTIFieldsEnabledFlag in the below depicted menu:

Screenshot 2021-12-21 at 15 13 16

That gave me the opportunity to set the ask_to_send_username flag to True.

Still I only get username=student.

sroertgen commented 2 years ago

Hello and a happy and successful new year!

Do you have some tips on debugging this issue? I'm quite new to openedx and still digging my way through.

If you have suggestions on how I can debug the LTI parameters and see where this goes wrong, I will be very grateful! Thank you!

sroertgen commented 2 years ago

Looking at the studio logs, I get a lot deprecation warnings, so it might have something to do with that? e.g.

edx.devstack.studio               | 2022-01-04 15:30:41,518 WARNING 610 [py.warnings] [user 3] [ip 172.25.0.1] warnings.py:109 - /edx/src/xblock-lti-consumer/lti_consumer/lti_xblock.py:884: DeprecationWarning: runtime.get_real_user is deprecated. Please use the user service instead.
edx.devstack.studio               |   if callable(self.runtime.get_real_user):
edx.devstack.studio               |
edx.devstack.studio               | 2022-01-04 15:30:41,543 WARNING 610 [py.warnings] [user 3] [ip 172.25.0.1] warnings.py:109 - /edx/src/xblock-lti-consumer/lti_consumer/lti_xblock.py:885: DeprecationWarning: runtime.get_real_user is deprecated. Please use the user service instead.
edx.devstack.studio               |   real_user_object = self.runtime.get_real_user(self.runtime.anonymous_student_id)
edx.devstack.studio               |
edx.devstack.studio               | 2022-01-04 15:30:41,546 WARNING 610 [py.warnings] [user 3] [ip 172.25.0.1] warnings.py:109 - /edx/src/xblock-lti-consumer/lti_consumer/lti_xblock.py:885: DeprecationWarning: runtime.anonymous_student_id is deprecated. Please use the user service instead.
edx.devstack.studio               |   real_user_object = self.runtime.get_real_user(self.runtime.anonymous_student_id)
edx.devstack.studio               |

Just thinking out loud here and further investigating...

sroertgen commented 2 years ago

So i found out the following, changing these lines:

https://github.com/openedx/xblock-lti-consumer/blob/582597a6a7fa758be5b1a32511c4f8acd116394b/lti_consumer/lti_xblock.py#L686-L694

to:

    @property
    def user_id(self):
        """
        Returns the opaque anonymous_student_id for the current user.
        """
        user_id = self.runtime.service(self, 'user').get_current_user().opt_attrs['edx-platform.user_id']
        if user_id is None:
            raise LtiError(self.ugettext("Could not get user id for current request"))
        return str(user_id)

gives me the current user_id as expected.

(Btw I find this user service is poorly documented. In the XBlock API documentation there is just the notice that runtime.user_id is deprecated in favour of a new user service, but no links to that service: https://edx.readthedocs.io/projects/xblock/en/latest/runtime.html#xblock.runtime.Runtime.user_id)

Some searching brought me here: https://github.com/openedx/XBlock/blob/master/xblock/reference/user_service.py where some properties are explained. Maybe I just the documentation part somewhere (please send it to me, if there is some :) )

giovannicimolin commented 2 years ago

Hi @sroertgen, sorry for the delay! I was on vacation for the past few weeks and I'm catching up now. Happy new year!

I scheduled a ticket to look at this issue in my next sprint (starting Jan 11) and will get back to you once I start working on it.

sroertgen commented 2 years ago

Hey @giovannicimolin,

no problem! I hope you had a nice and relaxing time!

If you want to know, what I changed, here is my fork: https://github.com/sroertgen/xblock-lti-consumer/blob/master/lti_consumer/lti_xblock.py

Since I'm currently only interested in LTI1.1 I only changed the necessary parts to get that working.

Maybe worth to point out: I didn't know exactly, what you were meaning with opaque anonymous_student_id in your code. There seem to be two options to get a user_id from the new user service. Either a really anonymous one, which is always student. Or the user_id of the current edx-user, which is some integer (and what is what I use now).

Alain1405 commented 2 years ago

We also encountered this issue on Maple.

Alain1405 commented 2 years ago

If I understand this issue correctly, after reading Maple release note at https://edx.readthedocs.io/projects/open-edx-release-notes/en/latest/maple.html#lti-1-3-and-lti-advantage-support we have enabled admin/lti_consumer/courseallowpiisharinginltiflag/ for the course and now the user_id is sent properly to the tool.

giovannicimolin commented 2 years ago

@sroertgen

user_id = self.runtime.service(self, 'user').get_current_user().opt_attrs['edx-platform.user_id']

This is not the best approach since the method you are modifying is supposed to return the anonymous user id and not the actual user id used by the platform. You're right about the user service though, the parameter currently being used is deprecated, so the proper usage would be similar to what you've used:

user_id = self.runtime.service(self, 'user').get_current_user().opt_attrs['edx-platform.anonymous_user_id']

If you open a pull request with this change I can review and approve it. :grin:

Following your guide to test LTI (https://github.com/edx/xblock-lti-consumer#lti-11) I always get user_id=student and not the current edx user. Am I still missing something here? What do I have to configure to get the current user passed on to my LTI component? There seem to be two options to get a user_id from the new user service. Either a really anonymous one, which is always student. Or the user_id of the current edx-user, which is some integer (and what is what I use now).

You are probably testing your code using studio, the runtime on studio will always return student - since it's only a means for testing and not actually using the block. If you used the same method in the LMS (either by clicking on Preview or "View Live") you'll receive the proper anonymous ID, see:

# This is on the LMS:
(Pdb) self.runtime.anonymous_student_id
'f8ecdecbae54d761637dfa15267bd996'

# This is on Studio:
(Pdb) self.runtime.anonymous_student_id
'student'
giovannicimolin commented 2 years ago

@sroertgen Also, I checked your fork (diff here), and saw that you replaced the deprecated runtime methods with the new user service.

I can review those changes if you want to contribute them back to the platform.

sroertgen commented 2 years ago

Hey @giovannicimolin ,

thanks for clarifying!

I will be happy to contribute them back and will prepare so (but it might take till the end of the week).

Just one more quick question (being new to edx): What is your recommended devstack, which you would also use for reviewing? Is it the Open edX devstack or is there something else you can recommend?

giovannicimolin commented 2 years ago

@sroertgen I personally use the Open edX devstack (https://github.com/openedx/devstack) - with a somewhat modified networking setup to test LTI 1.3 launches. An alternative would be to use Tutor, but I never tested it as a development environment.