PageRenderTime 146ms CodeModel.GetById 60ms app.highlight 15ms RepoModel.GetById 66ms app.codeStats 0ms

/src/googlecl/discovery/authentication.py

http://googlecl.googlecode.com/
Python | 64 lines | 25 code | 6 blank | 33 comment | 3 complexity | 36625ee91479051185b8171b8f48e5ec MD5 | raw file
 1# Copyright (C) 2010 Google Inc.
 2#
 3# Licensed under the Apache License, Version 2.0 (the "License");
 4# you may not use this file except in compliance with the License.
 5# You may obtain a copy of the License at
 6#
 7#      http://www.apache.org/licenses/LICENSE-2.0
 8#
 9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15
16"""Subfunction for the Google command line tool, GoogleCL.
17
18This function handles the authentication and storage of
19credentials for the services which use OAuth2
20"""
21
22import httplib2
23import logging
24import pickle
25import os
26import googlecl
27from oauth2client.file import Storage
28from oauth2client.client import OAuth2WebServerFlow
29from oauth2client.tools import run
30
31LOG = logging.getLogger(googlecl.LOGGER_NAME)
32
33TOKENS_FILENAME_FORMAT = 'dca_%s_%s'
34
35def authenticate(email, servicename, doc, http, client_id,
36                 client_secret, force_auth=False):
37  """ Authenticates an provided http object,
38  Prompts for user confirmation if necessary, and stores the credentials
39
40  Args:
41    email: The email address of the user
42    servicename: The service which requires authentication
43    doc: Documentation for the service (for determining scopes)
44    http: The object being authenticated
45
46  Returns:
47    The authorized object
48  """
49  tokens_path = googlecl.get_data_path(TOKENS_FILENAME_FORMAT %
50                                     (email, servicename),
51                                     create_missing_dir=True)
52  storage = Storage(tokens_path)
53  credentials = storage.get()
54  if credentials is None or credentials.invalid or force_auth:
55    # Works with google-api-python-client-1.0beta2, but not with
56    # beta4.  They're working on a way to allow deleting credentials.
57    #storage.put(None)
58    desiredcred = ""
59    for arg in doc['auth']['oauth2']['scopes']:
60      desiredcred = desiredcred + arg + ' '
61    FLOW = OAuth2WebServerFlow(client_id, client_secret,
62      scope=desiredcred, user_agent='discoverycl')
63    credentials = run(FLOW, storage)
64  return credentials.authorize(http)