Пример #1
0
    def test_validate_error(self):
        payload = (
            b'{'
            b'  "web": {'
            b'    "client_id": "[[CLIENT ID REQUIRED]]",'
            b'    "client_secret": "[[CLIENT SECRET REQUIRED]]",'
            b'    "redirect_uris": ["http://localhost:8080/oauth2callback"],'
            b'    "auth_uri": "",'
            b'    "token_uri": ""'
            b'  }'
            b'}')
        ERRORS = [
            ('{}', 'Invalid'),
            ('{"foo": {}}', 'Unknown'),
            ('{"web": {}}', 'Missing'),
            ('{"web": {"client_id": "dkkd"}}', 'Missing'),
            (payload, 'Property'),
        ]
        for src, match in ERRORS:
            # Ensure that it is unicode
            src = _from_bytes(src)
            # Test load(s)
            try:
                clientsecrets.loads(src)
                self.fail(src + ' should not be a valid client_secrets file.')
            except clientsecrets.InvalidClientSecretsError as e:
                self.assertTrue(str(e).startswith(match))

            # Test loads(fp)
            try:
                fp = StringIO(src)
                clientsecrets.load(fp)
                self.fail(src + ' should not be a valid client_secrets file.')
            except clientsecrets.InvalidClientSecretsError as e:
                self.assertTrue(str(e).startswith(match))
Пример #2
0
    def test_validate_error(self):
        payload = (
            b'{'
            b'  "web": {'
            b'    "client_id": "[[CLIENT ID REQUIRED]]",'
            b'    "client_secret": "[[CLIENT SECRET REQUIRED]]",'
            b'    "redirect_uris": ["http://localhost:8080/oauth2callback"],'
            b'    "auth_uri": "",'
            b'    "token_uri": ""'
            b'  }'
            b'}')
        ERRORS = [
            ('{}', 'Invalid'),
            ('{"foo": {}}', 'Unknown'),
            ('{"web": {}}', 'Missing'),
            ('{"web": {"client_id": "dkkd"}}', 'Missing'),
            (payload, 'Property'),
        ]
        for src, match in ERRORS:
            # Ensure that it is unicode
            src = _from_bytes(src)
            # Test load(s)
            try:
                clientsecrets.loads(src)
                self.fail(src + ' should not be a valid client_secrets file.')
            except clientsecrets.InvalidClientSecretsError as e:
                self.assertTrue(str(e).startswith(match))

            # Test loads(fp)
            try:
                fp = StringIO(src)
                clientsecrets.load(fp)
                self.fail(src + ' should not be a valid client_secrets file.')
            except clientsecrets.InvalidClientSecretsError as e:
                self.assertTrue(str(e).startswith(match))
Пример #3
0
  def test_validate_error(self):
    ERRORS = [
      ('{}', 'Invalid'),
      ('{"foo": {}}', 'Unknown'),
      ('{"web": {}}', 'Missing'),
      ('{"web": {"client_id": "dkkd"}}', 'Missing'),
      ("""{
         "web": {
           "client_id": "[[CLIENT ID REQUIRED]]",
           "client_secret": "[[CLIENT SECRET REQUIRED]]",
           "redirect_uris": ["http://localhost:8080/oauth2callback"],
           "auth_uri": "",
           "token_uri": ""
         }
       }
       """, 'Property'),
      ]
    for src, match in ERRORS:
      # Test load(s)
      try:
        clientsecrets.loads(src)
        self.fail(src + ' should not be a valid client_secrets file.')
      except clientsecrets.InvalidClientSecretsError, e:
        self.assertTrue(str(e).startswith(match))

      # Test loads(fp)
      try:
        fp = StringIO.StringIO(src)
        clientsecrets.load(fp)
        self.fail(src + ' should not be a valid client_secrets file.')
      except clientsecrets.InvalidClientSecretsError, e:
        self.assertTrue(str(e).startswith(match))
Пример #4
0
    def test_validate_error(self):
        payload = (
            b"{"
            b'  "web": {'
            b'    "client_id": "[[CLIENT ID REQUIRED]]",'
            b'    "client_secret": "[[CLIENT SECRET REQUIRED]]",'
            b'    "redirect_uris": ["http://localhost:8080/oauth2callback"],'
            b'    "auth_uri": "",'
            b'    "token_uri": ""'
            b"  }"
            b"}"
        )
        ERRORS = [
            ("{}", "Invalid"),
            ('{"foo": {}}', "Unknown"),
            ('{"web": {}}', "Missing"),
            ('{"web": {"client_id": "dkkd"}}', "Missing"),
            (payload, "Property"),
        ]
        for src, match in ERRORS:
            # Ensure that it is unicode
            src = _helpers._from_bytes(src)
            # Test load(s)
            with self.assertRaises(clientsecrets.InvalidClientSecretsError) as exc_manager:
                clientsecrets.loads(src)

            self.assertTrue(str(exc_manager.exception).startswith(match))

            # Test loads(fp)
            with self.assertRaises(clientsecrets.InvalidClientSecretsError) as exc_manager:
                fp = StringIO(src)
                clientsecrets.load(fp)

            self.assertTrue(str(exc_manager.exception).startswith(match))
Пример #5
0
def acquire_credentials(scopes):
    client_secrets_contents = pkgutil.get_data('ytsub','data/client_secrets.json')
    if client_secrets_contents is None:
        sys.exit("Missing clientsecrets.json file.")
    
    try: 
        client_type, client_info = clientsecrets.loads(client_secrets_contents)
        if client_type in [clientsecrets.TYPE_WEB, clientsecrets.TYPE_INSTALLED]: 
            flow = OAuth2WebServerFlow( 
                client_info['client_id'], 
                client_info['client_secret'], 
                scopes, 
                redirect_uri=client_info['redirect_uris'], 
                user_agent=None, 
                auth_uri=client_info['auth_uri'], 
                token_uri=client_info['token_uri']) 
   
    except clientsecrets.InvalidClientSecretsError: 
        sys.exit("Invalid client secrets file.") 

    
    storage = Storage(expanduser("~") + "/.ytsub-oauth2.json")
    credentials = storage.get()

    if credentials is None or credentials.invalid:
      credentials = run(flow, storage)

    return credentials
Пример #6
0
 def get(self, filename, namespace=''):
     CSJ = 'client_secrets.json'
     OSNS = 'oauth2client:secrets#ns'
     if filename == CSJ and namespace == OSNS:
         c_secrets = os.environ.get('CLIENT_SECRETS', '')
         if c_secrets:
             c_type, c_info = clientsecrets.loads(c_secrets)
             return {c_type: c_info}
     return None
Пример #7
0
def load_client_config(gauth, client_config):
    client_type, client_info = clientsecrets.loads(client_config)

    config_index = ['client_id', 'client_secret', 'auth_uri', 'token_uri']
    for config in config_index:
        gauth.client_config[config] = client_info[config]

    gauth.client_config['revoke_uri'] = client_info.get('revoke_uri')
    gauth.client_config['redirect_uri'] = client_info['redirect_uris'][0]

    service_auth_config = ['client_email']
    try:
        for config in service_auth_config:
            gauth.client_config[config] = client_info[config]
    except KeyError:
        pass
Пример #8
0
def flow_from_clientsecrets(client_dict,
                            scope,
                            redirect_uri=None,
                            message=None,
                            cache=None,
                            login_hint=None,
                            device_uri=None,
                            pkce=None,
                            code_verifier=None):
    '''
    Reimplementation of google Oauth2client, to use client secret
    from a string, instead of a file
    '''
    try:
        client_type, client_info = clientsecrets.loads(client_dict)
        if client_type in (clientsecrets.TYPE_WEB,
                           clientsecrets.TYPE_INSTALLED):
            constructor_kwargs = {
                'redirect_uri': redirect_uri,
                'auth_uri': client_info['auth_uri'],
                'token_uri': client_info['token_uri'],
                'login_hint': login_hint,
            }
            revoke_uri = client_info.get('revoke_uri')
            optional = ('revoke_uri', 'device_uri', 'pkce', 'code_verifier')
            for param in optional:  # pragma: no cover
                if locals()[param] is not None:
                    constructor_kwargs[param] = locals()[param]

            return client.OAuth2WebServerFlow(client_info['client_id'],
                                              client_info['client_secret'],
                                              scope, **constructor_kwargs)

    except clientsecrets.InvalidClientSecretsError as e:  # pragma: no cover
        if message is not None:
            if e.args:
                message = ('The client secrets were invalid: '
                           '\n{0}\n{1}'.format(e, message))
            sys.exit(message)
        else:
            raise
    else:  # pragma: no cover
        raise client.UnknownClientSecretsFlowError(
            'This OAuth 2.0 flow is unsupported: {0!r}'.format(client_type))