open-vault/vendor/layeh.com/radius/server.go

86 lines
2.3 KiB
Go
Raw Normal View History

2017-09-05 22:06:47 +00:00
package radius
2017-02-07 21:04:27 +00:00
import (
2017-09-05 22:06:47 +00:00
"context"
2017-02-07 21:04:27 +00:00
"errors"
"net"
)
2017-09-05 22:06:47 +00:00
// ErrServerShutdown is returned from server Serve methods when Shutdown
// has been called and handlers are still completing.
var ErrServerShutdown = errors.New("radius: server is shutting down")
// Handler provides a handler to RADIUS server requests. When a RADIUS request
// is received, ServeRADIUS is called.
2017-02-07 21:04:27 +00:00
type Handler interface {
2017-09-05 22:06:47 +00:00
ServeRADIUS(w ResponseWriter, r *Request)
2017-02-07 21:04:27 +00:00
}
2017-09-05 22:06:47 +00:00
// HandlerFunc allows a function to implement Handler.
type HandlerFunc func(w ResponseWriter, r *Request)
2017-02-07 21:04:27 +00:00
// ServeRADIUS calls h(w, p).
2017-09-05 22:06:47 +00:00
func (h HandlerFunc) ServeRADIUS(w ResponseWriter, r *Request) {
h(w, r)
2017-02-07 21:04:27 +00:00
}
2017-09-05 22:06:47 +00:00
// Request is an incoming RADIUS request that is being handled by the server.
type Request struct {
2018-02-11 00:29:52 +00:00
// LocalAddr is the local address on which the incoming RADIUS request
// was received.
LocalAddr net.Addr
// RemoteAddr is the address from which the incoming RADIUS request
// was sent.
2017-09-05 22:06:47 +00:00
RemoteAddr net.Addr
2017-02-07 21:04:27 +00:00
2018-02-11 00:29:52 +00:00
// Packet is the RADIUS packet sent in the request.
2017-09-05 22:06:47 +00:00
*Packet
2017-02-07 21:04:27 +00:00
2017-09-05 22:06:47 +00:00
ctx context.Context
2017-02-07 21:04:27 +00:00
}
2017-09-05 22:06:47 +00:00
// Context returns the context of the request. If a context has not been set
// using WithContext, the Background context is returned.
func (r *Request) Context() context.Context {
if r.ctx != nil {
return r.ctx
2017-02-07 21:04:27 +00:00
}
2017-09-05 22:06:47 +00:00
return context.Background()
2017-02-07 21:04:27 +00:00
}
2017-09-05 22:06:47 +00:00
// WithContext returns a shallow copy of the request with the new request's
// context set to the given context.
func (r *Request) WithContext(ctx context.Context) *Request {
if ctx == nil {
panic("nil ctx")
2017-02-07 21:04:27 +00:00
}
2017-09-05 22:06:47 +00:00
req := new(Request)
*req = *r
req.ctx = ctx
return req
2017-02-07 21:04:27 +00:00
}
2017-09-05 22:06:47 +00:00
// ResponseWriter is used by RADIUS servers when replying to a RADIUS request.
type ResponseWriter interface {
Write(packet *Packet) error
2017-02-07 21:04:27 +00:00
}
2017-09-05 22:06:47 +00:00
// SecretSource supplies RADIUS servers with the secret that should be used for
// authorizing and decrypting packets.
//
// ctx is canceled if the server's Shutdown method is called.
type SecretSource interface {
RADIUSSecret(ctx context.Context, remoteAddr net.Addr) ([]byte, error)
2017-02-07 21:04:27 +00:00
}
2017-09-05 22:06:47 +00:00
// StaticSecretSource returns a SecretSource that uses secret for all requests.
func StaticSecretSource(secret []byte) SecretSource {
return staticSecretSource(secret)
}
2017-02-07 21:04:27 +00:00
2017-09-05 22:06:47 +00:00
type staticSecretSource []byte
2017-02-07 21:04:27 +00:00
2017-09-05 22:06:47 +00:00
func (secret staticSecretSource) RADIUSSecret(ctx context.Context, remoteAddr net.Addr) ([]byte, error) {
return []byte(secret), nil
2017-02-07 21:04:27 +00:00
}