https://github.com/orijtech/authmid
Authentication middleware for signed requests, useful for webhook authentication verifying identities
https://github.com/orijtech/authmid
Last synced: 4 months ago
JSON representation
Authentication middleware for signed requests, useful for webhook authentication verifying identities
- Host: GitHub
- URL: https://github.com/orijtech/authmid
- Owner: orijtech
- License: apache-2.0
- Created: 2017-06-08T05:22:12.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-12-28T23:35:55.000Z (over 8 years ago)
- Last Synced: 2024-06-19T05:46:07.344Z (almost 2 years ago)
- Language: Go
- Size: 19.5 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# authmid
Authentication middleware for signed requests, useful for webhook authentication verifying identities
## Usage
Create your custom authenticator that conforms to interface Authenticator
so that you can custom lookup the secret, and headers and pass that into
Middleware to wrap the next handler. For example, simply:
```go
func main() {
http.Handle("/", authmid.Middleware(&sampleAuthChecker{}, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Authenticated pong!")
})))
// Then run the server to receive traffic.
}
```
Or for a more comprehensive end to end working example:
```go
func main() {
srv := httptest.NewServer(authmid.Middleware(&sampleAuthChecker{}, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := ioutil.ReadAll(r.Body)
fmt.Fprintf(w, "Well authenticated, and here is your body: %s", body)
})))
defer srv.Close()
// The client will then authenticate like this
req := makeReq("POST", []byte(`{"name": "foo", "age": 99}`), authKey1)
req.URL, _ = url.Parse(srv.URL)
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
blob, _ := ioutil.ReadAll(res.Body)
fmt.Printf("response: %s\n", blob)
}
```