示例#1
0
文件: utils.py 项目: AndyKovv/hostel
def jwt_payload_handler(user):
    username_field = get_username_field()
    username = get_username(user)

    warnings.warn(
        'The following fields will be removed in the future: '
        '`email` and `user_id`. ',
        DeprecationWarning
    )

    payload = {
        'user_id': user.pk,
        'email': user.email,
        'username': username,
        'exp': datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA
    }

    payload[username_field] = username

    # Include original issued at time for a brand new token,
    # to allow token refresh
    if api_settings.JWT_ALLOW_REFRESH:
        payload['orig_iat'] = timegm(
            datetime.utcnow().utctimetuple()
        )

    return payload
示例#2
0
def payload_handler(user, request):
    username_field = get_username_field()
    username = get_username(user)

    ip = get_ip(request)

    warnings.warn(
        'The following fields will be removed in the future: '
        '`email` and `user_id`. ', DeprecationWarning)

    payload = {
        'user_id': user.pk,
        'username': username,
        'ip': ip,
        'exp': datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA
    }
    if hasattr(user, 'email'):
        payload['email'] = user.email
    if isinstance(user.pk, uuid.UUID):
        payload['user_id'] = str(user.pk)

    payload[username_field] = username

    if api_settings.JWT_ALLOW_REFRESH:
        payload['orig_iat'] = timegm(datetime.utcnow().utctimetuple())

    if api_settings.JWT_AUDIENCE is not None:
        payload['aud'] = api_settings.JWT_AUDIENCE

    if api_settings.JWT_ISSUER is not None:
        payload['iss'] = api_settings.JWT_ISSUER

    return payload
示例#3
0
def jwt_otp_payload(user, device = None):
    """
    Opcionalmente inclui o Device TOP no payload do JWT
    """
    username_field = get_username_field()
    username = get_username(user)
        
    payload = {
        'user_id': user.pk,
        'username': username,
        'exp': datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA
    }
        
    # Include original issued at time for a brand new token,
    # to allow token refresh
    if api_settings.JWT_ALLOW_REFRESH:
        payload['orig_iat'] = timegm(
            datetime.utcnow().utctimetuple()
        )

    if api_settings.JWT_AUDIENCE is not None:
        payload['aud'] = api_settings.JWT_AUDIENCE

    if api_settings.JWT_ISSUER is not None:
        payload['iss'] = api_settings.JWT_ISSUER

    return payload
示例#4
0
def jwt_payload_handler(user):
    username_field = get_username_field()
    username = get_username(user)

    warnings.warn(
        'The following fields will be removed in the future: '
        '`email` and `user_id`. ',
        DeprecationWarning
    )
    if not user.is_verified:
        msg = _('Please verify your account.')
        raise exceptions.AuthenticationFailed(msg)

    payload = {
        'user_id': str(user.id.hex),
        'alias': user.username,
        'exp': datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA
    }

    # Include original issued at time for a brand new token,
    # to allow token refresh
    if api_settings.JWT_ALLOW_REFRESH:
        payload['orig_iat'] = timegm(
            datetime.utcnow().utctimetuple()
        )

    if api_settings.JWT_AUDIENCE is not None:
        payload['aud'] = api_settings.JWT_AUDIENCE

    if api_settings.JWT_ISSUER is not None:
        payload['iss'] = api_settings.JWT_ISSUER

    return payload
示例#5
0
def jwt_payload_handler(user):    
    username_field = get_username_field()
    username = get_username(user)

    payload = {
        'user_id': user.pk,
        'username': username,
        'exp': datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA
    }
    if hasattr(user, 'email'):
        payload['email'] = user.email
    if isinstance(user.pk, uuid.UUID):
        payload['user_id'] = str(user.pk)

    payload[username_field] = username

    # Include original issued at time for a brand new token,
    # to allow token refresh
    if api_settings.JWT_ALLOW_REFRESH:
        payload['orig_iat'] = timegm(
            datetime.utcnow().utctimetuple()
        )

    if api_settings.JWT_AUDIENCE is not None:
        payload['aud'] = api_settings.JWT_AUDIENCE

    if api_settings.JWT_ISSUER is not None:
        payload['iss'] = api_settings.JWT_ISSUER

    return payload
示例#6
0
def jwt_payload_handler(user):
    username_field = get_username_field()
    username = get_username(user)

    warnings.warn(
        'The following fields will be removed in the future: '
        '`email` and `user_id`. ',
        DeprecationWarning
    )

    payload = {
        'user_id': user.pk,
        'username': username,
        'exp': datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA
    }

    if hasattr(user, 'email'):
        payload['email'] = user.email

    if isinstance(user.pk, uuid.UUID):
        payload['user_id'] = str(user.pk)

    payload[username_field] = username

    return jwt_setup_payload_refresh(payload)
示例#7
0
def jwt_payload_handler(user):
    username_field = get_username_field()
    username = get_username(user)

    warnings.warn(
        'The following fields will be removed in the future: '
        '`email` and `user_id`. ',
        DeprecationWarning
    )

    payload = {
        'user_id': user.pk,
        'name': user.name,
        'username': username,
        'exp': datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA
    }
    if isinstance(user.pk, uuid.UUID):
        payload['user_id'] = str(user.pk)

    payload[username_field] = username

    # Include original issued at time for a brand new token,
    # to allow token refresh
    if api_settings.JWT_ALLOW_REFRESH:
        payload['orig_iat'] = timegm(
            datetime.utcnow().utctimetuple()
        )

    if api_settings.JWT_AUDIENCE is not None:
        payload['aud'] = api_settings.JWT_AUDIENCE

    if api_settings.JWT_ISSUER is not None:
        payload['iss'] = api_settings.JWT_ISSUER

    return payload
示例#8
0
def jwt_payload_handler(user, orig_iat=None):
    username_field = get_username_field()
    username = get_username(user)

    payload = {
        "id": user.pk,
        "username": username,
        "exp": datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA,
    }
    if hasattr(user, "email"):
        payload["email"] = user.email
    if isinstance(user.pk, uuid.UUID):
        payload["id"] = str(user.pk)

    payload[username_field] = username

    # Include original issued at time for a brand new token,
    # to allow token refresh
    if api_settings.JWT_ALLOW_REFRESH:
        payload["orig_iat"] = orig_iat or timegm(
            datetime.utcnow().utctimetuple())

    if api_settings.JWT_AUDIENCE is not None:
        payload["aud"] = api_settings.JWT_AUDIENCE

    if api_settings.JWT_ISSUER is not None:
        payload["iss"] = api_settings.JWT_ISSUER

    return payload
def jwt_payload_handler(user):
    username_field = get_username_field()
    username = get_username(user)

    warnings.warn("The following fields will be removed in the future: " "`email` and `user_id`. ", DeprecationWarning)

    payload = {
        "user_id": user.pk,
        "email": user.email,
        "username": username,
        "exp": datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA,
    }
    if isinstance(user.pk, uuid.UUID):
        payload["user_id"] = str(user.pk)

    payload[username_field] = username

    # Include original issued at time for a brand new token,
    # to allow token refresh
    if api_settings.JWT_ALLOW_REFRESH:
        payload["orig_iat"] = timegm(datetime.utcnow().utctimetuple())

    if api_settings.JWT_AUDIENCE is not None:
        payload["aud"] = api_settings.JWT_AUDIENCE

    if api_settings.JWT_ISSUER is not None:
        payload["iss"] = api_settings.JWT_ISSUER

    return payload
def jwt_payload_handler(user):
    username_field = get_username_field()
    username = get_username(user)

    payload = {
        'user_id': user.pk,
        'email': user.email,
        'username': username,
        'user_type': user.user_type,
        'approved_to_post_events': user.approved_to_post_events,
        'exp': datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA
    }

    payload[username_field] = username

    # Include original issued at time for a brand new token,
    # to allow token refresh
    if api_settings.JWT_ALLOW_REFRESH:
        payload['orig_iat'] = timegm(datetime.utcnow().utctimetuple())

    if api_settings.JWT_AUDIENCE is not None:
        payload['aud'] = api_settings.JWT_AUDIENCE

    if api_settings.JWT_ISSUER is not None:
        payload['iss'] = api_settings.JWT_ISSUER

    return payload
示例#11
0
def jwt_payload_handler(user):
    username_field = get_username_field()
    username = get_username(user)

    warnings.warn(
        'The following fields will be removed in the future: '
        '`email` and `user_id`. ', DeprecationWarning)

    payload = {
        'user_id': user.pk,
        'email': user.email,
        'username': username,
        'exp': datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA
    }
    if isinstance(user.pk, uuid.UUID):
        payload['user_id'] = str(user.pk)

    payload[username_field] = username

    # Include original issued at time for a brand new token,
    # to allow token refresh
    if api_settings.JWT_ALLOW_REFRESH:
        payload['orig_iat'] = timegm(datetime.utcnow().utctimetuple())

    if api_settings.JWT_AUDIENCE is not None:
        payload['aud'] = api_settings.JWT_AUDIENCE

    if api_settings.JWT_ISSUER is not None:
        payload['iss'] = api_settings.JWT_ISSUER

    return payload
示例#12
0
def jwt_otp_payload(user, device=None):
    """
    Optionally include OTP device in JWT payload
    """
    username_field = get_username_field()
    username = get_username(user)

    payload = {
        'user_id': user.pk,
        'username': username,
        'exp': datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA
    }

    # Include original issued at time for a brand new token,
    # to allow token refresh
    if api_settings.JWT_ALLOW_REFRESH:
        payload['orig_iat'] = timegm(datetime.utcnow().utctimetuple())

    if api_settings.JWT_AUDIENCE is not None:
        payload['aud'] = api_settings.JWT_AUDIENCE

    if api_settings.JWT_ISSUER is not None:
        payload['iss'] = api_settings.JWT_ISSUER

    # custom additions
    # if (user is not None) and (device is not None) and (device.user_id == user.id) and (device.confirmed is True):
    #    payload['otp_device_id'] = device.persistent_id
    # else:
    #    payload['otp_device_id'] = None

    return payload
示例#13
0
def jwt_payload_handler(user):
    username_field = get_username_field()
    # print(user['uuid'])

    # payload = {'user_id': user['uuid'], 'email': user['email'], 'username': user['email'],
    #            'exp': datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA, username_field: user['email']}

    payload = {
        'email': user.email,
        'password': user.password,
        'exp': datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA,
        username_field: user.email
    }
    # Include original issued at time for a brand new token,
    # to allow token refresh
    if api_settings.JWT_ALLOW_REFRESH:
        payload['orig_iat'] = timegm(datetime.utcnow().utctimetuple())

    if api_settings.JWT_AUDIENCE is not None:
        payload['aud'] = api_settings.JWT_AUDIENCE

    if api_settings.JWT_ISSUER is not None:
        payload['iss'] = api_settings.JWT_ISSUER

    return payload
示例#14
0
def session_payload(user, auth_2fa=False):
    username_field = get_username_field()
    username = get_username(user)

    warnings.warn(
        'The following fields will be removed in the future: '
        '`email` and `user_id`. ', DeprecationWarning)

    payload = {
        'user_id': user.pk,
        'username': sha1(username),
        'exp': datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA
    }

    if hasattr(user, 'email'):
        payload['email'] = user.email
    if isinstance(user.pk, uuid.UUID):
        payload['user_id'] = sha1(str(user.pk))

    payload[username_field] = username

    payload['tfa_enabled'] = user.get_user_security_setting(
    ).two_factor_authentication_enabled
    payload['tfa'] = auth_2fa

    if auth_2fa:
        payload['tfa_time_stamp'] = time.time()
    else:
        payload['tfa_time_stamp'] = None

    for i in range(1, 3):
        payload[random.randint(3, 10)] = sha1(random.randint(20, 32))

    return jwt_encode_handler(payload)
示例#15
0
def jwt_payload_handler(user, hijack=False):
    username_field = get_username_field()
    username = get_username(user)

    user.last_login = datetime.now()
    user.save()

    warnings.warn(
        'The following fields will be removed in the future: '
        '`email` and `user_id`. ', DeprecationWarning)

    payload = {
        'user_id': user.pk,
        'email': user.email,
        'exp': datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA
    }

    payload[username_field] = username

    if api_settings.JWT_AUDIENCE is not None:
        payload['aud'] = api_settings.JWT_AUDIENCE

    if api_settings.JWT_ISSUER is not None:
        payload['iss'] = api_settings.JWT_ISSUER

    return payload
示例#16
0
文件: jwt.py 项目: themother-lab/base
def payload_handler(user):

    username_field = get_username_field()
    username = get_username(user)

    payload = {
        'sub': username,
        'is_superuser': user.is_superuser,
        'groups': list(user.groups.all().values_list('name', flat=True)),
        'exp': datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA,
        'iss': api_settings.JWT_ISSUER
    }
    if isinstance(user.pk, uuid.UUID):
        payload['user_id'] = str(user.pk)

    payload[username_field] = username

    # Include original issued at time for a brand new token,
    # to allow token refresh
    if api_settings.JWT_ALLOW_REFRESH:
        payload['orig_iat'] = timegm(datetime.utcnow().utctimetuple())

    if api_settings.JWT_AUDIENCE is not None:
        payload['aud'] = api_settings.JWT_AUDIENCE

    if api_settings.JWT_ISSUER is not None:
        payload['iss'] = api_settings.JWT_ISSUER

    return payload
示例#17
0
文件: auth.py 项目: yummyweb/drf-user
def jwt_payload_handler(user):
    """
    A custom JWT Payload Handler that adds certain extra data in
    payload such as: email, mobile, name

    Source: Himanshu Shankar (https://github.com/iamhssingh)
    Parameters
    ----------
    user: get_user_model()

    Returns
    -------
    payload: dict
    """

    import uuid

    from calendar import timegm
    from datetime import datetime

    from rest_framework_jwt.compat import get_username
    from rest_framework_jwt.compat import get_username_field
    from rest_framework_jwt.settings import api_settings

    username_field = get_username_field()
    username = get_username(user)

    payload = {
        "user_id": user.pk,
        "is_admin": user.is_staff,
        "exp": datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA,
    }

    if hasattr(user, "email"):
        payload["email"] = user.email

    if hasattr(user, "mobile"):
        payload["mobile"] = user.mobile

    if hasattr(user, "name"):
        payload["name"] = user.name

    if isinstance(user.pk, uuid.UUID):
        payload["user_id"] = str(user.pk)

    payload[username_field] = username

    # Include original issued at time for a brand new token,
    # to allow token refresh

    if api_settings.JWT_ALLOW_REFRESH:
        payload["orig_iat"] = timegm(datetime.utcnow().utctimetuple())

    if api_settings.JWT_AUDIENCE is not None:
        payload["aud"] = api_settings.JWT_AUDIENCE

    if api_settings.JWT_ISSUER is not None:
        payload["iss"] = api_settings.JWT_ISSUER

    return payload
示例#18
0
def jwt_payload_handler(user):
    username_field = get_username_field()
    username = get_username(user)

    payload = {
        'user_id': user.pk,
        'username': username,
        'exp': datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA
    }
    if isinstance(user.pk, uuid.UUID):
        payload['user_id'] = str(user.pk)

    payload[username_field] = username

    if api_settings.JWT_ALLOW_REFRESH:
        payload['orig_iat'] = timegm(
            datetime.utcnow().utctimetuple()
        )

    if api_settings.JWT_AUDIENCE is not None:
        payload['aud'] = api_settings.JWT_AUDIENCE

    if api_settings.JWT_ISSUER is not None:
        payload['iss'] = api_settings.JWT_ISSUER

    return payload
示例#19
0
def jwt_payload_handler(user):
    username_field = get_username_field()
    username = get_username(user)

    warnings.warn(
        'The following fields will be removed in the future: '
        '`email` and `user_id`. ', DeprecationWarning)

    payload = {
        'user_id':
        user.pk,
        'company_id':
        user.company.id if user.company else None,
        'connected':
        True if user.stripe_connect else False,
        'verification':
        user.verification,
        'payouts_enabled':
        user.payouts_enabled,
        'payments_enabled':
        True if user.get_default_payment() else False,
        'email':
        user.email,
        'username':
        username,
        'photo':
        user.get_photo,
        'first_name':
        user.first_name,
        'last_name':
        user.last_name,
        'email_confirmed':
        user.email_confirmed,
        'tos':
        user.tos,
        #TODO subscribed flag hits db every time, refactor later
        'subscribed':
        True if Order.objects.filter(user=user, status='active') else False,
        'exp':
        datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA
    }
    if isinstance(user.pk, uuid.UUID):
        payload['user_id'] = str(user.pk)

    payload[username_field] = username

    # Include original issued at time for a brand new token,
    # to allow token refresh
    if api_settings.JWT_ALLOW_REFRESH:
        payload['orig_iat'] = timegm(datetime.utcnow().utctimetuple())

    if api_settings.JWT_AUDIENCE is not None:
        payload['aud'] = api_settings.JWT_AUDIENCE

    if api_settings.JWT_ISSUER is not None:
        payload['iss'] = api_settings.JWT_ISSUER

    return payload
示例#20
0
def jwt_payload_handler(user):
    username_field = get_username_field()
    username = get_username(user)
    try:
        org = Organization.objects.get(owner=user.pk)
        is_owner = True
        org_active = org.active
        org_billing_active = org.is_active()
        org_id = org.id
    except:
        org = Organization.objects.filter(members__id=user.pk)
        if org:
            is_owner = False
            org_id = org[0].id
            org_billing_active = org[0].is_active()
            org_active = org[0].active
        else:
            is_owner = False
            org_id = None
            org_billing_active = False
            org_active = False

    warnings.warn(
        'The following fields will be removed in the future: '
        '`email` and `user_id`. ', DeprecationWarning)

    payload = {
        'user_id': user.pk,
        'username': username,
        'is_superuser': user.is_superuser,
        'is_owner': is_owner,
        'org_id': org_id,
        'org_active': org_active,
        'org_billing_active': org_billing_active,
        'exp': datetime.datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA
    }
    if hasattr(user, 'email'):
        payload['email'] = user.email
    if isinstance(user.pk, uuid.UUID):
        payload['user_id'] = str(user.pk)

    payload[username_field] = username

    # Include original issued at time for a brand new token,
    # to allow token refresh
    if api_settings.JWT_ALLOW_REFRESH:
        payload['orig_iat'] = timegm(datetime.datetime.utcnow().utctimetuple())

    if api_settings.JWT_AUDIENCE is not None:
        payload['aud'] = api_settings.JWT_AUDIENCE

    if api_settings.JWT_ISSUER is not None:
        payload['iss'] = api_settings.JWT_ISSUER

    return payload
示例#21
0
def jwt_payload_handler(user):
    username_field = get_username_field()
    username = get_username(user)
    payload = {
        'alpha': False,
        'exp': datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA,
        'user_id': "{}".format(user.id),
        'user': UserSerializer(user).data,
    }
    payload[username_field] = username
    if api_settings.JWT_ALLOW_REFRESH:
        payload['orig_iat'] = timegm(datetime.utcnow().utctimetuple())
    return payload
示例#22
0
def payload_handler(user):
    """Function to generate the token payload.

    {
      "iss": "drf-saas-starter",
      "user_id": "4d92a809-4f68-4c19-b115-eaa1a0e5a170",
      "sub": "john",
      "email": "*****@*****.**",
      "groups": [
        "HR",
        "Test"
      ],
      "exp": 1487970010,
      "is_superuser": true
    }

    Who this person is (sub, short for subject)
    What this person can access with this token (scope)
    When the token expires (exp)
    Who issued the token (iss, short for issuer)
    """

    username_field = get_username_field()
    username = get_username(user)

    payload = {
        'sub': username,
        'is_superuser': user.is_superuser,
        'groups': list(user.groups.all().values_list('name', flat=True)),
        'exp': datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA,
        'iss': api_settings.JWT_ISSUER
    }
    if isinstance(user.pk, uuid.UUID):
        payload['user_id'] = str(user.pk)

    payload[username_field] = username

    # Include original issued at time for a brand new token,
    # to allow token refresh
    if api_settings.JWT_ALLOW_REFRESH:
        payload['orig_iat'] = timegm(
            datetime.utcnow().utctimetuple()
        )

    if api_settings.JWT_AUDIENCE is not None:
        payload['aud'] = api_settings.JWT_AUDIENCE

    if api_settings.JWT_ISSUER is not None:
        payload['iss'] = api_settings.JWT_ISSUER

    return payload
示例#23
0
def jwt_payload_handler(user):
    """
    Custom payload for JWT tokens.
    """

    payload = UserSerializer(user).data
    payload["exp"] = datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA
    payload[get_username_field()] = get_username(user)

    # Include original issued at time for a brand new token,
    # to allow token refresh
    if api_settings.JWT_ALLOW_REFRESH:
        payload["orig_iat"] = timegm(datetime.utcnow().utctimetuple())

    return payload
示例#24
0
def jwt_payload_handler(user):
    username_field = get_username_field()
    username = get_username(user)

    warnings.warn(
        'The following fields will be removed in the future: '
        '`email` and `user_id`. ',
        DeprecationWarning
    )

    expiry_date = datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA

    prof=ProfileModel.objects.get(user__pk=user.pk)
    name = prof.name
    profilePic = settings.BASE_URL + prof.image.url
            
    payload = {
        'user_id': user.pk,
        'username': username,
        'exp': expiry_date,
        'name': name,
        'pic':profilePic
    }

    if hasattr(user, 'email'):
        payload['email'] = user.email
    if isinstance(user.pk, uuid.UUID):
        payload['user_id'] = str(user.pk)

    payload[username_field] = username

    # Include original issued at time for a brand new token,
    # to allow token refresh
    if api_settings.JWT_ALLOW_REFRESH:
        payload['orig_iat'] = timegm(
            datetime.utcnow().utctimetuple()
        )

    if api_settings.JWT_AUDIENCE is not None:
        payload['aud'] = api_settings.JWT_AUDIENCE

    if api_settings.JWT_ISSUER is not None:
        payload['iss'] = api_settings.JWT_ISSUER

    # print(groupsRecieved.data[0])
    # payload['position']=groupsRecieved[0]['name']

    return payload
示例#25
0
def jwt_payload_handler(user=None, extra_data=None):
    extra_data = extra_data or {}
    username_field = get_username_field()

    email = extra_data.pop('email', '')
    if user is None:
        user_id = extra_data.pop('user_id', '')
        username = extra_data.pop('username', '')
    else:
        user_id = user.id
        username = get_username(user)
        if hasattr(user, 'email'):
            email = user.email

    if isinstance(user_id, uuid.UUID):
        user_id = str(user.pk)

    warnings.warn(
        'The following fields will be removed in the future: '
        '`email` and `user_id`. ',
        DeprecationWarning
    )

    payload = extra_data.copy()
    payload.update({
        'user_id': user_id,
        'username': username,
        'exp': datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA
    })
    if email:
        payload['email'] = email

    payload[username_field] = username

    # Include original issued at time for a brand new token,
    # to allow token refresh
    if api_settings.JWT_ALLOW_REFRESH:
        payload['orig_iat'] = timegm(
            datetime.utcnow().utctimetuple()
        )

    if api_settings.JWT_AUDIENCE is not None:
        payload['aud'] = api_settings.JWT_AUDIENCE

    if api_settings.JWT_ISSUER is not None:
        payload['iss'] = api_settings.JWT_ISSUER

    return payload
示例#26
0
def jwt_payload_handler(user):
    username_field = get_username_field()
    username = get_username(user)

    warnings.warn(
        'The following fields will be removed in the future: '
        '`email` and `user_id`. ',
        DeprecationWarning
    )

    payload = {
        'user_id': user.pk,
        'email': user.email,
        'username': username,
        'display_name': user.display_name,
        'first_name': user.first_name,
        'phone_number': user.phone_number,
        'gender': user.gender,
        'birth_day': user.birth_day.strftime('%Y-%m-%d') if user.birth_day else '',
        'last_name': user.last_name,
        'avatar': user.avatar.url if user.avatar else '',
        'is_staff': user.is_staff,
        'is_social': True if user.socialaccount_set.values() else False,
        'exp': datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA
    }
    if isinstance(user.pk, uuid.UUID):
        payload['user_id'] = str(user.pk)

    payload[username_field] = username

    # Include original issued at time for a brand new token,
    # to allow token refresh
    if api_settings.JWT_ALLOW_REFRESH:
        payload['orig_iat'] = timegm(
            datetime.utcnow().utctimetuple()
        )

    if api_settings.JWT_AUDIENCE is not None:
        payload['aud'] = api_settings.JWT_AUDIENCE

    if api_settings.JWT_ISSUER is not None:
        payload['iss'] = api_settings.JWT_ISSUER

    # TO-DO: Adding log using NLog
    if api_settings.JWT_ISSUER == "demo":
        pass

    return payload
示例#27
0
def jwt_payload_handler(user):
    """
    Custom payload for JWT tokens.
    """

    payload = UserSerializer(user).data
    payload["exp"] = datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA
    payload[get_username_field()] = get_username(user)

    # Include original issued at time for a brand new token,
    # to allow token refresh
    if api_settings.JWT_ALLOW_REFRESH:
        payload["orig_iat"] = timegm(
            datetime.utcnow().utctimetuple()
        )

    return payload
def jwt_payload_handler(user):

    username_field = get_username_field()
    username = get_username(user)

    payload = {
        'user_id': user.pk,
        'username': username,
        'profile_pic': user.profile_pic.url,
        'exp': datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA
    }
    if hasattr(user, 'fullname'):
        payload['fullname'] = user.fullname

    payload[username_field] = username

    return payload
示例#29
0
def jwt_get_secret_key(payload=None):
    """
    For enhanced security you may want to use a secret key based on user.

    This way you have an option to logout only this user if:
        - token is compromised
        - password is changed
        - etc.
    """
    if api_settings.JWT_GET_USER_SECRET_KEY:
        User = get_user_model()  # noqa: N806
        username_field = get_username_field()
        username = payload.get(username_field)
        user = User.objects.get_by_natural_key(username)
        key = str(api_settings.JWT_GET_USER_SECRET_KEY(user))
        return key
    return api_settings.JWT_SECRET_KEY
    def _check_user(self, username: str):
        username_field = get_username_field()

        # Make sure user exists
        try:
            user = _UserModel.objects.get(**{username_field: username})
        except _UserModel.DoesNotExist:
            raise serializers.ValidationError(_("User doesn't exist."))
        except _UserModel.MultipleObjectsReturned:
            logging.exception(
                "Improperly configured username field: username should be unique"
            )
            raise serializers.ValidationError(_("User cannot be determined."))

        if not user.is_active:
            raise serializers.ValidationError(_('User account is disabled.'))

        return user
示例#31
0
def jwt_payload_handler(user):
    username_field = get_username_field()
    username = get_username(user)

    warnings.warn(
        'The following fields will be removed in the future: '
        '`email` and `user_id`. ',
        DeprecationWarning
    )

    payload = {
        'user_id': user.pk,
        'email': user.email,
        'username': username,
        'exp': datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA
    }

    payload[username_field] = username

    # Include original issued at time for a brand new token,
    # to allow token refresh
    if api_settings.JWT_ALLOW_REFRESH:
        payload['orig_iat'] = timegm(
            datetime.utcnow().utctimetuple()
        )

    if api_settings.JWT_AUDIENCE is not None:
        payload['aud'] = api_settings.JWT_AUDIENCE

    if api_settings.JWT_ISSUER is not None:
        payload['iss'] = api_settings.JWT_ISSUER

    payload['phone'] = user.profile.phone
    payload['type'] = user.profile.type
    payload['picture'] = user.profile.picture
    payload['swift_bank_account'] = user.profile.swift_bank_account
    payload['iban_bank_account'] = user.profile.iban_bank_account
    payload['documents'] = user.profile.documents
    payload['first_name'] = user.first_name
    payload['last_name'] = user.last_name
    payload['account_name'] = user.profile.account_name
    payload['city'] = user.profile.city

    return payload
示例#32
0
def jwt_payload_handler(user):
    username_field = get_username_field()
    payload = {'userID': user.userID, 'username':user.username,
               'exp': datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA}

    payload[username_field] = user.userID

    if api_settings.JWT_ALLOW_REFRESH:
        payload['orig_iat'] = timegm(
            datetime.utcnow().utctimetuple()
        )

    if api_settings.JWT_AUDIENCE is not None:
        payload['aud'] = api_settings.JWT_AUDIENCE

    if api_settings.JWT_ISSUER is not None:
        payload['iss'] = api_settings.JWT_ISSUER

    return payload
示例#33
0
def jwt_payload_handler(payload):
    if isinstance(payload, dict):
        user = payload['user']
    else:
        user = payload

    username_field = get_username_field()
    username = get_username(user)

    user_type = 'unknown'
    if user.person and isinstance(user.person, Teacher):
        if user.person.is_counselor:
            user_type = 'counselor'
        else:
            user_type = 'teacher'
    elif user.person and isinstance(user.person, Student):
        user_type = 'student'

    payload = {
        'user_id': user.pk,
        'email': user.email,
        'username': username,
        'user_type': user_type,
        'exp': datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA
    }
    if isinstance(user.pk, uuid.UUID):
        payload['user_id'] = str(user.pk)

    payload[username_field] = username

    # Include original issued at time for a brand new token,
    # to allow token refresh
    if api_settings.JWT_ALLOW_REFRESH:
        payload['orig_iat'] = timegm(datetime.utcnow().utctimetuple())

    if api_settings.JWT_AUDIENCE is not None:
        payload['aud'] = api_settings.JWT_AUDIENCE

    if api_settings.JWT_ISSUER is not None:
        payload['iss'] = api_settings.JWT_ISSUER

    return payload
示例#34
0
def jwt_payload_refresh_token(user):
    """
    Créer un refresh token
    """
    username_field = get_username_field()
    username = get_username(user)

    payload = {'exp': datetime.utcnow() + api_settings.JWT_REFRESH_EXPIRATION_DELTA,
               username_field: username,
               'orig_iat': timegm(
                   datetime.utcnow().utctimetuple()
               )}

    if api_settings.JWT_AUDIENCE is not None:
        payload['aud'] = api_settings.JWT_AUDIENCE

    if api_settings.JWT_ISSUER is not None:
        payload['iss'] = api_settings.JWT_ISSUER

    return payload
 def username_field(self):
     return get_username_field()