22Utility functions for working with OAuth signatures.
33"""
44
5- import logging
6- import hashlib
5+ from __future__ import absolute_import , unicode_literals
6+
77import base64
8- import urllib
8+ import hashlib
9+ import logging
910
11+ import six .moves .urllib .error
12+ import six .moves .urllib .parse
13+ import six
1014from oauthlib import oauth1
1115
1216from .exceptions import LtiError
1317
14-
1518log = logging .getLogger (__name__ )
1619
1720
18- class SignedRequest (object ):
21+ class SignedRequest (object ): # pylint: disable=bad-option-value, useless-object-inheritance
1922 """
2023 Encapsulates request attributes needed when working
2124 with the `oauthlib.oauth1` API
@@ -45,14 +48,14 @@ def get_oauth_request_signature(key, secret, url, headers, body):
4548 Returns:
4649 str: Authorization header for the OAuth signed request
4750 """
48- client = oauth1 .Client (client_key = unicode (key ), client_secret = unicode (secret ))
51+ client = oauth1 .Client (client_key = six . text_type (key ), client_secret = six . text_type (secret ))
4952 try :
5053 # Add Authorization header which looks like:
5154 # Authorization: OAuth oauth_nonce="80966668944732164491378916897",
5255 # oauth_timestamp="1378916897", oauth_version="1.0", oauth_signature_method="HMAC-SHA1",
5356 # oauth_consumer_key="", oauth_signature="frVp4JuvT1mVXlxktiAUjQ7%2F1cw%3D"
54- __ , headers , __ = client .sign (
55- unicode (url .strip ()),
57+ _ , headers , _ = client .sign (
58+ six . text_type (url .strip ()),
5659 http_method = u'POST' ,
5760 body = body ,
5861 headers = headers
@@ -83,7 +86,7 @@ def verify_oauth_body_signature(request, lti_provider_secret, service_url):
8386 """
8487
8588 headers = {
86- 'Authorization' : unicode (request .headers .get ('Authorization' )),
89+ 'Authorization' : six . text_type (request .headers .get ('Authorization' )),
8790 'Content-Type' : request .content_type ,
8891 }
8992
@@ -94,18 +97,18 @@ def verify_oauth_body_signature(request, lti_provider_secret, service_url):
9497 oauth_headers = dict (oauth_params )
9598 oauth_signature = oauth_headers .pop ('oauth_signature' )
9699 mock_request_lti_1 = SignedRequest (
97- uri = unicode ( urllib .unquote (service_url )),
98- http_method = unicode (request .method ),
99- params = oauth_headers .items (),
100+ uri = six . text_type ( six . moves . urllib . parse .unquote (service_url )),
101+ http_method = six . text_type (request .method ),
102+ params = list ( oauth_headers .items () ),
100103 signature = oauth_signature
101104 )
102105 mock_request_lti_2 = SignedRequest (
103- uri = unicode ( urllib .unquote (request .url )),
104- http_method = unicode (request .method ),
105- params = oauth_headers .items (),
106+ uri = six . text_type ( six . moves . urllib . parse .unquote (request .url )),
107+ http_method = six . text_type (request .method ),
108+ params = list ( oauth_headers .items () ),
106109 signature = oauth_signature
107110 )
108- if oauth_body_hash != oauth_headers .get ('oauth_body_hash' ):
111+ if oauth_body_hash . decode ( 'utf-8' ) != oauth_headers .get ('oauth_body_hash' ):
109112 log .error (
110113 "OAuth body hash verification failed, provided: %s, "
111114 "calculated: %s, for url: %s, body is: %s" ,
@@ -123,7 +126,7 @@ def verify_oauth_body_signature(request, lti_provider_secret, service_url):
123126 "headers:%s url:%s method:%s" ,
124127 oauth_headers ,
125128 service_url ,
126- unicode (request .method )
129+ six . text_type (request .method )
127130 )
128131 raise LtiError ("OAuth signature verification has failed." )
129132
@@ -145,18 +148,18 @@ def log_authorization_header(request, client_key, client_secret):
145148 """
146149 sha1 = hashlib .sha1 ()
147150 sha1 .update (request .body )
148- oauth_body_hash = unicode (base64 .b64encode (sha1 .digest ())) # pylint: disable=too-many-function-args
151+ oauth_body_hash = six . text_type (base64 .b64encode (sha1 .digest ())) # pylint: disable=too-many-function-args
149152 log .debug ("[LTI] oauth_body_hash = %s" , oauth_body_hash )
150153 client = oauth1 .Client (client_key , client_secret )
151154 params = client .get_oauth_params (request )
152155 params .append ((u'oauth_body_hash' , oauth_body_hash ))
153156 mock_request = SignedRequest (
154- uri = unicode ( urllib .unquote (request .url )),
157+ uri = six . text_type ( six . moves . urllib . parse .unquote (request .url )),
155158 headers = request .headers ,
156159 body = u"" ,
157160 decoded_body = u"" ,
158161 oauth_params = params ,
159- http_method = unicode (request .method ),
162+ http_method = six . text_type (request .method ),
160163 )
161164 sig = client .get_oauth_signature (mock_request )
162165 mock_request .oauth_params .append ((u'oauth_signature' , sig ))
0 commit comments