def fetch_oauth_request_token(self, callback_uri): """Obtains an OAuth request token from Google's Accounts API.""" if not self.oauth_request_token: # Build and sign an OAuth request parameters = { 'oauth_consumer_key': self.oauth_consumer.key, 'oauth_timestamp': oauth.generate_timestamp(), 'oauth_nonce': oauth.generate_nonce(), 'oauth_version': oauth.OAuthRequest.version, 'oauth_callback': callback_uri, 'scope': ' '.join(self.oauth_scopes) } if self.oauth_display_name: parameters['xoauth_displayname'] = self.oauth_display_name oauth_request = oauth.OAuthRequest( 'POST', OAUTH_REQUEST_TOKEN_URI, parameters ) oauth_request.sign_request( self._oauth_signature_method_hmac_sha1, self.oauth_consumer, token=None ) response = self.fetch_oauth_response(oauth_request) if response.status == 200: # Create the token from the response self.oauth_request_token = oauth.OAuthToken.from_string( response.read() ) else: raise Exception('Failed to obtain request token:\n' + response.read()) return self.oauth_request_token
def fetch_oauth_access_token(self, verifier=None, token=None): """Obtains an OAuth access token from Google's Accounts API.""" if not self.oauth_access_token: if not token: token = self.oauth_request_token if not token: raise ValueError("A request token must be supplied.") # Build and sign an OAuth request parameters = { 'oauth_consumer_key': self.oauth_consumer.key, 'oauth_timestamp': oauth.generate_timestamp(), 'oauth_nonce': oauth.generate_nonce(), 'oauth_version': oauth.OAuthRequest.version, 'oauth_token': token.key, 'oauth_verifier': verifier } oauth_request = oauth.OAuthRequest( 'POST', OAUTH_ACCESS_TOKEN_URI, parameters ) oauth_request.sign_request( self._oauth_signature_method_hmac_sha1, self.oauth_consumer, token=token ) response = self.fetch_oauth_response(oauth_request) if response.status == 200: # Create the token from the response self.oauth_access_token = oauth.OAuthToken.from_string( response.read() ) else: raise Exception('Failed to obtain access token:\n' + response.read()) return self.oauth_access_token
def from_consumer_and_token(cls, oauth_consumer, token=None, callback=None, verifier=None, http_method=oauth.HTTP_METHOD, http_url=None, parameters=None): """ Overridden to allow optional oauth_callback and oauth_verifier """ if not parameters: parameters = {} defaults = { 'oauth_consumer_key': oauth_consumer.key, 'oauth_timestamp': oauth.generate_timestamp(), 'oauth_nonce': oauth.generate_nonce(), 'oauth_version': oauth.OAuthRequest.version, } defaults.update(parameters) parameters = defaults if token: parameters['oauth_token'] = token.key if getattr(token, 'callback', None): parameters['oauth_callback'] = token.callback elif callback: parameters['oauth_callback'] = callback; if verifier: # 1.0a support for verifier. parameters['oauth_verifier'] = verifier elif callback: # 1.0a support for callback in the request token request. parameters['oauth_callback'] = callback return cls(http_method, http_url, parameters)
def build_access_headers(self, method, resource_url, params=None, request_token=None): """Build OAuth access headers for a future request. Args: method: The HTTP method being used (e.g. 'GET' or 'POST'). resource_url: The full url the request will be made to. params: A dictionary of parameters to add to what's already on the url. Typically, this would consist of POST parameters. Returns: A tuple of (header_dict, params) where header_dict is a dictionary of header names and values appropriate for passing into dropbox.rest.RESTClient and params is a dictionary like the one that was passed in, but augmented with oauth-related parameters as appropriate. """ if params is None: params = {} else: params = params.copy() oauth_params = { 'oauth_consumer_key': self.consumer.key, 'oauth_timestamp': oauth.generate_timestamp(), 'oauth_nonce': oauth.generate_nonce(), 'oauth_version': oauth.OAuthRequest.version, } token = request_token if request_token else self.token if token: oauth_params['oauth_token'] = token.key params.update(oauth_params) oauth_request = oauth.OAuthRequest.from_request(method, resource_url, parameters=params) oauth_request.sign_request(self.signature_method, self.consumer, token) return oauth_request.to_header(), params
def sign_request(self, consumer, signature_method): """Add oauth parameters and sign the request with the given method. Args: consumer: The OAuthConsumer set with a key and secret. signature_method: A supported method for signing the built request. """ params = { 'oauth_consumer_key': consumer.key, 'oauth_timestamp': oauth.generate_timestamp(), 'oauth_nonce': oauth.generate_nonce(), 'oauth_version': oauth.OAuthRequest.version, } # PHP OAuth library contains a bug which interferes with signing. Since # some containers use this library, we will implement a workaround here. if self.use_body_as_signing_parameter: params[self.get_post_body()] = "" else: # Otherwise, use the oauth_body_hash extension to sign the request body. if self.post_body: if VERBOSE > 0: logging.info("post_body => %s" % str(self.post_body)) body_hash = b64encode(hashlib.sha1(self.get_post_body()).digest()) params['oauth_body_hash'] = body_hash if self.get_security_token(): self.set_parameter("xoauth_requestor_id", None) self.set_parameters(params) if VERBOSE > 0: key, raw = signature_method.build_signature_base_string( self.oauth_request, consumer, None) logging.info("build_signature key => %s" % key) logging.info("build_signature raw => %s" % raw) self.oauth_request.sign_request(signature_method, consumer, None)
def on_buzz_auth_clicked(self, widget, data=None): self.winsize = self.window.get_size() web = webkit.WebView() web.get_settings().set_property("enable-plugins", False) web.load_html_string(_("<p>Please wait...</p>"), "file:///") self.consumer = oauth.OAuthConsumer("anonymous", "anonymous") params = { "oauth_consumer_key": self.consumer.key, "oauth_timestamp": oauth.generate_timestamp(), "oauth_nonce": oauth.generate_nonce(), "oauth_version": oauth.OAuthRequest.version, "oauth_callback": "http://gwibber.com/0/auth.html", "scope": "https://www.googleapis.com/auth/buzz", } request = oauth.OAuthRequest("POST", "https://www.google.com/accounts/OAuthGetRequestToken", params) request.sign_request(sigmeth, self.consumer, token=None) tokendata = urllib2.urlopen(request.http_url, request.to_postdata()).read() self.token = oauth.OAuthToken.from_string(tokendata) url = "https://www.google.com/accounts/OAuthAuthorizeToken?oauth_token=" + self.token.key web.open(url) web.set_size_request(450, 340) web.connect("title-changed", self.on_buzz_auth_title_change) scroll = gtk.ScrolledWindow() scroll.add(web) self.pack_start(scroll, True, True, 0) self.show_all() self.ui.get_object("vbox1").hide() self.ui.get_object("expander1").hide()
from acj.sqlalchemy_acj import db_session, LTIInfo, Course, User, Enrollment from acj.general import commit from werkzeug import urls import oauth.oauth as oauth import hmac import base64 import hashlib import requests import xml.etree.ElementTree as ET from acj.users import import_users from acj.course import enrol_users #get all courses to update courses = LTIInfo.query.all() for course in courses: #iterate through courses and get their membership info via LTI timestamp = oauth.generate_timestamp() nonce = oauth.generate_nonce(16) postParams = {} postParams['lti_message_type'] = 'basic-lis-readmembershipsforcontext' postParams['id'] = course.LTIid postParams['lti_version'] = 'LTI-1p0' postParams['oauth_consumer_key'] = 'LTI_ACJ' postParams['oauth_callback'] = 'about:blank' postParams['oauth_version'] = '1.0' postParams['oauth_signature_method'] = 'HMAC-SHA1' postParams['oauth_timestamp'] = timestamp postParams['oauth_nonce'] = nonce req = oauth.OAuthRequest(http_url=course.LTIURL, http_method='POST', parameters=postParams)
from acj.sqlalchemy_acj import db_session, LTIInfo, Course, User, Enrollment from acj.general import commit from werkzeug import urls import oauth.oauth as oauth import hmac import base64 import hashlib import requests import xml.etree.ElementTree as ET from acj.users import import_users from acj.course import enrol_users #get all courses to update courses = LTIInfo.query.all() for course in courses: #iterate through courses and get their membership info via LTI timestamp = oauth.generate_timestamp() nonce = oauth.generate_nonce(16) postParams = {} postParams['lti_message_type'] = 'basic-lis-readmembershipsforcontext' postParams['id'] = course.LTIid postParams['lti_version'] = 'LTI-1p0' postParams['oauth_consumer_key'] = 'LTI_ACJ' postParams['oauth_callback'] = 'about:blank' postParams['oauth_version'] = '1.0' postParams['oauth_signature_method'] = 'HMAC-SHA1' postParams['oauth_timestamp'] = timestamp postParams['oauth_nonce'] = nonce req = oauth.OAuthRequest(http_url=course.LTIURL, http_method='POST', parameters=postParams) hmacAlg = hmac.HMAC('acjsecret&', urls.url_quote_plus(req.get_normalized_http_method()) + '&' + urls.url_quote_plus(course.LTIURL) + '&' + urls.url_quote_plus(req.get_normalized_parameters()), hashlib.sha1)