def setUp(self): # Create an 'ephemeral' token so we can test token timeouts. We # want a timeout long enough to last the test, but we don't want to # slow down the tests too much either. self.normal_creds = TokenCredentials(self.key_path, self.key_id, self.team_id) self.lasting_header = self.normal_creds.get_authorization_header( self.topics[0]) self.expiring_creds = \ TokenCredentials(self.key_path, self.key_id, self.team_id, token_lifetime=self.token_lifetime) self.expiring_header = self.expiring_creds.get_authorization_header( self.topics[0])
def token_credentials(): return TokenCredentials( auth_key_path='test/eckey.pem', auth_key_id='1QBCDJ9RST', team_id='3Z24IP123A', token_lifetime=30, # seconds )
def __init__(self, sandbox=True, yaml_tokens=False): self.sandbox = sandbox self.yaml = yaml_tokens self.payload = None self.tokens = [] with open("app.yaml") as app_file: try: app_data = yaml.safe_load(app_file) except yaml.YAMLError: sys.exit(yaml.YAMLError) else: self.config = { "auth-key": app_data["auth-key"], "auth-key-filename": app_data["auth-key-filename"], "bundle-id": app_data["bundle-id"], "cert-filename": app_data["cert-filename"], "team-id": app_data["team-id"], "api-url": app_data["api-url"] + "/tokens/" + app_data["bundle-id"] } self.credentials = TokenCredentials(auth_key_path=self.config["auth-key-filename"], auth_key_id=self.config["auth-key"], team_id=self.config["team-id"]) self.client = APNsClient(credentials=self.credentials, use_sandbox=self.sandbox) self.load_tokens()
def setUp(self): # Create an 'ephemeral' token so we can test token timeouts. We # want a timeout long enough to last the test, but we don't want to # slow down the tests too much either. self.normal_creds = TokenCredentials(self.key_path, self.key_id, self.team_id) self.lasting_header = self.normal_creds.get_authorization_header( self.topics[0]) with freeze_time('2012-01-14'): self.expiring_creds = \ TokenCredentials(self.key_path, self.key_id, self.team_id, token_lifetime=self.token_lifetime) self.expiring_header = self.expiring_creds.get_authorization_header( self.topics[0])
def __init__(self, topic, auth_key_path, auth_key_id, team_id, use_sandbox=False): self.topic = topic self.client = APNsClient(TokenCredentials(auth_key_path, auth_key_id, team_id), use_sandbox=use_sandbox, use_alternative_port=False)
class TokenCredentialsTestCase(TestCase): @classmethod def setUpClass(cls): cls.key_path = 'test/eckey.pem' cls.team_id = '3Z24IP123A' cls.key_id = '1QBCDJ9RST' cls.topics = ( 'com.example.first_app', 'com.example.second_app', ) cls.token_lifetime = 0.5 def setUp(self): # Create an 'ephemeral' token so we can test token timeouts. We # want a timeout long enough to last the test, but we don't want to # slow down the tests too much either. self.normal_creds = TokenCredentials(self.key_path, self.key_id, self.team_id) self.lasting_header = self.normal_creds.get_authorization_header( self.topics[0]) with freeze_time('2012-01-14'): self.expiring_creds = \ TokenCredentials(self.key_path, self.key_id, self.team_id, token_lifetime=self.token_lifetime) self.expiring_header = self.expiring_creds.get_authorization_header( self.topics[0]) def test_token_expiration(self): # As long as the token lifetime hasn't elapsed, this should work. To # be really careful, we should check how much time has elapsed to # know if it fail. But, either way, we'd have to come up with a good # lifetime for future tests... time.sleep(self.token_lifetime) h3 = self.expiring_creds.get_authorization_header(self.topics[0]) self.assertNotEqual(self.expiring_header, h3)
def send_notification(user_id, st_code, condition, curr_value): logger.info("User to notify: %s" % user_id) notif_flag = False # Get the real name of the station station_name = Station.query.filter(Station.code == st_code).one().name logger.info("Station to be notified about: %s" % station_name) # Get the notification token from the users table token_hex = User.query.filter(User.device_id == user_id).one().notif_token logger.info("Notification token: %s" % token_hex) # Get the key, key path and team_id from the constants file and build # token credentials object token_credentials = TokenCredentials(auth_key_path=NOTIF_AUTH_KEY_PATH, auth_key_id=NOTIF_AUTH_KEY_ID, team_id=NOTIF_TEAM_ID) # Compose the text notif_text = notif_texts_format[condition['dimension']][condition['quantifier']] % \ (station_name,condition['value'],float(curr_value)) logger.info("Sending notification: %s" % notif_text) # Compose the dict with the station code and station name custom_data = {'station_code': st_code, 'station_name': station_name} topic = NOTIF_TOPIC payload = Payload(alert=notif_text, sound="default", badge=0, custom=custom_data) client = APNsClient(credentials=token_credentials, use_sandbox=True) try: #apns.gateway_server.send_notification(token_hex, payload,identifier=identifier) client.send_notification(token_hex, payload, topic) mark_as_notified(user_id, st_code, condition) logger.info("Notification sent!") notif_flag = True except: logger.error("Error sending notification!!") print("ERROR ENVIANDO NOTIFICACION") return notif_flag
def get_credentials(self): if WALLETPASS_CONF['PUSH_AUTH_STRATEGY'] == 'token': return TokenCredentials( auth_key_path=WALLETPASS_CONF['TOKEN_AUTH_KEY_PATH'], auth_key_id=WALLETPASS_CONF['TOKEN_AUTH_KEY_ID'], team_id=WALLETPASS_CONF['TEAM_ID'], ) # legacy cert/key auth context = init_context( cert=( WALLETPASS_CONF['CERT_PATH'], WALLETPASS_CONF['KEY_PATH'], ), # cert_path=WALLETPASS_CONF['APPLE_WWDRCA_PEM_PATH'], cert_password=WALLETPASS_CONF['KEY_PASSWORD'], ) return Credentials(context)
class TokenCredentialsTestCase(TestCase): @classmethod def setUpClass(cls): cls.key_path = 'test/eckey.pem' cls.team_id = '3Z24IP123A' cls.key_id = '1QBCDJ9RST' cls.topics = ('com.example.first_app', 'com.example.second_app',) cls.token_lifetime = 0.5 def setUp(self): # Create an 'ephemeral' token so we can test token timeouts. We # want a timeout long enough to last the test, but we don't want to # slow down the tests too much either. self.normal_creds = TokenCredentials(self.key_path, self.key_id, self.team_id) self.lasting_header = self.normal_creds.get_authorization_header( self.topics[0]) with freeze_time('2012-01-14'): self.expiring_creds = \ TokenCredentials(self.key_path, self.key_id, self.team_id, token_lifetime=self.token_lifetime) self.expiring_header = self.expiring_creds.get_authorization_header( self.topics[0]) def test_create_multiple_topics(self): h1 = self.normal_creds.get_authorization_header(self.topics[0]) self.assertEqual(len(self.normal_creds.get_tokens()), 1) h2 = self.normal_creds.get_authorization_header(self.topics[1]) self.assertNotEqual(h1, h2) self.assertEqual(len(self.normal_creds.get_tokens()), 2) def test_token_expiration(self): # As long as the token lifetime hasn't elapsed, this should work. To # be really careful, we should check how much time has elapsed to # know if it fail. But, either way, we'd have to come up with a good # lifetime for future tests... time.sleep(self.token_lifetime) h3 = self.expiring_creds.get_authorization_header(self.topics[0]) self.assertNotEqual(self.expiring_header, h3)
def sendMultipleNotifications(tokens, title, body, flightString, dateString): costumDict = { "flightString": flightString, "dateString": dateString, } payload = Payload(alert=PayloadAlert(title=title, body=body), custom=costumDict, sound="default", badge=0) topic = 'com.MihaStrah.FlightTracker' token_credentials = TokenCredentials( auth_key_path="AppleAuthentication/AuthKey_85KZTANBJ8.p8", auth_key_id="85KZTANBJ8", team_id="7YNLV7443U") client = APNsClient(credentials=token_credentials, use_sandbox=False) Notification = collections.namedtuple('Notification', ['token', 'payload']) notifications = [] for token in tokens: logger.info("notification sent to APNS") notifications.append(Notification(payload=payload, token=token)) #pošljemo skupek obvestil (za vse uporabnike, ki so naročeni na let) client.send_notification_batch(notifications=notifications, topic=topic)
def token_credentials(): return TokenCredentials('test/eckey.pem', '1QBCDJ9RST', '3Z24IP123A')