Django rest framework - generate TOTP and serialize it?

I am not sure how to do this:

I want create an end point where an authenticated user can click to get a TOTP.

The function which I have used as a separate file:

# gen_totp.py

import hmac, base64, struct, hashlib, time

def get_hotp_token(secret, intervals_no):
    key = base64.b32decode(secret, True)
    msg = struct.pack(">Q", intervals_no)
    h = hmac.new(key, msg, hashlib.sha1).digest()
    o = h[19] & 15
    h = (struct.unpack(">I", h[o:o+4])[0] & 0x7fffffff) % 1000000
    return h

def get_totp_token(secret):
    return get_hotp_token(secret, intervals_no=int(time.time())//30)

Then to serialize:

from re import U
from rest_framework import serializers


# totp serializer
class totp_serializer(serializers.Serializer):
    totp = serializers.IntegerField(get_totp_token())

I am stumped on how to provide secret to get_totp_token() in the totp_serializer.

Back to Top