-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathJWT-Datetime.py
More file actions
27 lines (21 loc) 路 880 Bytes
/
Copy pathJWT-Datetime.py
File metadata and controls
27 lines (21 loc) 路 880 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import jwt
from datetime import datetime, timedelta
# Definir la clave secreta para firmar el token
SECRET_KEY = 'tu_clave_secreta'
# Funci贸n para generar un token JWT con una duraci贸n de 10 minutos
def generar_token():
# Definir la fecha y hora de emisi贸n del token
ahora = datetime.utcnow()
# Definir la fecha y hora de expiraci贸n del token (10 minutos despu茅s)
expiracion = ahora + timedelta(minutes=10)
# Crear los datos a incluir en el token (puedes agregar m谩s datos si lo necesitas)
datos_token = {
'usuario_id': 1234567890,
'exp': expiracion
}
# Generar el token JWT usando PyJWT
token = jwt.encode(datos_token, SECRET_KEY, algorithm='HS256')
return token
# Ejemplo de c贸mo usar la funci贸n para generar un token
token_generado = generar_token()
print("Token JWT generado:", token_generado)