async def async_create_refresh_token( self, user: models.User, client_id: Optional[str] = None, client_name: Optional[str] = None, client_icon: Optional[str] = None, token_type: str = models.TOKEN_TYPE_NORMAL, access_token_expiration: timedelta = ACCESS_TOKEN_EXPIRATION, credential: models.Credentials = None, ) -> models.RefreshToken: # 如果是小度或天猫精灵,则给它们十年的授权 if client_id is not None and [ 'https://open.bot.tmall.com', 'https://xiaodu.baidu.com' ].count(client_id.strip('/')) > 0: access_token_expiration = timedelta(hours=87600) _LOGGER.debug('如果是小度或天猫精灵,则给它们十年的授权') """Create a new token for a user.""" kwargs = { 'user': user, 'client_id': client_id, 'token_type': token_type, 'access_token_expiration': access_token_expiration } # type: Dict[str, Any] if client_name: kwargs['client_name'] = client_name if client_icon: kwargs['client_icon'] = client_icon refresh_token = models.RefreshToken(**kwargs) user.refresh_tokens[refresh_token.id] = refresh_token self.hass.auth._store._async_schedule_save() return refresh_token
async def async_create_refresh_token( user: models.User, client_id: Optional[str] = None, client_name: Optional[str] = None, client_icon: Optional[str] = None, token_type: str = models.TOKEN_TYPE_NORMAL, access_token_expiration: timedelta = ACCESS_TOKEN_EXPIRATION ) -> models.RefreshToken: if access_token_expiration == ACCESS_TOKEN_EXPIRATION: access_token_expiration = timedelta(hours=EXPIRE_HOURS) _LOGGER.info('Access token expiration: %d hours', EXPIRE_HOURS) """Create a new token for a user.""" kwargs = { 'user': user, 'client_id': client_id, 'token_type': token_type, 'access_token_expiration': access_token_expiration } # type: Dict[str, Any] if client_name: kwargs['client_name'] = client_name if client_icon: kwargs['client_icon'] = client_icon refresh_token = models.RefreshToken(**kwargs) user.refresh_tokens[refresh_token.id] = refresh_token _hass.auth._store._async_schedule_save() return refresh_token
async def async_create_refresh_token77( user: models.User, client_id: Optional[str] = None) \ -> models.RefreshToken: """Create a new token for a user.""" _LOGGER.info('access token expiration: %d hours', _expire_hours) refresh_token = models.RefreshToken(user=user, client_id=client_id, access_token_expiration = timedelta(hours=_expire_hours)) user.refresh_tokens[refresh_token.id] = refresh_token _hass.auth._store._async_schedule_save() return refresh_token
def test_access_token_expired(): """Test that the expired property on access tokens work.""" refresh_token = auth_models.RefreshToken(user=None, client_id='bla') access_token = auth_models.AccessToken(refresh_token=refresh_token) assert access_token.expired is False with patch('homeassistant.util.dt.utcnow', return_value=dt_util.utcnow() + auth_const.ACCESS_TOKEN_EXPIRATION): assert access_token.expired is True almost_exp = \ dt_util.utcnow() + auth_const.ACCESS_TOKEN_EXPIRATION - timedelta(1) with patch('homeassistant.util.dt.utcnow', return_value=almost_exp): assert access_token.expired is False
async def async_create_refresh_token(self, user, client_id=None, client_name=None, client_icon=None, token_type=models.TOKEN_TYPE_NORMAL, access_token_expiration=ACCESS_TOKEN_EXPIRATION): if access_token_expiration == ACCESS_TOKEN_EXPIRATION: access_token_expiration = timedelta(hours=EXPIRE_HOURS) kwargs = { 'user': user, 'client_id': client_id, 'token_type': token_type, 'access_token_expiration': access_token_expiration } if client_name: kwargs['client_name'] = client_name if client_icon: kwargs['client_icon'] = client_icon refresh_token = models.RefreshToken(**kwargs) user.refresh_tokens[refresh_token.id] = refresh_token self.hass.auth._store._async_schedule_save() return refresh_token