Skip to content
This repository has been archived by the owner on Jan 12, 2024. It is now read-only.

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
akshayjshah committed May 9, 2023
1 parent 4da9eb0 commit 2c209c4
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,47 @@ go get go.akshayshah.org/connectauth
package main

import (
"context"
"fmt"
"net/http"
"context"
"fmt"
"net/http"

"github.com/bufbuild/connect-go"
"go.akshayshah.org/connectauth"
"github.com/bufbuild/connect-go"
"go.akshayshah.org/connectauth"
)

// Our authentication logic is just a function.
func authenticate(ctx context.Context, req *connectauth.Request) (any, error) {
const passphrase = "open-sesame"
if req.Header.Get("Authorization") != "Bearer "+passphrase {
// If authentication fails, we return an error. connectauth.Errorf is a
// convenient shortcut to produce an error coded with
// connect.CodeUnauthenticated.
return nil, connectauth.Errorf("try %q as a bearer token instead", passphrase)
}
// Once we've authenticated the request, we can return the authenticated
// identity. The identity gets attached to the context passed to subsequent
// interceptors and our service implementation.
return "Ali Baba", nil
const passphrase = "open-sesame"
if req.Header.Get("Authorization") != "Bearer "+passphrase {
// If authentication fails, we return an error. connectauth.Errorf is a
// convenient shortcut to produce an error coded with
// connect.CodeUnauthenticated.
return nil, connectauth.Errorf("try %q as a bearer token instead", passphrase)
}
// Once we've authenticated the request, we can return some information about
// the client. That information gets attached to the context passed to
// subsequent interceptors and our service implementation.
return "Ali Baba", nil
}

// This constructor would normally be generated by protoc-gen-connect-go. For
// this example, we'll use a small stub.
func NewHelloServiceHandler(svc any, opts ...connect.HandlerOption) (string, http.Handler) {
return "/hello.v1/Hello", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Service implementations can retrieve the authenticated identity of the
// caller from the context.
identity := connectauth.GetIdentity(r.Context())
fmt.Fprintf(w, "Hello, %v!", identity)
})
return "/hello.v1/Hello", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Service implementations can retrieve information about the authenticated
// caller from the context.
identity := connectauth.GetInfo(r.Context())
fmt.Fprintf(w, "Hello, %v!", identity)
})
}

func main() {
mux := http.NewServeMux()
mux.Handle(NewHelloServiceHandler(struct{}{}))
// Before starting the HTTP server, wrap the whole mux in our authenticating
// middleware.
middleware := connectauth.NewMiddleware(authenticate)
http.ListenAndServe("localhost:8080", middleware.Wrap(mux))
mux := http.NewServeMux()
mux.Handle(NewHelloServiceHandler(struct{}{}))
// Before starting the HTTP server, wrap the whole mux in our authenticating
// middleware.
middleware := connectauth.NewMiddleware(authenticate)
http.ListenAndServe("localhost:8080", middleware.Wrap(mux))
}
```

Expand Down

0 comments on commit 2c209c4

Please sign in to comment.