Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from .access_token import AccessToken
- import logging
- import urllib.parse
- class Authorization:
- """Осуществляет прямую авторизацию <https://vk.com/dev/auth_direct>"""
- AUTH_HOST = "https://oauth.vk.com"
- AUTH_PATH = "/token"
- GRANT_TYPE = "password"
- def __init__(self,
- client,
- client_id,
- client_secret,
- scope=None,
- username=None,
- password=None,
- test_redirect_uri=None):
- """Description goes here
- :param client,:
- :type client,:
- :param client_id,:
- :type client_id,:
- :param client_secret,:
- :type client_secret,:
- :param scope:
- :type scope:
- :param username:
- :type username:
- :param password:
- :type password:
- :param test_redirect_uri:
- :type test_redirect_uri:
- """
- self.logger = logging.getLogger(
- ".".join([self.__class__.__module__, self.__class__.__name__]))
- self.client = client
- self.client_id = client_id
- self.client_secret = client_secret
- self.scope = scope
- self.username = username
- self.password = password
- self.test_redirect_uri = test_redirect_uri
- def authorize(self, username=None, password=None):
- self.username = username or self.username
- self.password = password or self.password
- if self.username is None:
- raise AttributeError("username is not set")
- if self.password is None:
- raise AttributeError("password is not set")
- params = dict(
- username=self.username,
- password=self.password
- )
- if self.test_redirect_uri:
- params['test_redirect_uri']
- self.do_auth(params)
- @property
- def auth_url(self):
- return urllib.parse.urljoin(self.AUTH_HOST, self.AUTH_PATH)
- def do_auth(self, add_params={}):
- params = self.get_auth_params(add_params)
- response = self.client.get(self.auth_url, params)
- if 'error' in response:
- self.process_error_response(response, add_params)
- return
- self.logger.debug(response)
- self.client.access_token = AccessToken(
- token=response.access_token,
- user_id=response.user_id,
- secret=response.get('secret')
- )
- self.logger.info("Successfully authorized")
- def process_error_response(self, response, add_params):
- """Можно переопределить в классах потомках, а можно что-то типа этого
- сделать:
- def process_error_response(self, response, add_params):
- if response.error == AuthError.NEED_CAPTCHA
- # Получаем captcha_key
- ...
- add_params.update({
- "captcha_key": captcha_key,
- "captcha_sid": response.captcha_sid
- })
- self.do_auth(add_params)
- return
- super().process_error_response(response, add_params)
- """
- raise AuthError(response)
- def get_auth_params(self, add_params={}):
- params = dict(
- grant_type=self.GRANT_TYPE,
- v=self.client.api_version,
- client_id=self.client_id,
- client_secret=self.client_secret
- )
- if self.scope:
- params['scope'] = self.scope
- if add_params:
- params.update(add_params)
- return params
- class AuthError(Exception):
- NEED_CAPTCHA = 'need_captcha'
- NEED_VALIDATON = 'need_validation'
- def __init__(self, response):
- self.error_type = response.error
- self.error_description = response.get('error_description')
- def __str__(self):
- return "{}: {}".format(self.error_type, self.error_description) \
- if self.error_description else self.error_type
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement