Búsqueda de sitios web

javascript - Transformar Tick a Fecha en JS


Estoy tratando de verificar la fecha de vencimiento de un token JWT y todo lo que he intentado no me da la fecha correcta.

"exp": 1522210228 => real answer => Wednesday, March 28, 2018 12:10:28 AM

He probado esas bibliotecas y no logré que funcionaran...

  1. https://github.com/auth0/angular2-jwt/blob/master/src/jwthelper.service.ts
  2. https://github.com/auth0/jwt-decode

1

const helper = new JwtHelperService();
const decodedToken = helper.decodeToken(this.authentificationInfos.token);
const expirationDate = helper.getTokenExpirationDate(this.authentificationInfos.token);

console.log(expirationDate); => null?

2

import * as decode from 'jwt-decode';

const token = decode<{ data: { exp: number, iat: number, iss: string, nbf: number, username: string } }>(this.authentificationInfos.token);
const date = new Date(token.data.exp);
console.log(date); => Sun Jan 18 1970 09:50:10 GMT-0500 (Eastern Standard Time)

const d = new Date(0);
d.setUTCMilliseconds(token.data.exp);
console.log(d); => Sun Jan 18 1970 09:50:10 GMT-0500 (Eastern Standard Time)

Aquí está el token completo:

eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7InVzZXJuYW1lIjoiYmlsb2RlYXV2aW5jZW50QG91dGxvb2suY29tIiwiaWF0IjoxNTIyMjA2NjI4LCJpc3MiOiJtbnAuY29tIiwibmJmIjoxNTIyMjA2NjI4LCJleHAiOjE1MjIyMTAyMjh9fQ.1WRlQatauXw2HEWj9B9VL6fIVR-4nAoKuWvkS4_m86k

https://jwt.io/ está decodificando el token y el exp mostrado es correcto.

¿Cómo puedo obtener la fecha real de token.exp?

Respuesta #1

Las marcas de tiempo en JWT son marcas de tiempo UNIX contadas desde el 01.01.1970 00:00 UTC: https://www.rfc-editor.org/rfc/rfc7519#section-4.1.4 explica que se utiliza una fecha numérica para la reclamación exp (y también para las reclamaciones nbf (no antes de) e iat (emitido en))

https://www.rfc-editor.org/rfc/rfc7519#section-2 define la fecha numérica:

Un valor numérico JSON que representa el número de segundos desde 1970-01-01T00:00:00Z UTC hasta la fecha/hora UTC especificada, ignorando los segundos intercalares.

var jwtDecode = require('jwt-decode');
var jwt = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkYXRhIjp7InVzZXJuYW1lIjoiYmlsb2RlYXV2aW5jZW50QG91dGxvb2suY29tIiwiaWF0IjoxNTIyMjA2NjI4LCJpc3MiOiJtbnAuY29tIiwibmJmIjoxNTIyMjA2NjI4LCJleHAiOjE1MjIyMTAyMjh9fQ.1WRlQatauXw2HEWj9B9VL6fIVR-4nAoKuWvkS4_m86k";

const token = jwtDecode(jwt);
const d = new Date(0);
d.setUTCSeconds(token.data.exp);
console.log(d);

salida :

2018-03-28T04:10:28.000Z

Usa d.getHours(), d.getMinutes(), etc. para obtener tu hora local.

Respuesta #2

Dado que es un valor en segundos desde la época, todo lo que tienes que hacer es multiplicar exp por 1000.

Vea la demostración a continuación.

const jwt = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IllvIiwiZXhwIjoxNTE2MjM5MDIyfQ.9xA3RWBjEdQmUFlf5E7fLrR8Xi36ogcjGrOdkL6DA3Y";

const decodedJwt = jwt_decode(jwt);

console.log( new Date(decodedJwt.exp * 1000) );

// output: "2018-01-18T01:30:22.000Z"
<script src="https://unpkg.com/[email /build/jwt-decode.js"></script>

Artículos relacionados