Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

example.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. const express = require('express')
  2. var app = express()
  3. const basicAuth = require('./index.js')
  4. /**
  5. * express-basic-auth
  6. *
  7. * Example server. Just run in the same folder:
  8. *
  9. * npm install express express-basic-auth
  10. *
  11. * and then run this file with node ('node example.js')
  12. *
  13. * You can send GET requests to localhost:8080/async , /custom, /challenge or /static
  14. * and see how it refuses or accepts your request matching the basic auth settings.
  15. */
  16. //TODO: Implement some form of automatic testing against the example server
  17. //Requires basic auth with username 'Admin' and password 'secret1234'
  18. var staticUserAuth = basicAuth({
  19. users: {
  20. 'Admin': 'secret1234'
  21. },
  22. challenge: false
  23. })
  24. //Uses a custom (synchronous) authorizer function
  25. var customAuthorizerAuth = basicAuth({
  26. authorizer: myAuthorizer
  27. })
  28. //Same, but sends a basic auth challenge header when authorization fails
  29. var challengeAuth = basicAuth({
  30. authorizer: myAuthorizer,
  31. challenge: true
  32. })
  33. //Uses a custom asynchronous authorizer function
  34. var asyncAuth = basicAuth({
  35. authorizer: myAsyncAuthorizer,
  36. authorizeAsync: true
  37. })
  38. //Uses a custom response body function
  39. var customBodyAuth = basicAuth({
  40. users: { 'Foo': 'bar' },
  41. unauthorizedResponse: getUnauthorizedResponse
  42. })
  43. //Uses a static response body
  44. var staticBodyAuth = basicAuth({
  45. unauthorizedResponse: 'Haaaaaha'
  46. })
  47. //Uses a JSON response body
  48. var jsonBodyAuth = basicAuth({
  49. unauthorizedResponse: { foo: 'bar' }
  50. })
  51. //Uses a custom realm
  52. var realmAuth = basicAuth({
  53. challenge: true,
  54. realm: 'test'
  55. })
  56. //Uses a custom realm function
  57. var realmFunctionAuth = basicAuth({
  58. challenge: true,
  59. realm: function (req) {
  60. return 'bla'
  61. }
  62. })
  63. app.get('/static', staticUserAuth, function(req, res) {
  64. res.status(200).send('You passed')
  65. })
  66. app.get('/custom', customAuthorizerAuth, function(req, res) {
  67. res.status(200).send('You passed')
  68. })
  69. app.get('/challenge', challengeAuth, function(req, res) {
  70. res.status(200).send('You passed')
  71. })
  72. app.get('/async', asyncAuth, function(req, res) {
  73. res.status(200).send('You passed')
  74. })
  75. app.get('/custombody', customBodyAuth, function(req, res) {
  76. res.status(200).send('You passed')
  77. })
  78. app.get('/staticbody', staticBodyAuth, function(req, res) {
  79. res.status(200).send('You passed')
  80. })
  81. app.get('/jsonbody', jsonBodyAuth, function(req, res) {
  82. res.status(200).send('You passed')
  83. })
  84. app.get('/realm', realmAuth, function(req, res) {
  85. res.status(200).send('You passed')
  86. })
  87. app.get('/realmfunction', realmFunctionAuth, function(req, res) {
  88. res.status(200).send('You passed')
  89. })
  90. app.listen(8080, function() {
  91. console.log("Listening!")
  92. })
  93. //Custom authorizer checking if the username starts with 'A' and the password with 'secret'
  94. function myAuthorizer(username, password) {
  95. return username.startsWith('A') && password.startsWith('secret')
  96. }
  97. //Same but asynchronous
  98. function myAsyncAuthorizer(username, password, cb) {
  99. if(username.startsWith('A') && password.startsWith('secret'))
  100. return cb(null, true)
  101. else
  102. return cb(null, false)
  103. }
  104. function getUnauthorizedResponse(req) {
  105. return req.auth ? ('Credentials ' + req.auth.user + ':' + req.auth.password + ' rejected') : 'No credentials provided'
  106. }