gdata.auth | index /usr/local/google/home/afshar/src/external-gdata-release/google3/src/gdata/auth.py

`# Copyright (C) 2007 - 2009 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.`

Modules

``

atom cgi gdata.tlslite.utils.cryptomath gdata | gdata.tlslite.utils.keyfactory math gdata.oauth gdata.oauth.rsa | random re time types | urllib

Classes

``

__builtin__.object
  OAuthInputParams
  OAuthSignatureMethod
atom.http_interface.GenericToken(__builtin__.object)
  ClientLoginToken
    AuthSubToken
      SecureAuthSubToken
  OAuthToken

class AuthSubToken(ClientLoginToken)

``

Method resolution order:
  AuthSubToken
  ClientLoginToken
  atom.http_interface.GenericToken
  __builtin__.object

Methods defined here:

get_token_string(self)
  `Removes AUTHSUB_AUTH_LABEL to give just the token value.`

set_token_string(self, token_string)

Methods inherited from ClientLoginToken:

__init__(self, auth_header=None, scopes=None)

__str__(self)

perform_request(self, http_client, operation, url, data=None, headers=None)
  `Sets the Authorization header and makes the HTTP request.`

valid_for_scope(self, url)
  `Tells the caller if the token authorizes access to the desired URL.`

Data descriptors inherited from atom.http_interface.GenericToken:

__dict__
  `dictionary for instance variables (if defined)`

__weakref__
  `list of weak references to the object (if defined)`

class ClientLoginToken(atom.http_interface.GenericToken)

``

`Stores the Authorization header in auth_header and adds to requests.
This token will add it's Authorization header to an HTTP request
as it is made. Ths token class is simple but
some Token classes must calculate portions of the Authorization header
based on the request being made, which is why the token is responsible
for making requests via an http_client parameter.
Args:
auth_header: str The value for the Authorization header.
scopes: list of str or atom.url.Url specifying the beginnings of URLs
for which this token can be used. For example, if scopes contains
'http://example.com/foo', then this token can be used for a request to
'http://example.com/foo/bar' but it cannot be used for a request to
'http://example.com/baz'
`

Method resolution order:
  ClientLoginToken
  atom.http_interface.GenericToken
  __builtin__.object

Methods defined here:

__init__(self, auth_header=None, scopes=None)

__str__(self)

get_token_string(self)
  `Removes PROGRAMMATIC_AUTH_LABEL to give just the token value.`

perform_request(self, http_client, operation, url, data=None, headers=None)
  `Sets the Authorization header and makes the HTTP request.`

set_token_string(self, token_string)

valid_for_scope(self, url)
  `Tells the caller if the token authorizes access to the desired URL.`

Data descriptors inherited from atom.http_interface.GenericToken:

__dict__
  `dictionary for instance variables (if defined)`

__weakref__
  `list of weak references to the object (if defined)`

class OAuthInputParams(__builtin__.object)
`` | `Stores OAuth input parameters. This class is a store for OAuth input parameters viz. consumer key and secret, signature method and RSA key.`

Methods defined here:

GetConsumer(self)
  `Gets the OAuth consumer.
  Returns:
  object of type <oauth.oauth.Consumer>`

GetSignatureMethod(self)
  `Gets the OAuth signature method.
  Returns:
  object of supertype <oauth.oauth.OAuthSignatureMethod>`

__init__(self, signature_method, consumer_key, consumer_secret=None, rsa_key=None, requestor_id=None)
  `Initializes object with parameters required for using OAuth mechanism.
  NOTE: Though consumer_secret and rsa_key are optional, either of the two
  is required depending on the value of the signature_method.
  Args:
  signature_method: class which provides implementation for strategy class
  oauth.oauth.OAuthSignatureMethod. Signature method to be used for
  signing each request. Valid implementations are provided as the
  constants defined by gdata.auth.OAuthSignatureMethod. Currently
  they are gdata.auth.OAuthSignatureMethod.RSA_SHA1 and
  gdata.auth.OAuthSignatureMethod.HMAC_SHA1. Instead of passing in
  the strategy class, you may pass in a string for 'RSA_SHA1' or
  'HMAC_SHA1'. If you plan to use OAuth on App Engine (or another
  WSGI environment) I recommend specifying signature method using a
  string (the only options are 'RSA_SHA1' and 'HMAC_SHA1'). In these
  environments there are sometimes issues with pickling an object in
  which a member references a class or function. Storing a string to
  refer to the signature method mitigates complications when
  pickling.
  consumer_key: string Domain identifying third_party web application.
  consumer_secret: string (optional) Secret generated during registration.
  Required only for HMAC_SHA1 signature method.
  rsa_key: string (optional) Private key required for RSA_SHA1 signature
  method.
  requestor_id: string (optional) User email adress to make requests on
  their behalf. This parameter should only be set when performing
  2 legged OAuth requests.`

Data descriptors defined here:

__dict__
  `dictionary for instance variables (if defined)`

__weakref__
  `list of weak references to the object (if defined)`

class OAuthSignatureMethod(__builtin__.object)
`` | `Holds valid OAuth signature methods. RSA_SHA1: Class to build signature according to RSA-SHA1 algorithm. HMAC_SHA1: Class to build signature according to HMAC-SHA1 algorithm.`

Data descriptors defined here:

__dict__
  `dictionary for instance variables (if defined)`

__weakref__
  `list of weak references to the object (if defined)`

Data and other attributes defined here:

HMAC_SHA1 = <class 'gdata.oauth.OAuthSignatureMethod_HMAC_SHA1'>

RSA_SHA1 = <class 'gdata.auth.RSA_SHA1'>
  `Provides implementation for abstract methods to return RSA certs.`

class OAuthToken(atom.http_interface.GenericToken)

``

`Stores the token key, token secret and scopes for which token is valid.
This token adds the authorization header to each request made. It
re-calculates authorization header for every request since the OAuth
signature to be added to the authorization header is dependent on the
request parameters.
Attributes:
key: str The value for the OAuth token i.e. token key.
secret: str The value for the OAuth token secret.
scopes: list of str or atom.url.Url specifying the beginnings of URLs
for which this token can be used. For example, if scopes contains
'http://example.com/foo', then this token can be used for a request to
'http://example.com/foo/bar' but it cannot be used for a request to
'http://example.com/baz'
oauth_input_params: OAuthInputParams OAuth input parameters.
`

Method resolution order:
  OAuthToken
  atom.http_interface.GenericToken
  __builtin__.object

Methods defined here:

GetAuthHeader(self, http_method, http_url, realm='')
  `Get the authentication header.
  Args:
  http_method: string HTTP method i.e. operation e.g. GET, POST, PUT, etc.
  http_url: string or atom.url.Url HTTP URL to which request is made.
  realm: string (default='') realm parameter to be included in the
  authorization header.
  Returns:
  dict Header to be sent with every subsequent request after
  authentication.`

__init__(self, key=None, secret=None, scopes=None, oauth_input_params=None)

__str__(self)

get_token_string(self)
  `Returns the token string.
  The token string returned is of format
  oauth_token=[0]&oauth_token_secret=[1], where [0] and [1] are some strings.
  Returns:
  A token string of format oauth_token=[0]&oauth_token_secret=[1],
  where [0] and [1] are some strings. If self.secret is absent, it just
  returns oauth_token=[0]. If self.key is absent, it just returns
  oauth_token_secret=[1]. If both are absent, it returns None.`

perform_request(self, http_client, operation, url, data=None, headers=None)
  `Sets the Authorization header and makes the HTTP request.`

set_token_string(self, token_string)
  `Sets the token key and secret from the token string.
  Args:
  token_string: str Token string of form
  oauth_token=[0]&oauth_token_secret=[1]. If oauth_token is not present,
  self.key will be None. If oauth_token_secret is not present,
  self.secret will be None.`

valid_for_scope(self, url)

Data descriptors inherited from atom.http_interface.GenericToken:

__dict__
  `dictionary for instance variables (if defined)`

__weakref__
  `list of weak references to the object (if defined)`

class SecureAuthSubToken(AuthSubToken)

``

`Stores the rsa private key, token, and scopes for the secure AuthSub token.
This token adds the authorization header to each request made. It
re-calculates authorization header for every request since the secure AuthSub
signature to be added to the authorization header is dependent on the
request parameters.
Attributes:
rsa_key: string The RSA private key in PEM format that the token will
use to sign requests
token_string: string (optional) The value for the AuthSub token.
scopes: list of str or atom.url.Url specifying the beginnings of URLs
for which this token can be used. For example, if scopes contains
'http://example.com/foo', then this token can be used for a request to
'http://example.com/foo/bar' but it cannot be used for a request to
'http://example.com/baz'
`

Method resolution order:
  SecureAuthSubToken
  AuthSubToken
  ClientLoginToken
  atom.http_interface.GenericToken
  __builtin__.object

Methods defined here:

GetAuthHeader(self, http_method, http_url)
  `Generates the Authorization header.
  The form of the secure AuthSub Authorization header is
  Authorization: AuthSub token="token" sigalg="sigalg" data="data" sig="sig"
  and data represents a string in the form
  data = http_method http_url timestamp nonce
  Args:
  http_method: string HTTP method i.e. operation e.g. GET, POST, PUT, etc.
  http_url: string or atom.url.Url HTTP URL to which request is made.
  Returns:
  dict Header to be sent with every subsequent request after authentication.`

__init__(self, rsa_key, token_string=None, scopes=None)

__str__(self)

get_token_string(self)

perform_request(self, http_client, operation, url, data=None, headers=None)
  `Sets the Authorization header and makes the HTTP request.`

set_token_string(self, token_string)

Methods inherited from ClientLoginToken:

valid_for_scope(self, url)
  `Tells the caller if the token authorizes access to the desired URL.`

Data descriptors inherited from atom.http_interface.GenericToken:

__dict__
  `dictionary for instance variables (if defined)`

__weakref__
  `list of weak references to the object (if defined)`

Functions

``

AuthSubTokenFromHttpBody(http_body)
  `Extracts the AuthSub token from an HTTP body string.
  Used to find the new session token after making a request to upgrade a
  single use AuthSub token.
  Args:
  http_body: str The repsonse from the server which contains the AuthSub
  key. For example, this function would find the new session token
  from the server's response to an upgrade token request.
  Returns:
  The header value to use for Authorization which contains the AuthSub
  token.`

AuthSubTokenFromUrl(url)
  `Extracts the AuthSub token from the URL.
  Used after the AuthSub redirect has sent the user to the 'next' page and
  appended the token to the URL. This function returns the value to be used
  in the Authorization header.
  Args:
  url: str The URL of the current page which contains the AuthSub token as
  a URL parameter.`

GenerateAuthSubUrl(next, scope, secure=False, session=True, request_url='https://www.google.com/accounts/AuthSubRequest', domain='default')
  `Generate a URL at which the user will login and be redirected back.
  Users enter their credentials on a Google login page and a token is sent
  to the URL specified in next. See documentation for AuthSub login at:
  http://code.google.com/apis/accounts/AuthForWebApps.html
  Args:
  request_url: str The beginning of the request URL. This is normally
  'http://www.google.com/accounts/AuthSubRequest' or
  '/accounts/AuthSubRequest'
  next: string The URL user will be sent to after logging in.
  scope: string The URL of the service to be accessed.
  secure: boolean (optional) Determines whether or not the issued token
  is a secure token.
  session: boolean (optional) Determines whether or not the issued token
  can be upgraded to a session token.
  domain: str (optional) The Google Apps domain for this account. If this
  is not a Google Apps account, use 'default' which is the default
  value.`

GenerateClientLoginAuthToken(http_body)
  `Returns the token value to use in Authorization headers.
  Reads the token from the server's response to a Client Login request and
  creates header value to use in requests.
  Args:
  http_body: str The body of the server's HTTP response to a Client Login
  request
  Returns:
  The value half of an Authorization header.`

GenerateClientLoginRequestBody = generate_client_login_request_body(email, password, service, source, account_type='HOSTED_OR_GOOGLE', captcha_token=None, captcha_response=None)
  `Creates the body of the autentication request
  See http://code.google.com/apis/accounts/AuthForInstalledApps.html#Request
  for more details.
  Args:
  email: str
  password: str
  service: str
  source: str
  account_type: str (optional) Defaul is 'HOSTED_OR_GOOGLE', other valid
  values are 'GOOGLE' and 'HOSTED'
  captcha_token: str (optional)
  captcha_response: str (optional)
  Returns:
  The HTTP body to send in a request for a client login token.`

GenerateOAuthAccessTokenUrl(authorized_request_token, oauth_input_params, access_token_url='https://www.google.com/accounts/OAuthGetAccessToken', oauth_version='1.0', oauth_verifier=None)
  `Generates URL at which user will login to authorize the request token.
  Args:
  authorized_request_token: gdata.auth.OAuthToken OAuth authorized request
  token.
  oauth_input_params: OAuthInputParams OAuth input parameters.
  access_token_url: string The beginning of the authorization URL. This is
  normally 'https://www.google.com/accounts/OAuthGetAccessToken' or
  '/accounts/OAuthGetAccessToken'
  oauth_version: str (default='1.0') oauth_version parameter.
  oauth_verifier: str (optional) If present, it is assumed that the client
  will use the OAuth v1.0a protocol which includes passing the
  oauth_verifier (as returned by the SP) in the access token step.
  Returns:
  atom.url.Url OAuth access token URL.`

GenerateOAuthAuthorizationUrl(request_token, authorization_url='https://www.google.com/accounts/OAuthAuthorizeToken', callback_url=None, extra_params=None, include_scopes_in_callback=False, scopes_param_prefix='oauth_token_scope')
  `Generates URL at which user will login to authorize the request token.
  Args:
  request_token: gdata.auth.OAuthToken OAuth request token.
  authorization_url: string The beginning of the authorization URL. This is
  normally 'https://www.google.com/accounts/OAuthAuthorizeToken' or
  '/accounts/OAuthAuthorizeToken'
  callback_url: string (optional) The URL user will be sent to after
  logging in and granting access.
  extra_params: dict (optional) Additional parameters to be sent.
  include_scopes_in_callback: Boolean (default=False) if set to True, and
  if 'callback_url' is present, the 'callback_url' will be modified to
  include the scope(s) from the request token as a URL parameter. The
  key for the 'callback' URL's scope parameter will be
  OAUTH_SCOPE_URL_PARAM_NAME. The benefit of including the scope URL as
  a parameter to the 'callback' URL, is that the page which receives
  the OAuth token will be able to tell which URLs the token grants
  access to.
  scopes_param_prefix: string (default='oauth_token_scope') The URL
  parameter key which maps to the list of valid scopes for the token.
  This URL parameter will be included in the callback URL along with
  the scopes of the token as value if include_scopes_in_callback=True.
  Returns:
  atom.url.Url OAuth authorization URL.`

GenerateOAuthRequestTokenUrl(oauth_input_params, scopes, request_token_url='https://www.google.com/accounts/OAuthGetRequestToken', extra_parameters=None)
  `Generate a URL at which a request for OAuth request token is to be sent.
  Args:
  oauth_input_params: OAuthInputParams OAuth input parameters.
  scopes: list of strings The URLs of the services to be accessed.
  request_token_url: string The beginning of the request token URL. This is
  normally 'https://www.google.com/accounts/OAuthGetRequestToken' or
  '/accounts/OAuthGetRequestToken'
  extra_parameters: dict (optional) key-value pairs as any additional
  parameters to be included in the URL and signature while making a
  request for fetching an OAuth request token. All the OAuth parameters
  are added by default. But if provided through this argument, any
  default parameters will be overwritten. For e.g. a default parameter
  oauth_version 1.0 can be overwritten if
  extra_parameters = {'oauth_version': '2.0'}
  Returns:
  atom.url.Url OAuth request token URL.`

GetCaptchaChallenge = get_captcha_challenge(http_body, captcha_base_url='http://www.google.com/accounts/')
  `Returns the URL and token for a CAPTCHA challenge issued by the server.
  Args:
  http_body: str The body of the HTTP response from the server which
  contains the CAPTCHA challenge.
  captcha_base_url: str This function returns a full URL for viewing the
  challenge image which is built from the server's response. This
  base_url is used as the beginning of the URL because the server
  only provides the end of the URL. For example the server provides
  'Captcha?ctoken=Hi...N' and the URL for the image is
  'http://www.google.com/accounts/Captcha?ctoken=Hi...N'
  Returns:
  A dictionary containing the information needed to repond to the CAPTCHA
  challenge, the image URL and the ID token of the challenge. The
  dictionary is in the form:
  {'token': string identifying the CAPTCHA image,
  'url': string containing the URL of the image}
  Returns None if there was no CAPTCHA challenge in the response.`

OAuthTokenFromHttpBody(http_body)
  `Parses the HTTP response body and returns an OAuth token.
  The returned OAuth token will just have key and secret parameters set.
  It won't have any knowledge about the scopes or oauth_input_params. It is
  your responsibility to make it aware of the remaining parameters.
  Returns:
  OAuthToken OAuth token.`

OAuthTokenFromUrl(url, scopes_param_prefix='oauth_token_scope')
  `Creates an OAuthToken and sets token key and scopes (if present) from URL.
  After the Google Accounts OAuth pages redirect the user's broswer back to
  the web application (using the 'callback' URL from the request) the web app
  can extract the token from the current page's URL. The token is same as the
  request token, but it is either authorized (if user grants access) or
  unauthorized (if user denies access). The token is provided as a
  URL parameter named 'oauth_token' and if it was chosen to use
  GenerateOAuthAuthorizationUrl with include_scopes_in_param=True, the token's
  valid scopes are included in a URL parameter whose name is specified in
  scopes_param_prefix.
  Args:
  url: atom.url.Url or str representing the current URL. The token value
  and valid scopes should be included as URL parameters.
  scopes_param_prefix: str (optional) The URL parameter key which maps to
  the list of valid scopes for the token.
  Returns:
  An OAuthToken with the token key from the URL and set to be valid for
  the scopes passed in on the URL. If no scopes were included in the URL,
  the OAuthToken defaults to being valid for no scopes. If there was no
  'oauth_token' parameter in the URL, this function returns None.`

TokenFromHttpBody = token_from_http_body(http_body)
  `Extracts the AuthSub token from an HTTP body string.
  Used to find the new session token after making a request to upgrade a
  single use AuthSub token.
  Args:
  http_body: str The repsonse from the server which contains the AuthSub
  key. For example, this function would find the new session token
  from the server's response to an upgrade token request.
  Returns:
  The raw token value to use in an AuthSubToken object.`

TokenFromUrl(url)
  `Extracts the AuthSub token from the URL.
  Returns the raw token value.
  Args:
  url: str The URL or the query portion of the URL string (after the ?) of
  the current page which contains the AuthSub token as a URL parameter.`

extract_auth_sub_token_from_url(url, scopes_param_prefix='auth_sub_scopes', rsa_key=None)
  `Creates an AuthSubToken and sets the token value and scopes from the URL.
  After the Google Accounts AuthSub pages redirect the user's broswer back to
  the web application (using the 'next' URL from the request) the web app must
  extract the token from the current page's URL. The token is provided as a
  URL parameter named 'token' and if generate_auth_sub_url was used to create
  the request, the token's valid scopes are included in a URL parameter whose
  name is specified in scopes_param_prefix.
  Args:
  url: atom.url.Url or str representing the current URL. The token value
  and valid scopes should be included as URL parameters.
  scopes_param_prefix: str (optional) The URL parameter key which maps to
  the list of valid scopes for the token.
  Returns:
  An AuthSubToken with the token value from the URL and set to be valid for
  the scopes passed in on the URL. If no scopes were included in the URL,
  the AuthSubToken defaults to being valid for no scopes. If there was no
  'token' parameter in the URL, this function returns None.`

extract_client_login_token(http_body, scopes)
  `Parses the server's response and returns a ClientLoginToken.
  Args:
  http_body: str The body of the server's HTTP response to a Client Login
  request. It is assumed that the login request was successful.
  scopes: list containing atom.url.Urls or strs. The scopes list contains
  all of the partial URLs under which the client login token is
  valid. For example, if scopes contains ['http://example.com/foo']
  then the client login token would be valid for
  http://example.com/foo/bar/baz
  Returns:
  A ClientLoginToken which is valid for the specified scopes.`

generate_auth_sub_url(next, scopes, secure=False, session=True, request_url='https://www.google.com/accounts/AuthSubRequest', domain='default', scopes_param_prefix='auth_sub_scopes')
  `Constructs a URL string for requesting a multiscope AuthSub token.
  The generated token will contain a URL parameter to pass along the
  requested scopes to the next URL. When the Google Accounts page
  redirects the broswser to the 'next' URL, it appends the single use
  AuthSub token value to the URL as a URL parameter with the key 'token'.
  However, the information about which scopes were requested is not
  included by Google Accounts. This method adds the scopes to the next
  URL before making the request so that the redirect will be sent to
  a page, and both the token value and the list of scopes can be
  extracted from the request URL.
  Args:
  next: atom.url.URL or string The URL user will be sent to after
  authorizing this web application to access their data.
  scopes: list containint strings The URLs of the services to be accessed.
  secure: boolean (optional) Determines whether or not the issued token
  is a secure token.
  session: boolean (optional) Determines whether or not the issued token
  can be upgraded to a session token.
  request_url: atom.url.Url or str The beginning of the request URL. This
  is normally 'http://www.google.com/accounts/AuthSubRequest' or
  '/accounts/AuthSubRequest'
  domain: The domain which the account is part of. This is used for Google
  Apps accounts, the default value is 'default' which means that the
  requested account is a Google Account (@gmail.com for example)
  scopes_param_prefix: str (optional) The requested scopes are added as a
  URL parameter to the next URL so that the page at the 'next' URL can
  extract the token value and the valid scopes from the URL. The key
  for the URL parameter defaults to 'auth_sub_scopes'
  Returns:
  An atom.url.Url which the user's browser should be directed to in order
  to authorize this application to access their information.`

generate_client_login_request_body(email, password, service, source, account_type='HOSTED_OR_GOOGLE', captcha_token=None, captcha_response=None)
  `Creates the body of the autentication request
  See http://code.google.com/apis/accounts/AuthForInstalledApps.html#Request
  for more details.
  Args:
  email: str
  password: str
  service: str
  source: str
  account_type: str (optional) Defaul is 'HOSTED_OR_GOOGLE', other valid
  values are 'GOOGLE' and 'HOSTED'
  captcha_token: str (optional)
  captcha_response: str (optional)
  Returns:
  The HTTP body to send in a request for a client login token.`

get_captcha_challenge(http_body, captcha_base_url='http://www.google.com/accounts/')
  `Returns the URL and token for a CAPTCHA challenge issued by the server.
  Args:
  http_body: str The body of the HTTP response from the server which
  contains the CAPTCHA challenge.
  captcha_base_url: str This function returns a full URL for viewing the
  challenge image which is built from the server's response. This
  base_url is used as the beginning of the URL because the server
  only provides the end of the URL. For example the server provides
  'Captcha?ctoken=Hi...N' and the URL for the image is
  'http://www.google.com/accounts/Captcha?ctoken=Hi...N'
  Returns:
  A dictionary containing the information needed to repond to the CAPTCHA
  challenge, the image URL and the ID token of the challenge. The
  dictionary is in the form:
  {'token': string identifying the CAPTCHA image,
  'url': string containing the URL of the image}
  Returns None if there was no CAPTCHA challenge in the response.`

get_client_login_token(http_body)
  `Returns the token value for a ClientLoginToken.
  Reads the token from the server's response to a Client Login request and
  creates the token value string to use in requests.
  Args:
  http_body: str The body of the server's HTTP response to a Client Login
  request
  Returns:
  The token value string for a ClientLoginToken.`

token_from_http_body(http_body)
  `Extracts the AuthSub token from an HTTP body string.
  Used to find the new session token after making a request to upgrade a
  single use AuthSub token.
  Args:
  http_body: str The repsonse from the server which contains the AuthSub
  key. For example, this function would find the new session token
  from the server's response to an upgrade token request.
  Returns:
  The raw token value to use in an AuthSubToken object.`

Data
`` |  | AUTHSUB_AUTH_LABEL = 'AuthSub token=' PROGRAMMATIC_AUTH_LABEL = 'GoogleLogin auth=' __author__ = 'api.jscudder (Jeff Scudder)'

Author
`` |  | api.jscudder (Jeff Scudder)