Esempio n. 1
0
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']

    # we never load disabled users, so this one isn't disabled
    attrs['disabled'] = False

    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)
Esempio n. 2
0
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']

    # we never load disabled users, so this one isn't disabled
    attrs['disabled'] = False

    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)
Esempio n. 3
0
def issue_tmp(body, requested_permissions):
    if body.not_before:
        raise BadRequest("do not specify not_before when creating a tmp token")
    nbf = int(time.time())
    exp = calendar.timegm(body.expires.utctimetuple())
    if exp <= nbf:
        raise BadRequest("expiration time must be in the future")
    max_lifetime = current_app.config.get("RELENGAPI_TMP_TOKEN_MAX_LIFETIME",
                                          86400)
    if exp > nbf + max_lifetime:
        raise BadRequest(
            "expiration time is more than %d seconds in the future" %
            max_lifetime)
    perm_strs = [str(prm) for prm in requested_permissions]
    token = tokenstr.claims_to_str({
        'iss': 'ra2',
        'typ': 'tmp',
        'nbf': nbf,
        'exp': exp,
        'prm': perm_strs,
        'mta': body.metadata,
    })
    return types.JsonToken(typ='tmp',
                           token=token,
                           not_before=tz.utcfromtimestamp(nbf),
                           expires=body.expires,
                           permissions=perm_strs,
                           metadata=body.metadata,
                           disabled=False)
Esempio n. 4
0
def issue_tmp(body, requested_permissions):
    if body.not_before:
        raise BadRequest("do not specify not_before when creating a tmp token")
    nbf = int(time.time())
    exp = calendar.timegm(body.expires.utctimetuple())
    if exp <= nbf:
        raise BadRequest("expiration time must be in the future")
    max_lifetime = current_app.config.get(
        "RELENGAPI_TMP_TOKEN_MAX_LIFETIME", 86400)
    if exp > nbf + max_lifetime:
        raise BadRequest("expiration time is more than %d seconds in the future" %
                         max_lifetime)
    perm_strs = [str(prm) for prm in requested_permissions]
    token = tokenstr.claims_to_str({
        'iss': 'ra2',
        'typ': 'tmp',
        'nbf': nbf,
        'exp': exp,
        'prm': perm_strs,
        'mta': body.metadata,
    })
    return types.JsonToken(typ='tmp', token=token,
                           not_before=tz.utcfromtimestamp(nbf),
                           expires=body.expires,
                           permissions=perm_strs,
                           metadata=body.metadata,
                           disabled=False)
Esempio n. 5
0
def test_utcfromtimestamp():
    timestamp = 1401240762.0  # UTC ver of datetime(2014, 5, 28, 1, 32, 42)
    dt = datetime.datetime.utcfromtimestamp(timestamp)
    util_dt = tz.utcfromtimestamp(timestamp)
    eq_(util_dt.tzinfo, pytz.UTC)
    eq_(util_dt.replace(tzinfo=None), dt)
Esempio n. 6
0
def test_utcfromtimestamp():
    timestamp = 1401240762.0  # UTC ver of datetime(2014, 5, 28, 1, 32, 42)
    dt = datetime.datetime.utcfromtimestamp(timestamp)
    util_dt = tz.utcfromtimestamp(timestamp)
    eq_(util_dt.tzinfo, pytz.UTC)
    eq_(util_dt.replace(tzinfo=None), dt)