Skip to content
This repository has been archived by the owner on Nov 4, 2020. It is now read-only.

Latest commit

 

History

History
72 lines (55 loc) · 1.65 KB

File metadata and controls

72 lines (55 loc) · 1.65 KB
description
Decodes a Decentralized ID Token from a Base64 string into a tuple of its individual components.

decode

Arguments

decode(didToken)

Returns

[string, Claim]: containing the [proof, claim] that composes the DID Token. The claim, while given as a JSON string, is automatically parsed to the following interface:

interface Claim {
  iat: number; // Issued At Timestamp
  ext: number; // Expiration Timestamp
  iss: string; // Issuer of DID Token
  sub: string; // Subject
  aud: string; // Audience
  nbf: number; // Not Before Timestamp
  tid: string; // DID Token ID
  add: string; // (optional) Misc additional signed data
}

As a convenience, the above interface is available to your code in TypeScript:

import { Claim } from '@magic-sdk/admin';

Example

const { Magic } = require('@magic-sdk/admin');
const mAdmin = new Magic('SECRET_API_KEY');

...

/* Gets the DID token from a login request */
app.get('/login', async (req: any, res: any) => {
  /*
    Assumes DIDToken was passed in the Authorization header
    in the standard `Bearer {token}` format.
   */
  const DIDToken = req.headers.authorization.substring(7);
  const [proof, claim] =  mAdmin.token.decode(DIDToken);
  
  console.log(proof); // => string
  
  console.log(claim);
  /*
    => {
      iat: number,
      ext: number,
      iss: string,
      sub: string,
      aud: string,
      nbf: number,
      tid: string
    }
   */
});