Esempio n. 1
0
    def create_session_from_oauth_code(self, code, state):
        """Create a session object authenticated by an oauth code.

        :param code: Auth code received from Automatic redirect URL GET
        :param state: State received from Automatic redirect URL GET
        :returns Session: Authenticated session object
        """
        if state != self.state:
            raise exceptions.StateError(
                "Error creating session. State {} returned by OAauth2 request "
                "doesn't match aioautomatic state {}. A possible forgery "
                "attack is taking place. See "
                "https://developer.automatic.com/api-reference/#oauth-workflow"
                .format(state, self.state))

        _LOGGER.info("Creating session from oauth code.")
        auth_payload = {
            'client_id': self._client_id,
            'client_secret': self._client_secret,
            'grant_type': 'authorization_code',
            'code': code,
        }
        resp = yield from self._post(const.AUTH_URL, auth_payload)
        data = validation.validate(validation.AUTH_TOKEN, resp)
        return session.Session(self, **data)
Esempio n. 2
0
    def create_session_from_refresh_token(self, refresh_token):
        """Create a session object authenticated by a stored refresh token.

        :param refresh_token: Refresh token from previous session
        :returns Session: Authenticated session object
        """
        _LOGGER.info("Creating session from refresh token.")
        auth_payload = {
            'client_id': self._client_id,
            'client_secret': self._client_secret,
            'grant_type': 'refresh_token',
            'refresh_token': refresh_token,
        }
        resp = yield from self._post(const.AUTH_URL, auth_payload)
        data = validation.validate(validation.AUTH_TOKEN, resp)
        return session.Session(self, **data)
Esempio n. 3
0
    def create_session_from_password(self, scope, username, password):
        """Create a session object authenticated by username and password.

        :param scope: Requested API scope for this session
        :param username: User's Automatic account username
        :param username: User's Automatic account password
        :returns Session: Authenticated session object
        """
        _LOGGER.info("Creating session from username/password.")
        auth_payload = {
            'client_id': self._client_id,
            'client_secret': self._client_secret,
            'grant_type': 'password',
            'username': username,
            'password': password,
            'scope': ' '.join('scope:{}'.format(item) for item in scope),
            }
        resp = yield from self._post(const.AUTH_URL, auth_payload)
        data = validation.AUTH_TOKEN(resp)
        return session.Session(self, **data)