Construct a token.

Based on a JSON Web Token string, construct a token object. Optionally
if a `clientId` is provided, the token may be tested for roles with
`hasRole()`.

@constructor

@param {String} token The JSON Web Token formatted token string.
@param {String} clientId Optional clientId if this is an `access_token`.
function Token (token, clientId) {
  this.token = token;
  this.clientId = clientId;

  if (token) {
    try {
      const parts = token.split('.');
      this.header = JSON.parse(Buffer.from(parts[0], 'base64').toString());
      this.content = JSON.parse(Buffer.from(parts[1], 'base64').toString());
      this.signature = Buffer.from(parts[2], 'base64');
      this.signed = parts[0] + '.' + parts[1];
    } catch (err) {
      this.content = {
        exp: 0
      };
    }
  }
}