Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

model.js 2.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // from https://github.com/pedroetb/node-oauth2-server-example
  2. var config = {
  3. clients: [{
  4. clientId: "application",
  5. clientSecret: "secret"
  6. }],
  7. confidentialClients: [{
  8. clientId: "confidentialApplication",
  9. clientSecret: "topSecret"
  10. }],
  11. tokens: [],
  12. users: [{
  13. id: "123",
  14. username: "swagger",
  15. password: "password"
  16. }]
  17. }
  18. /**
  19. * Dump the memory storage content (for debug).
  20. */
  21. var dump = function () {
  22. console.log("clients", config.clients)
  23. console.log("confidentialClients", config.confidentialClients)
  24. console.log("tokens", config.tokens)
  25. console.log("users", config.users)
  26. }
  27. /*
  28. * Methods used by all grant types.
  29. */
  30. var getAccessToken = function (bearerToken, callback) {
  31. var tokens = config.tokens.filter(function (token) {
  32. return token.accessToken === bearerToken
  33. })
  34. return callback(false, tokens[0])
  35. }
  36. var getClient = function (clientId, clientSecret, callback) {
  37. var clients = config.clients.filter(function (client) {
  38. return client.clientId === clientId && client.clientSecret === clientSecret
  39. })
  40. var confidentialClients = config.confidentialClients.filter(function (client) {
  41. return client.clientId === clientId && client.clientSecret === clientSecret
  42. })
  43. callback(false, clients[0] || confidentialClients[0])
  44. }
  45. var grantTypeAllowed = function (clientId, grantType, callback) {
  46. var clientsSource,
  47. clients = []
  48. if (grantType === "password") {
  49. clientsSource = config.clients
  50. } else if (grantType === "client_credentials") {
  51. clientsSource = config.confidentialClients
  52. }
  53. if (clientsSource) {
  54. clients = clientsSource.filter(function (client) {
  55. return client.clientId === clientId
  56. })
  57. }
  58. callback(false, clients.length)
  59. }
  60. var saveAccessToken = function (accessToken, clientId, expires, user, callback) {
  61. config.tokens.push({
  62. accessToken: accessToken,
  63. expires: expires,
  64. clientId: clientId,
  65. user: user
  66. })
  67. callback(false)
  68. }
  69. /*
  70. * Method used only by password grant type.
  71. */
  72. var getUser = function (username, password, callback) {
  73. var users = config.users.filter(function (user) {
  74. return user.username === username && user.password === password
  75. })
  76. callback(false, users[0])
  77. }
  78. /*
  79. * Method used only by client_credentials grant type.
  80. */
  81. var getUserFromClient = function (clientId, clientSecret, callback) {
  82. var clients = config.confidentialClients.filter(function (client) {
  83. return client.clientId === clientId && client.clientSecret === clientSecret
  84. })
  85. var user
  86. if (clients.length) {
  87. user = {
  88. username: clientId
  89. }
  90. }
  91. callback(false, user)
  92. }
  93. /**
  94. * Export model definition object.
  95. */
  96. module.exports = {
  97. getAccessToken: getAccessToken,
  98. getClient: getClient,
  99. grantTypeAllowed: grantTypeAllowed,
  100. saveAccessToken: saveAccessToken,
  101. getUser: getUser,
  102. getUserFromClient: getUserFromClient
  103. }