A golang webfinger server implementation
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.

66 lines
2.4 KiB

  1. package webfinger
  2. import (
  3. "net/http"
  4. "github.com/captncraig/cors"
  5. )
  6. // Service is the webfinger service containing the required
  7. // HTTP handlers and defaults for webfinger implementations.
  8. type Service struct {
  9. // PreHandlers are invoked at the start of each HTTP method, used to
  10. // setup things like CORS, caching, etc. You can delete or replace
  11. // a handler by setting service.PreHandlers[name] = nil
  12. PreHandlers map[string]http.Handler
  13. // NotFoundHandler is the handler to invoke when a URL is not matched. It does NOT
  14. // handle the case of a non-existing users unless your Resolver.DummyUser returns
  15. // an error that matches Resolver.IsNotFoundError(err) == true.
  16. NotFoundHandler http.Handler
  17. // MethodNotSupportedHandler is the handler invoked when an unsupported
  18. // method is called on the webfinger HTTP service.
  19. MethodNotSupportedHandler http.Handler
  20. // MalformedRequestHandler is the handler invoked if the request routes
  21. // but is malformed in some way. The default behavior is to return 400 BadRequest,
  22. // per the webfinger specification
  23. MalformedRequestHandler http.Handler
  24. // NoTLSHandler is the handler invoked if the request is not
  25. // a TLS request. The default behavior is to redirect to the TLS
  26. // version of the URL, per the webfinger specification. Setting
  27. // this to nil will allow nonTLS connections, but that is not advised.
  28. NoTLSHandler http.Handler
  29. // ErrorHandler is the handler invoked when an error is called. The request
  30. // context contains the error in the webfinger.ErrorKey and can be fetched
  31. // via webfinger.ErrorFromContext(ctx)
  32. ErrorHandler http.Handler
  33. // Resolver is the interface for resolving user details
  34. Resolver Resolver
  35. }
  36. // Default creates a new service with the default registered handlers
  37. func Default(ur Resolver) *Service {
  38. var c = cors.Default()
  39. s := &Service{}
  40. s.Resolver = ur
  41. s.ErrorHandler = http.HandlerFunc(s.defaultErrorHandler)
  42. s.NotFoundHandler = http.HandlerFunc(s.defaultNotFoundHandler)
  43. s.MethodNotSupportedHandler = http.HandlerFunc(s.defaultMethodNotSupportedHandler)
  44. s.MalformedRequestHandler = http.HandlerFunc(s.defaultMalformedRequestHandler)
  45. s.NoTLSHandler = http.HandlerFunc(s.defaultNoTLSHandler)
  46. s.PreHandlers = make(map[string]http.Handler)
  47. s.PreHandlers[NoCacheMiddleware] = http.HandlerFunc(noCache)
  48. s.PreHandlers[CorsMiddleware] = http.HandlerFunc(c.HandleRequest)
  49. s.PreHandlers[ContentTypeMiddleware] = http.HandlerFunc(jrdSetup)
  50. return s
  51. }