account.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. const store = new Map();
  2. const logins = new Map();
  3. const nanoid = require('nanoid');
  4. class Account {
  5. constructor(id, profile) {
  6. this.accountId = id || nanoid();
  7. this.profile = profile;
  8. store.set(this.accountId, this);
  9. }
  10. /**
  11. * @param use - can either be "id_token" or "userinfo", depending on
  12. * where the specific claims are intended to be put in.
  13. * @param scope - the intended scope, while oidc-provider will mask
  14. * claims depending on the scope automatically you might want to skip
  15. * loading some claims from external resources etc. based on this detail
  16. * or not return them in id tokens but only userinfo and so on.
  17. */
  18. async claims(use, scope) { // eslint-disable-line no-unused-vars
  19. if (this.profile) {
  20. return {
  21. sub: this.accountId, // it is essential to always return a sub claim
  22. email: this.profile.email,
  23. email_verified: this.profile.email_verified,
  24. family_name: this.profile.family_name,
  25. given_name: this.profile.given_name,
  26. locale: this.profile.locale,
  27. name: this.profile.name,
  28. };
  29. }
  30. return {
  31. sub: this.accountId, // it is essential to always return a sub claim
  32. address: {
  33. country: '000',
  34. formatted: '000',
  35. locality: '000',
  36. postal_code: '000',
  37. region: '000',
  38. street_address: '000',
  39. },
  40. birthdate: '1987-10-16',
  41. email: 'johndoe@example.com',
  42. email_verified: false,
  43. family_name: 'Doe',
  44. gender: 'male',
  45. given_name: 'John',
  46. locale: 'en-US',
  47. middle_name: 'Middle',
  48. name: 'John Doe',
  49. nickname: 'Johny',
  50. phone_number: '+49 000 000000',
  51. phone_number_verified: false,
  52. picture: 'http://lorempixel.com/400/200/',
  53. preferred_username: 'johnny',
  54. profile: 'https://johnswebsite.com',
  55. updated_at: 1454704946,
  56. website: 'http://example.com',
  57. zoneinfo: 'Europe/Berlin',
  58. };
  59. }
  60. static async findByFederated(provider, claims) {
  61. const id = `${provider}.${claims.sub}`;
  62. if (!logins.get(id)) {
  63. logins.set(id, new Account(id, claims));
  64. }
  65. return logins.get(id);
  66. }
  67. static async findByLogin(login) {
  68. if (!logins.get(login)) {
  69. logins.set(login, new Account(login));
  70. }
  71. return logins.get(login);
  72. }
  73. static async findAccount(ctx, id, token) { // eslint-disable-line no-unused-vars
  74. // token is a reference to the token used for which a given account is being loaded,
  75. // it is undefined in scenarios where account claims are returned from authorization endpoint
  76. // ctx is the koa request context
  77. if (!store.get(id)) new Account(id); // eslint-disable-line no-new
  78. return store.get(id);
  79. }
  80. }
  81. module.exports = Account;