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
Beispiel #2
0
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
Beispiel #3
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
    is_user_and_device = user is not None and device is not None
    is_users_device = is_user_and_device and device.user_id == user.id
    is_device_confirmed = is_users_device and device.confirmed is True
    if is_device_confirmed:
        payload['otp_device_id'] = device.persistent_id
    else:
        payload['otp_device_id'] = None

    return payload
Beispiel #4
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
Beispiel #5
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
Beispiel #6
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)
Beispiel #7
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
Beispiel #8
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)
Beispiel #9
0
def jwt_otp_payload_handler(user, device=None):
    """
    Override default payload handler to optionally include OTP device
    """
    username = compat.get_username(user)

    payload = {
        'user_id': user.pk,
        'username': username,
        'exp': datetime.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'] = calendar.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

    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
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
def jwt_payload_handler(user):
    """
    Custom payload handler for JWT tokens with PhoneUser model
    """
    from rest_framework_jwt.compat import get_username
    from rest_framework_jwt.settings import api_settings
    from calendar import timegm
    from datetime import datetime

    username = get_username(user)

    payload = {
        'user_id': user.pk,
        'username': str(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
Beispiel #12
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
Beispiel #13
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()
        )

    return payload
Beispiel #14
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
Beispiel #15
0
def jwt_payload_handler(user):
    username = get_username(user)

    payload = {
        'username': username,
        'email': user.email,
        'first_name': user.first_name,
        'last_name': user.last_name,
        'gender': user.gender,
        'birthdate': user.birthdate.strftime('%d/%m/%Y'),
        '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
Beispiel #16
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
Beispiel #17
0
def jwt_payload_handler(user, exp=datetime.utcnow() + timedelta(days=60)):
    username_field = "user_name"
    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,
        'user_name': username,
        'exp': exp
    }
    # 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
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
Beispiel #19
0
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
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
Beispiel #21
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
Beispiel #22
0
def jwt_payload_handler(user):
    payload = {
        'user_id': user.pk,
        'jti': uuid.uuid4().hex,
        'exp': datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA,
        'token_type': api_settings.JWT_TOKEN_KEYWORD,
        'username': get_username(user)
    }
    if isinstance(user.pk, uuid.UUID):
        payload['user_id'] = str(user.pk)

    # 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
Beispiel #23
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
Beispiel #24
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
Beispiel #25
0
def default_idtoken_processing_hook(id_token, user, **kwargs):
    username = get_username(user)
    id_token['username'] = username
    id_token['email'] = user.email
    id_token['first_name'] = user.first_name
    id_token['last_name'] = user.last_name
    id_token['gender'] = user.gender
    id_token['birthdate'] = user.birthdate.strftime('%d/%m/%Y')
    id_token['dni_type'] = user.dni_type
    id_token['dni_number'] = user.dni_number

    return id_token
Beispiel #26
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
Beispiel #27
0
def skb_jwt_payload_handler(user):
    username = get_username(user)

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

    return payload
Beispiel #28
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
Beispiel #29
0
def jwt_payload_handler(user):
    username = get_username(user)
    profile = UserProfile.active.filter(user=user).first()
    if profile:
        user_id = str(profile.id)
    else:
        user_id = None
    # 'user_id': user.pk,  # comment. may be use uuid?
    payload = {
        'username': username,
        'user_id': user_id,
        'exp': datetime.utcnow() + api_settings.JWT_EXPIRATION_DELTA
    }

    return payload
Beispiel #30
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
Beispiel #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,
        '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
Beispiel #32
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
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
Beispiel #34
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_response_payload_handler(token, user=None, request=None):
    """
    Returns the response data for both the login and refresh views.
    Override to return a custom response such as including the
    serialized representation of the User.

    Example:

    def jwt_response_payload_handler(token, user=None, request=None):
        return {
            'token': token,
            'user': UserSerializer(user).data
        }

    """
    return {
        'user': get_username(user),
        'token': token
    }