<commit_before># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import calendar
import logging
import sqlalchemy as sa
import wsme

from flask import Blueprint
from flask import g
from flask import url_for
from flask.ext.login import current_user
from flask.ext.login import login_required
from relengapi import apimethod
from relengapi import p
from relengapi.blueprints.tokenauth import loader
from relengapi.blueprints.tokenauth import tables
from relengapi.blueprints.tokenauth import tokenstr
from relengapi.blueprints.tokenauth import types
from relengapi.lib import angular
from relengapi.lib import api
from relengapi.lib import auth
from relengapi.util import tz
from werkzeug.exceptions import BadRequest
from werkzeug.exceptions import Forbidden
from werkzeug.exceptions import NotFound

logger = logging.getLogger(__name__)
bp = Blueprint('tokenauth', __name__,
               template_folder='templates',
               static_folder='static')

p.base.tokens.prm.view.doc('See permanent tokens')
p.base.tokens.prm.issue.doc('Issue permanent tokens')
p.base.tokens.prm.revoke.doc('Revoke permanent tokens')

p.base.tokens.usr.view.all.doc('See all user tokens')
p.base.tokens.usr.view.my.doc('See my user tokens')
p.base.tokens.usr.issue.doc('Issue user tokens')
p.base.tokens.usr.revoke.all.doc('Revoke any user token')
p.base.tokens.usr.revoke.my.doc('Revoke my user tokens')

p.base.tokens.tmp.issue.doc('Issue temporary tokens')


def any_token_perm():
    for prm in [
        p.base.tokens.prm.view,
        p.base.tokens.prm.issue,
        p.base.tokens.prm.revoke,
        p.base.tokens.tmp.issue,
        p.base.tokens.usr.view.all,
        p.base.tokens.usr.view.my,
        p.base.tokens.usr.issue,
        p.base.tokens.usr.revoke.all,
        p.base.tokens.usr.revoke.my,
    ]:
        if prm.can():
            return True

bp.root_widget_template(
    'tokenauth_root_widget.html', priority=100, condition=any_token_perm)


def user_to_jsontoken(user):
    attrs = {}
    cl = user.claims
    attrs['typ'] = user.claims['typ']
    if 'nbf' in cl:
        attrs['not_before'] = tz.utcfromtimestamp(cl['nbf'])
    if 'exp' in cl:
        attrs['expires'] = tz.utcfromtimestamp(cl['exp'])
    if 'mta' in cl:
        attrs['metadata'] = cl['mta']
    if 'prm' in cl:
        attrs['permissions'] = cl['prm']
    # TODO: client_id

    if user.token_data:
        td = user.token_data
        attrs['id'] = td.id
        attrs['description'] = td.description
        attrs['permissions'] = [str(p) for p in td.permissions]
        if td.user:
            attrs['user'] = td.user

    return types.JsonToken(**attrs)


def get_user_email():
    try:
        return current_user.authenticated_email
    except AttributeError:
        return None
<commit_msg>remove TODO note<commit_after># This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import calendar
import logging
import sqlalchemy as sa
import wsme

from flask import Blueprint
from flask import g
from flask import url_for
from flask.ext.login import current_user
from flask.ext.login import login_required
from relengapi import apimethod
from relengapi import p
from relengapi.blueprints.tokenauth import loader
from relengapi.blueprints.tokenauth import tables
from relengapi.blueprints.tokenauth import tokenstr
from relengapi.blueprints.tokenauth import types
from relengapi.lib import angular
from relengapi.lib import api
from relengapi.lib import auth
from relengapi.util import tz
from werkzeug.exceptions import BadRequest
from werkzeug.exceptions import Forbidden
from werkzeug.exceptions import NotFound

logger = logging.getLogger(__name__)
bp = Blueprint('tokenauth', __name__,
               template_folder='templates',
               static_folder='static')

p.base.tokens.prm.view.doc('See permanent tokens')
p.base.tokens.prm.issue.doc('Issue permanent tokens')
p.base.tokens.prm.revoke.doc('Revoke permanent tokens')

p.base.tokens.usr.view.all.doc('See all user tokens')
p.base.tokens.usr.view.my.doc('See my user tokens')
p.base.tokens.usr.issue.doc('Issue user tokens')
p.base.tokens.usr.revoke.all.doc('Revoke any user token')
p.base.tokens.usr.revoke.my.doc('Revoke my user tokens')

p.base.tokens.tmp.issue.doc('Issue temporary tokens')


def any_token_perm():
    for prm in [
        p.base.tokens.prm.view,
        p.base.tokens.prm.issue,
        p.base.tokens.prm.revoke,
        p.base.tokens.tmp.issue,
        p.base.tokens.usr.view.all,
        p.base.tokens.usr.view.my,
        p.base.tokens.usr.issue,
        p.base.tokens.usr.revoke.all,
        p.base.tokens.usr.revoke.my,
    ]:
        if prm.can():
            return True

bp.root_widget_template(
    'tokenauth_root_widget.html', priority=100, condition=any_token_perm)


def user_to_jsontoken(user):
    attrs = {}
    cl = user.claims
    attrs['typ'] = user.claims['typ']
    if 'nbf' in cl:
        attrs['not_before'] = tz.utcfromtimestamp(cl['nbf'])
    if 'exp' in cl:
        attrs['expires'] = tz.utcfromtimestamp(cl['exp'])
    if 'mta' in cl:
        attrs['metadata'] = cl['mta']
    if 'prm' in cl:
        attrs['permissions'] = cl['prm']

    if user.token_data:
        td = user.token_data
        attrs['id'] = td.id
        attrs['description'] = td.description
        attrs['permissions'] = [str(p) for p in td.permissions]
        if td.user:
            attrs['user'] = td.user

    return types.JsonToken(**attrs)


def get_user_email():
    try:
        return current_user.authenticated_email
    except AttributeError:
        return None

{'fraction_non_alphanumeric': 0.11261338353585487, 'fraction_numerical': 0.002396029436933082, 'mean_word_length': 3.234782608695652, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5313906', 'n_tokens_mistral': 1925, 'n_tokens_neox': 1874, 'n_words': 478}