Skip to content
This repository was archived by the owner on Jun 30, 2024. It is now read-only.

Commit f5be9fd

Browse files
committed
Fix: Port LTI to use oauth2.
1 parent 3cb36dd commit f5be9fd

File tree

3 files changed

+20
-656
lines changed

3 files changed

+20
-656
lines changed

controllers/lti.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import uuid
2+
import six
23

34

45
from rs_grading import _try_to_send_lti_grade
5-
import oauth
6-
import oauth_store
6+
import oauth2
77

88

99
# For some reason, URL query parameters are being processed twice by Canvas and returned as a list, like [23, 23]. So, just take the first element in the list.
@@ -63,20 +63,26 @@ def index():
6363
masterapp = 'welcome'
6464
session.connect(request, response, masterapp=masterapp, db=db)
6565

66-
oauth_server = oauth.OAuthServer(oauth_store.LTI_OAuthDataStore(myrecord.consumer,myrecord.secret))
67-
oauth_server.add_signature_method(oauth.OAuthSignatureMethod_PLAINTEXT())
68-
oauth_server.add_signature_method(oauth.OAuthSignatureMethod_HMAC_SHA1())
69-
70-
# Use ``setting.lti_uri`` if it's defined; otherwise, use the current URI (which must be built from its components). Don't include query parameters, which causes a filure in OAuth security validation.
71-
full_uri = settings.get('lti_uri',
72-
'{}://{}{}'.format(request.env.wsgi_url_scheme,
73-
request.env.http_host, request.url))
74-
oauth_request = oauth.OAuthRequest.from_request('POST', full_uri, None,
75-
dict(request.vars), query_string=request.env.query_string)
66+
oauth_server = oauth2.Server()
67+
oauth_server.add_signature_method(oauth2.SignatureMethod_PLAINTEXT())
68+
oauth_server.add_signature_method(oauth2.SignatureMethod_HMAC_SHA1())
69+
70+
# Use ``setting.lti_uri`` if it's defined; otherwise, use the current URI (which must be built from its components). Don't include query parameters, which causes a failure in OAuth security validation.
71+
full_uri = settings.get('lti_uri', '{}://{}{}'.format(
72+
request.env.wsgi_url_scheme, request.env.http_host, request.url
73+
))
74+
oauth_request = oauth2.Request.from_request(
75+
'POST', full_uri, None, dict(request.vars),
76+
query_string=request.env.query_string
77+
)
78+
# Fix encoding -- the signed keys are in bytes, but the oauth2 Request constructor translates everything to a string. Therefore, they never compare as equal. ???
79+
if isinstance(oauth_request.get('oauth_signature'), six.string_types):
80+
oauth_request['oauth_signature'] = oauth_request['oauth_signature'].encode('utf-8')
81+
consumer = oauth2.Consumer(myrecord.consumer, myrecord.secret)
7682

7783
try:
78-
consumer, token, params = oauth_server.verify_request(oauth_request)
79-
except oauth.OAuthError as err:
84+
oauth_server.verify_request(oauth_request, consumer, None)
85+
except oauth2.Error as err:
8086
return dict(logged_in=False, lti_errors=["OAuth Security Validation failed:"+err.message, request.vars],
8187
masterapp=masterapp)
8288
consumer = None

0 commit comments

Comments
 (0)