authenticate.js 859 B

123456789101112131415161718192021222324252627282930313233343536
  1. module.exports = authenticate
  2. function authenticate (state, options) {
  3. if (!options) {
  4. state.auth = false
  5. return
  6. }
  7. switch (options.type) {
  8. case 'basic':
  9. if (!options.username || !options.password) {
  10. throw new Error('Basic authentication requires both a username and password to be set')
  11. }
  12. break
  13. case 'oauth':
  14. if (!options.token && !(options.key && options.secret)) {
  15. throw new Error('OAuth2 authentication requires a token or key & secret to be set')
  16. }
  17. break
  18. case 'token':
  19. case 'integration':
  20. case 'app':
  21. if (!options.token) {
  22. throw new Error('Token authentication requires a token to be set')
  23. }
  24. break
  25. default:
  26. throw new Error("Invalid authentication type, must be 'basic', 'integration', or 'oauth'")
  27. }
  28. state.auth = options
  29. }