Beispiel #1
0
def imap():
  server = varconfig('imap_server', 'IMAP server: ')
  address = varconfig('mail_address', 'Address: ')
  password = getpass.getpass('Password: ')
  mail = imaplib.IMAP4_SSL(server)
  mail.login(address, password)
  return mail
Beispiel #2
0
  def __init__(self, host='localhost', port='4443'):
    if port == '443' or port == '':
      self._base_url = 'https://{}/api/v0'.format(host, port)
      self._upload_url = 'https://{}/upload/api/v0'.format(host, port)
    else:
      self._base_url = 'https://{}:{}/api/v0'.format(host, port)
      self._upload_url = 'https://{}:{}/upload/api/v0'.format(host, port)

    consumer_key = varconfig('consumer_key', 'OAuth Consumer key: ', printval=False)
    consumer_secret = varconfig('consumer_secret', 'OAuth Consumer secret: ', printval=False)

    # Consumer key is undefined, switching to regular login.
    if consumer_key == '':
      username = input('PEPS Login: '******'Password: '******'username': username, 'password': password}
      headers = {'content-type': 'application/json'}
      res = requests.post('{}/login'.format(self._base_url), data=json.dumps(data), headers=headers, verify=verify)
      try:
        token = res.json()['token']
        self.session = OAuth1Session('', '', access_token=token, access_token_secret='')
        print('Api has been successfully configured')
      except Exception as err:
        print('Api: Configuration error: {}'.format(err))
        self.session = None

    # Consumer key has been provided, standard OAuth flow.
    else:
      peps = OAuth1Service(
        name='webmail-api-test',
        consumer_key=consumer_key,
        consumer_secret=consumer_secret,
        request_token_url='{}/oauth/request_token'.format(self._base_url),
        access_token_url='{}/oauth/access_token'.format(self._base_url),
        authorize_url='{}/oauth/authorize'.format(self._base_url),
        base_url=self._base_url
      )

      print("---> Initiate OAuth1 connection")
      request_token, request_token_secret = peps.get_request_token(verify=verify, params={'oauth_callback': oauth_callback})

      print("---> Request token")
      authorize_url = peps.get_authorize_url(request_token, verify=verify, params={'oauth_callback': oauth_callback})

      print("---> Authorize token")
      print('Visit this URL in your browser: {url}'.format(url=authorize_url))

      webbrowser.open(authorize_url) # Authenticate the user.
      start_server() # Open the server to receive the verifier.
      # oauth_verifier = input('Enter oauth_verifier from browser: ')
      # Waiting for the token verifier...
      tries = 0
      while oauth_verifier.value == '' and tries < 30:
        time.sleep(1)
        tries = tries + 1
      terminate_server()
      if oauth_verifier.value == '':
        print('I waited too long for your password, now I am leaving...')
      else:
        print("---> Create session")
        self.session = peps.get_auth_session(
          request_token, request_token_secret,
          method='POST',
          verify=verify,
          params={'oauth_verifier': oauth_verifier.value},
          headers={'content-type': 'application/json'},  # These two things are necessary to
          data='{}'                                      # prevent rauth from using the body to pass the oauth tokens.
        )
        print('Api has been successfully configured !')
Beispiel #3
0
    def __init__(self, host='localhost', port='4443'):
        if port == '443' or port == '':
            self._base_url = 'https://{}/api/v0'.format(host, port)
            self._upload_url = 'https://{}/upload/api/v0'.format(host, port)
        else:
            self._base_url = 'https://{}:{}/api/v0'.format(host, port)
            self._upload_url = 'https://{}:{}/upload/api/v0'.format(host, port)

        consumer_key = varconfig('consumer_key',
                                 'OAuth Consumer key: ',
                                 printval=False)
        consumer_secret = varconfig('consumer_secret',
                                    'OAuth Consumer secret: ',
                                    printval=False)

        # Consumer key is undefined, switching to regular login.
        if consumer_key == '':
            username = input('PEPS Login: '******'Password: '******'username': username, 'password': password}
            headers = {'content-type': 'application/json'}
            res = requests.post('{}/login'.format(self._base_url),
                                data=json.dumps(data),
                                headers=headers,
                                verify=verify)
            try:
                token = res.json()['token']
                self.session = OAuth1Session('',
                                             '',
                                             access_token=token,
                                             access_token_secret='')
                print('Api has been successfully configured')
            except Exception as err:
                print('Api: Configuration error: {}'.format(err))
                self.session = None

        # Consumer key has been provided, standard OAuth flow.
        else:
            peps = OAuth1Service(
                name='webmail-api-test',
                consumer_key=consumer_key,
                consumer_secret=consumer_secret,
                request_token_url='{}/oauth/request_token'.format(
                    self._base_url),
                access_token_url='{}/oauth/access_token'.format(
                    self._base_url),
                authorize_url='{}/oauth/authorize'.format(self._base_url),
                base_url=self._base_url)

            print("---> Initiate OAuth1 connection")
            request_token, request_token_secret = peps.get_request_token(
                verify=verify, params={'oauth_callback': oauth_callback})

            print("---> Request token")
            authorize_url = peps.get_authorize_url(
                request_token,
                verify=verify,
                params={'oauth_callback': oauth_callback})

            print("---> Authorize token")
            print('Visit this URL in your browser: {url}'.format(
                url=authorize_url))

            webbrowser.open(authorize_url)  # Authenticate the user.
            start_server()  # Open the server to receive the verifier.
            # oauth_verifier = input('Enter oauth_verifier from browser: ')
            # Waiting for the token verifier...
            tries = 0
            while oauth_verifier.value == '' and tries < 30:
                time.sleep(1)
                tries = tries + 1
            terminate_server()
            if oauth_verifier.value == '':
                print(
                    'I waited too long for your password, now I am leaving...')
            else:
                print("---> Create session")
                self.session = peps.get_auth_session(
                    request_token,
                    request_token_secret,
                    method='POST',
                    verify=verify,
                    params={'oauth_verifier': oauth_verifier.value},
                    headers={'content-type': 'application/json'
                             },  # These two things are necessary to
                    data=
                    '{}'  # prevent rauth from using the body to pass the oauth tokens.
                )
                print('Api has been successfully configured !')