produce.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import epoch from '../lib/epoch.js';
  2. import isObject from '../lib/is_object.js';
  3. import secs from '../lib/secs.js';
  4. export class ProduceJWT {
  5. constructor(payload) {
  6. if (!isObject(payload)) {
  7. throw new TypeError('JWT Claims Set MUST be an object');
  8. }
  9. this._payload = payload;
  10. }
  11. setIssuer(issuer) {
  12. this._payload = { ...this._payload, iss: issuer };
  13. return this;
  14. }
  15. setSubject(subject) {
  16. this._payload = { ...this._payload, sub: subject };
  17. return this;
  18. }
  19. setAudience(audience) {
  20. this._payload = { ...this._payload, aud: audience };
  21. return this;
  22. }
  23. setJti(jwtId) {
  24. this._payload = { ...this._payload, jti: jwtId };
  25. return this;
  26. }
  27. setNotBefore(input) {
  28. if (typeof input === 'number') {
  29. this._payload = { ...this._payload, nbf: input };
  30. }
  31. else {
  32. this._payload = { ...this._payload, nbf: epoch(new Date()) + secs(input) };
  33. }
  34. return this;
  35. }
  36. setExpirationTime(input) {
  37. if (typeof input === 'number') {
  38. this._payload = { ...this._payload, exp: input };
  39. }
  40. else {
  41. this._payload = { ...this._payload, exp: epoch(new Date()) + secs(input) };
  42. }
  43. return this;
  44. }
  45. setIssuedAt(input) {
  46. if (typeof input === 'undefined') {
  47. this._payload = { ...this._payload, iat: epoch(new Date()) };
  48. }
  49. else {
  50. this._payload = { ...this._payload, iat: input };
  51. }
  52. return this;
  53. }
  54. }