auth.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Package auth defines the implementation for authentication plugins.
  2. // Authentication plugins allow to authenticate external users
  3. package auth
  4. import (
  5. "context"
  6. "github.com/hashicorp/go-plugin"
  7. "google.golang.org/grpc"
  8. "github.com/drakkan/sftpgo/v2/sdk/plugin/auth/proto"
  9. )
  10. const (
  11. // PluginName defines the name for a notifier plugin
  12. PluginName = "auth"
  13. )
  14. // Handshake is a common handshake that is shared by plugin and host.
  15. var Handshake = plugin.HandshakeConfig{
  16. ProtocolVersion: 1,
  17. MagicCookieKey: "SFTPGO_PLUGIN_AUTH",
  18. MagicCookieValue: "d1ed507d-d2be-4a38-a460-6fe0b2cc7efc",
  19. }
  20. // PluginMap is the map of plugins we can dispense.
  21. var PluginMap = map[string]plugin.Plugin{
  22. PluginName: &Plugin{},
  23. }
  24. // Authenticator defines the interface for authentication plugins
  25. type Authenticator interface {
  26. CheckUserAndPass(username, password, ip, protocol string, userAsJSON []byte) ([]byte, error)
  27. CheckUserAndTLSCert(username, tlsCert, ip, protocol string, userAsJSON []byte) ([]byte, error)
  28. CheckUserAndPublicKey(username, pubKey, ip, protocol string, userAsJSON []byte) ([]byte, error)
  29. CheckUserAndKeyboardInteractive(username, ip, protocol string, userAsJSON []byte) ([]byte, error)
  30. SendKeyboardAuthRequest(requestID, username, password, ip string, answers, questions []string, step int32) (string, []string, []bool, int, int, error)
  31. }
  32. // Plugin defines the implementation to serve/connect to an authe plugin
  33. type Plugin struct {
  34. plugin.Plugin
  35. Impl Authenticator
  36. }
  37. // GRPCServer defines the GRPC server implementation for this plugin
  38. func (p *Plugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
  39. proto.RegisterAuthServer(s, &GRPCServer{
  40. Impl: p.Impl,
  41. })
  42. return nil
  43. }
  44. // GRPCClient defines the GRPC client implementation for this plugin
  45. func (p *Plugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
  46. return &GRPCClient{
  47. client: proto.NewAuthClient(c),
  48. }, nil
  49. }