Added dummy auth example

This commit is contained in:
Stephen McQuay 2018-01-21 20:18:28 -08:00
parent f61632af7f
commit bea25aa0d1
Signed by: sm
GPG Key ID: 4E4B72F479BA3CE5
4 changed files with 52 additions and 3 deletions

View File

@ -7,6 +7,9 @@ import (
"os"
"strings"
"github.com/twitchtv/twirp"
"mcquay.me/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{})
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++ {
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 {
fmt.Fprintf(os.Stderr, "hello: %#v\n", err)
os.Exit(1)

View File

@ -26,7 +26,7 @@ func main() {
hs := hwt.NewMetricsHooks(metrics.HTTPLatency)
th := pb.NewHelloWorldServer(s, hs)
sm := http.NewServeMux()
sm.Handle("/", th)
sm.HandleFunc("/", hwt.Auth(th.ServeHTTP))
sm.Handle("/metrics", promhttp.Handler())
if err := http.ListenAndServe(":8080", sm); err != nil {
log.Fatalf("listen and serve: %v", err)

7
hwt.go
View File

@ -17,8 +17,13 @@ func (s *Server) Hello(ctx context.Context, req *pb.HelloReq) (*pb.HelloResp, er
return nil, twirp.RequiredArgumentError("subject")
}
u, err := getUser(ctx)
if err != nil {
return nil, twirp.InternalErrorWith(err)
}
r := &pb.HelloResp{
Text: fmt.Sprintf("echo: %v", req.Subject),
Text: fmt.Sprintf("%s said: %v", u, req.Subject),
Hostname: s.Hostname,
}
return r, nil

32
middleware.go Normal file
View 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
}