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.

express-basic-auth.d.ts 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /// <reference types="express" />
  2. import { Request, RequestHandler } from 'express'
  3. /**
  4. * This is the middleware builder.
  5. *
  6. * Example:
  7. * const users = { alice: '1234', bob: 'correcthorsebatterystaple' }
  8. * app.use(basicAuth({ users, challenge: true }), myHandler)
  9. *
  10. * @param options The middleware's options (at least 'users' or 'authorizer' are mandatory).
  11. */
  12. declare function expressBasicAuth(options: expressBasicAuth.BasicAuthMiddlewareOptions): RequestHandler
  13. declare namespace expressBasicAuth {
  14. /**
  15. * Time safe string comparison function to protect against timing attacks.
  16. *
  17. * It is important to provide the arguments in the correct order, as the runtime
  18. * depends only on the `userInput` argument. Switching the order would expose the `secret`
  19. * to timing attacks.
  20. *
  21. * @param userInput The user input to be compared
  22. * @param secret The secret value the user input should be compared with
  23. *
  24. * @returns true if `userInput` matches `secret`, false if not
  25. */
  26. export function safeCompare(userInput: string, secret: string): boolean
  27. /**
  28. * The configuration you pass to the middleware can take three forms, either:
  29. * - A map of static users ({ bob: 'pa$$w0rd', ... }) ;
  30. * - An authorizer function
  31. * - An asynchronous authorizer function
  32. */
  33. export type BasicAuthMiddlewareOptions = IUsersOptions | (IAuthorizerOptions | IAsyncAuthorizerOptions)
  34. /**
  35. * express-basic-auth patches the request object to set an `auth` property that lets you retrieve the authed user.
  36. *
  37. * Example (TypeScript):
  38. * app.use(basicAuth({ ... }), (req: basicAuth.IBasicAuthedRequest, res, next) => {
  39. * res.end(`Welcome ${req.auth.user} (your password is ${req.auth.password})`)
  40. * next()
  41. * })
  42. */
  43. export interface IBasicAuthedRequest extends Request {
  44. auth: { user: string, password: string }
  45. }
  46. type Authorizer = (username: string, password: string) => boolean
  47. type AsyncAuthorizerCallback = (err: any, authed?: boolean) => void
  48. type AsyncAuthorizer = (username: string, password: string, callback: AsyncAuthorizerCallback) => void
  49. type ValueOrFunction<T> = T | ((req: IBasicAuthedRequest) => T)
  50. interface IBaseOptions {
  51. /**
  52. * Per default the middleware will not add a WWW-Authenticate challenge header to responses of unauthorized requests.
  53. * You can enable that by setting this to true, causing most browsers to show a popup to enter credentials
  54. * on unauthorized responses.
  55. *
  56. * @default false
  57. */
  58. challenge?: boolean
  59. /**
  60. * You can set the realm (the realm identifies the system to authenticate against and can be used by clients to
  61. * save credentials) of the challenge by passing a string or a function that gets passed the request and is
  62. * expected to return the realm.
  63. *
  64. * @default undefined
  65. */
  66. realm?: ValueOrFunction<string>
  67. /**
  68. * Per default, the response body for unauthorized responses will be empty.
  69. * It can be configured using the unauthorizedResponse option. You can either pass a static response or a
  70. * function that gets passed the express request object and is expected to return the response body.
  71. * If the response body is a string, it will be used as-is, otherwise it will be sent as JSON.
  72. *
  73. * @default ''
  74. */
  75. unauthorizedResponse?: ValueOrFunction<any>
  76. }
  77. interface IUsersOptions extends IBaseOptions {
  78. /**
  79. * If you simply want to check basic auth against one or multiple static credentials, you can pass those
  80. * credentials in the users option.
  81. *
  82. * Example:
  83. * const users = { alice: '1234', bob: 'correcthorsebatterystaple' }
  84. * app.use(basicAuth({ users, challenge: true }), myHandler)
  85. */
  86. users: { [username: string]: string }
  87. }
  88. interface IAuthorizerOptions extends IBaseOptions {
  89. /**
  90. * Set to true if your authorizer is asynchronous.
  91. */
  92. authorizeAsync?: false
  93. /**
  94. * You can pass your own authorizer function, to check the credentials however you want.
  95. * It will be called with a username and password and is expected to return true or false to indicate that the
  96. * credentials were approved or not:
  97. *
  98. * Example:
  99. * app.use(basicAuth({ authorizer }))
  100. *
  101. * function myAuthorizer(username: string, password: string) {
  102. * return username.startsWith('A') && password.startsWith('secret');
  103. * }
  104. *
  105. * This will authorize all requests with credentials where the username begins with 'A' and the password begins
  106. * with 'secret'. In an actual application you would likely look up some data instead ;-)
  107. */
  108. authorizer: Authorizer
  109. }
  110. interface IAsyncAuthorizerOptions extends IBaseOptions {
  111. /**
  112. * Set it to true to use a asynchronous authorizer.
  113. */
  114. authorizeAsync: true
  115. /**
  116. * You can pass an asynchronous authorizer. It will be passed a callback as the third parameter, which is
  117. * expected to be called by standard node convention with an error and a boolean to indicate if the credentials
  118. * have been approved or not.
  119. *
  120. * Example:
  121. * app.use(basicAuth({ authorizer, authorizeAsync: true }));
  122. *
  123. * function authorizer(username, password, authorize) {
  124. * if(username.startsWith('A') && password.startsWith('secret'))
  125. * return authorize(null, true)
  126. *
  127. * return authorize(null, false)
  128. * }
  129. */
  130. authorizer: AsyncAuthorizer
  131. }
  132. }
  133. export = expressBasicAuth