<commit_before># -*- coding: utf-8 -*-
#
#    2019-02-12 <NAME> <<EMAIL>>
#
#
#    This program is free software: you can redistribute it and/or
#    modify it under the terms of the GNU Affero General Public
#    License, version 3, as published by the Free Software Foundation.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the
#               GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#

__doc__ = """This is the provider class that communicates with Googles
Firebase Cloud Messaging Service.
This provider is used for the push token and can be used for SMS tokens.
"""

from privacyidea.lib.smsprovider.SMSProvider import (ISMSProvider)
from privacyidea.lib.error import ConfigAdminError
from privacyidea.lib.framework import get_app_local_store
from privacyidea.lib import _
import logging
from oauth2client.service_account import ServiceAccountCredentials
import requests
import json
import time
import datetime

FIREBASE_URL_SEND = 'https://fcm.googleapis.com/v1/projects/{0!s}/messages:send'
SCOPES = ['https://www.googleapis.com/auth/cloud-platform',
          'https://www.googleapis.com/auth/datastore',
          'https://www.googleapis.com/auth/devstorage.read_write',
          'https://www.googleapis.com/auth/firebase',
          'https://www.googleapis.com/auth/identitytoolkit',
          'https://www.googleapis.com/auth/userinfo.email']

log = logging.getLogger(__name__)


class AccessToken(object):

    def __init__(self, access_token, validity):
        self.access_token = access_token
        self.expires_at = time.time() + validity - 10


def get_firebase_access_token(config_file_name):
    """
    This returns the access token for a given JSON config file name

    :param config_file_name:
    :return:
    """
    fbt = "firebase_token"
    now = time.time()
    app_store = get_app_local_store()

    if fbt not in app_store or not isinstance(app_store[fbt], dict):
        # initialize the firebase_token in the app_store as dict
        app_store[fbt] = {}

    if not isinstance(app_store[fbt].get(config_file_name), AccessToken) or \
            now > app_store[fbt].get(config_file_name).expires_at:
        # If the type of the config is not class AccessToken or
        # if the token has expired
        credentials = ServiceAccountCredentials.from_json_keyfile_name(config_file_name, SCOPES)
        log.debug("Fetching a new access_token for {!r} from firebase...".format(config_file_name))
        access_token_info = credentials.get_access_token()
        # Now we set the expiration date for the new access_token with a margin of 10 seconds
        At = AccessToken(access_token_info.access_token, access_token_info.expires_in)
        app_store[fbt][config_file_name] = At
        readable_time = datetime.datetime.fromtimestamp(At.expires_at).isoformat()
        log.debug(u"Setting the expiration for {!r} of the new access_token to {0!s}.".format(config_file_name, readable_time))

    return app_store[fbt][config_file_name].access_token

<commit_msg>Fix the log messages format string<commit_after># -*- coding: utf-8 -*-
#
#    2019-02-12 <NAME> <<EMAIL>>
#
#
#    This program is free software: you can redistribute it and/or
#    modify it under the terms of the GNU Affero General Public
#    License, version 3, as published by the Free Software Foundation.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the
#               GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#

__doc__ = """This is the provider class that communicates with Googles
Firebase Cloud Messaging Service.
This provider is used for the push token and can be used for SMS tokens.
"""

from privacyidea.lib.smsprovider.SMSProvider import (ISMSProvider)
from privacyidea.lib.error import ConfigAdminError
from privacyidea.lib.framework import get_app_local_store
from privacyidea.lib import _
import logging
from oauth2client.service_account import ServiceAccountCredentials
import requests
import json
import time
import datetime

FIREBASE_URL_SEND = 'https://fcm.googleapis.com/v1/projects/{0!s}/messages:send'
SCOPES = ['https://www.googleapis.com/auth/cloud-platform',
          'https://www.googleapis.com/auth/datastore',
          'https://www.googleapis.com/auth/devstorage.read_write',
          'https://www.googleapis.com/auth/firebase',
          'https://www.googleapis.com/auth/identitytoolkit',
          'https://www.googleapis.com/auth/userinfo.email']

log = logging.getLogger(__name__)


class AccessToken(object):

    def __init__(self, access_token, validity):
        self.access_token = access_token
        self.expires_at = time.time() + validity - 10


def get_firebase_access_token(config_file_name):
    """
    This returns the access token for a given JSON config file name

    :param config_file_name:
    :return:
    """
    fbt = "firebase_token"
    now = time.time()
    app_store = get_app_local_store()

    if fbt not in app_store or not isinstance(app_store[fbt], dict):
        # initialize the firebase_token in the app_store as dict
        app_store[fbt] = {}

    if not isinstance(app_store[fbt].get(config_file_name), AccessToken) or \
            now > app_store[fbt].get(config_file_name).expires_at:
        # If the type of the config is not class AccessToken or
        # if the token has expired
        credentials = ServiceAccountCredentials.from_json_keyfile_name(config_file_name, SCOPES)
        log.debug("Fetching a new access_token for {!r} from firebase...".format(config_file_name))
        access_token_info = credentials.get_access_token()
        # Now we set the expiration date for the new access_token with a margin of 10 seconds
        At = AccessToken(access_token_info.access_token, access_token_info.expires_in)
        app_store[fbt][config_file_name] = At
        readable_time = datetime.datetime.fromtimestamp(At.expires_at).isoformat()
        log.debug(u"Setting the expiration for {!r} of the new access_token to {!s}.".format(config_file_name, readable_time))

    return app_store[fbt][config_file_name].access_token


{'fraction_non_alphanumeric': 0.08979347500748279, 'fraction_numerical': 0.005237952708769829, 'mean_word_length': 3.583676268861454, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 14, 'lorem ipsum': 0, 'www.': 14, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4931713', 'n_tokens_mistral': 2001, 'n_tokens_neox': 1941, 'n_words': 589}