Added dummy auth example
This commit is contained in:
parent
f61632af7f
commit
bea25aa0d1
@ -7,6 +7,9 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/twitchtv/twirp"
|
||||||
|
|
||||||
|
"mcquay.me/hwt"
|
||||||
pb "mcquay.me/hwt/rpc/hwt"
|
pb "mcquay.me/hwt/rpc/hwt"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -20,8 +23,17 @@ func main() {
|
|||||||
|
|
||||||
c := pb.NewHelloWorldProtobufClient(fmt.Sprintf("http://%s", os.Args[1]), &http.Client{})
|
c := pb.NewHelloWorldProtobufClient(fmt.Sprintf("http://%s", os.Args[1]), &http.Client{})
|
||||||
|
|
||||||
|
h := http.Header{}
|
||||||
|
h.Set("sm-auth", hwt.PSK)
|
||||||
|
ctx := context.Background()
|
||||||
|
ctx, err := twirp.WithHTTPRequestHeaders(ctx, h)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "setting twirp headers: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
for i := 0; ; i++ {
|
for i := 0; ; i++ {
|
||||||
resp, err := c.Hello(context.Background(), &pb.HelloReq{Subject: strings.Join(os.Args[2:], " ")})
|
resp, err := c.Hello(ctx, &pb.HelloReq{Subject: strings.Join(os.Args[2:], " ")})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "hello: %#v\n", err)
|
fmt.Fprintf(os.Stderr, "hello: %#v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
@ -26,7 +26,7 @@ func main() {
|
|||||||
hs := hwt.NewMetricsHooks(metrics.HTTPLatency)
|
hs := hwt.NewMetricsHooks(metrics.HTTPLatency)
|
||||||
th := pb.NewHelloWorldServer(s, hs)
|
th := pb.NewHelloWorldServer(s, hs)
|
||||||
sm := http.NewServeMux()
|
sm := http.NewServeMux()
|
||||||
sm.Handle("/", th)
|
sm.HandleFunc("/", hwt.Auth(th.ServeHTTP))
|
||||||
sm.Handle("/metrics", promhttp.Handler())
|
sm.Handle("/metrics", promhttp.Handler())
|
||||||
if err := http.ListenAndServe(":8080", sm); err != nil {
|
if err := http.ListenAndServe(":8080", sm); err != nil {
|
||||||
log.Fatalf("listen and serve: %v", err)
|
log.Fatalf("listen and serve: %v", err)
|
||||||
|
7
hwt.go
7
hwt.go
@ -17,8 +17,13 @@ func (s *Server) Hello(ctx context.Context, req *pb.HelloReq) (*pb.HelloResp, er
|
|||||||
return nil, twirp.RequiredArgumentError("subject")
|
return nil, twirp.RequiredArgumentError("subject")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
u, err := getUser(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, twirp.InternalErrorWith(err)
|
||||||
|
}
|
||||||
|
|
||||||
r := &pb.HelloResp{
|
r := &pb.HelloResp{
|
||||||
Text: fmt.Sprintf("echo: %v", req.Subject),
|
Text: fmt.Sprintf("%s said: %v", u, req.Subject),
|
||||||
Hostname: s.Hostname,
|
Hostname: s.Hostname,
|
||||||
}
|
}
|
||||||
return r, nil
|
return r, nil
|
||||||
|
32
middleware.go
Normal file
32
middleware.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package hwt
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
const PSK = "some key"
|
||||||
|
|
||||||
|
var reqUserKey = new(int)
|
||||||
|
|
||||||
|
func Auth(h http.HandlerFunc) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, req *http.Request) {
|
||||||
|
k := req.Header.Get("sm-auth")
|
||||||
|
if k == "" {
|
||||||
|
w.Header().Set("www-authenticate", "sm-auth")
|
||||||
|
http.Error(w, "missing/invalid key", http.StatusUnauthorized)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ctx := context.WithValue(req.Context(), reqUserKey, "valid user")
|
||||||
|
h(w, req.WithContext(ctx))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getUser(ctx context.Context) (string, error) {
|
||||||
|
u, ok := ctx.Value(reqUserKey).(string)
|
||||||
|
if !ok {
|
||||||
|
return "", errors.New("user key not found in context")
|
||||||
|
}
|
||||||
|
return u, nil
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user