153 lines
3.1 KiB
Go
153 lines
3.1 KiB
Go
package auth
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/go-gost/core/auth"
|
|
"github.com/go-gost/core/logger"
|
|
"github.com/go-gost/plugin/auth/proto"
|
|
"github.com/go-gost/x/internal/plugin"
|
|
auth_util "github.com/go-gost/x/internal/util/auth"
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
type grpcPlugin struct {
|
|
conn grpc.ClientConnInterface
|
|
client proto.AuthenticatorClient
|
|
log logger.Logger
|
|
}
|
|
|
|
// NewGRPCPlugin creates an Authenticator plugin based on gRPC.
|
|
func NewGRPCPlugin(name string, addr string, opts ...plugin.Option) auth.Authenticator {
|
|
var options plugin.Options
|
|
for _, opt := range opts {
|
|
opt(&options)
|
|
}
|
|
|
|
log := logger.Default().WithFields(map[string]any{
|
|
"kind": "auther",
|
|
"auther": name,
|
|
})
|
|
conn, err := plugin.NewGRPCConn(addr, &options)
|
|
if err != nil {
|
|
log.Error(err)
|
|
}
|
|
|
|
p := &grpcPlugin{
|
|
conn: conn,
|
|
log: log,
|
|
}
|
|
|
|
if conn != nil {
|
|
p.client = proto.NewAuthenticatorClient(conn)
|
|
}
|
|
return p
|
|
}
|
|
|
|
// Authenticate checks the validity of the provided user-password pair.
|
|
func (p *grpcPlugin) Authenticate(ctx context.Context, user, password string, opts ...auth.Option) (string, bool) {
|
|
if p.client == nil {
|
|
return "", false
|
|
}
|
|
|
|
r, err := p.client.Authenticate(ctx,
|
|
&proto.AuthenticateRequest{
|
|
Username: user,
|
|
Password: password,
|
|
Client: string(auth_util.ClientAddrFromContext(ctx)),
|
|
})
|
|
if err != nil {
|
|
p.log.Error(err)
|
|
return "", false
|
|
}
|
|
return r.Id, r.Ok
|
|
}
|
|
|
|
func (p *grpcPlugin) Close() error {
|
|
if closer, ok := p.conn.(io.Closer); ok {
|
|
return closer.Close()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type httpPluginRequest struct {
|
|
Username string `json:"username"`
|
|
Password string `json:"password"`
|
|
Client string `json:"client"`
|
|
}
|
|
|
|
type httpPluginResponse struct {
|
|
OK bool `json:"ok"`
|
|
ID string `json:"id"`
|
|
}
|
|
|
|
type httpPlugin struct {
|
|
url string
|
|
client *http.Client
|
|
header http.Header
|
|
log logger.Logger
|
|
}
|
|
|
|
// NewHTTPPlugin creates an Authenticator plugin based on HTTP.
|
|
func NewHTTPPlugin(name string, url string, opts ...plugin.Option) auth.Authenticator {
|
|
var options plugin.Options
|
|
for _, opt := range opts {
|
|
opt(&options)
|
|
}
|
|
|
|
return &httpPlugin{
|
|
url: url,
|
|
client: plugin.NewHTTPClient(&options),
|
|
header: options.Header,
|
|
log: logger.Default().WithFields(map[string]any{
|
|
"kind": "auther",
|
|
"auther": name,
|
|
}),
|
|
}
|
|
}
|
|
|
|
func (p *httpPlugin) Authenticate(ctx context.Context, user, password string, opts ...auth.Option) (id string, ok bool) {
|
|
if p.client == nil {
|
|
return
|
|
}
|
|
|
|
rb := httpPluginRequest{
|
|
Username: user,
|
|
Password: password,
|
|
Client: string(auth_util.ClientAddrFromContext(ctx)),
|
|
}
|
|
v, err := json.Marshal(&rb)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, p.url, bytes.NewReader(v))
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
if p.header != nil {
|
|
req.Header = p.header.Clone()
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
resp, err := p.client.Do(req)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return
|
|
}
|
|
|
|
res := httpPluginResponse{}
|
|
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
|
|
return
|
|
}
|
|
return res.ID, res.OK
|
|
}
|