Skip to content

Commit

Permalink
Added examples (#31)
Browse files Browse the repository at this point in the history
* Added examples

* remove os exit
  • Loading branch information
kristinapathak authored Jul 8, 2019
1 parent 5022272 commit 3974295
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
*.so
*.dylib

# Example binaries
*/examples/acquirer/acquirer
*/examples/basculehttp/basculehttp

# Test binary, build with `go test -c`
*.test

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The library for authorization: both acquiring and validating.
[![Go Report Card](https://goreportcard.com/badge/github.com/Comcast/comcast-bascule)](https://goreportcard.com/report/github.com/Comcast/comcast-bascule)
[![Apache V2 License](http://img.shields.io/badge/license-Apache%20V2-blue.svg)](https://github.com/Comcast/comcast-bascule/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/Comcast/comcast-bascule.svg)](CHANGELOG.md)
[![GoDoc](https://godoc.org/github.com/Comcast/comcast-bascule/bascule?status.svg)](https://godoc.org/github.com/Comcast/comcast-bascule/bascule)

## Summary

Expand All @@ -27,7 +28,7 @@ expires.
## Validating Authorization

Validation of Tokens happens once an authorization value has been parsed into
something that implements the Token interface. TODO: Add link to interface in GoDoc.
something that implements the [Token interface](https://godoc.org/github.com/Comcast/comcast-bascule/bascule#Token).
The `basculehttp` subpackage provides http decorators/middleware that will parse an http
request into a Token and validate it with configurable rules.

Expand Down
2 changes: 2 additions & 0 deletions bascule/basculehttp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

The package for auth related middleware, implemented as [alice-style http decorators](https://github.com/justinas/alice).

[![GoDoc](https://godoc.org/github.com/Comcast/comcast-bascule/bascule/basculehttp?status.svg)](https://godoc.org/github.com/Comcast/comcast-bascule/bascule/basculehttp)

## Summary

This package makes it easy to validate the Authorization of an incoming http
Expand Down
3 changes: 0 additions & 3 deletions bascule/basculehttp/examples/example.go

This file was deleted.

48 changes: 48 additions & 0 deletions bascule/examples/acquirer/acquirer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"fmt"
"io/ioutil"
"net/http"
"os"
"time"

"github.com/Comcast/comcast-bascule/bascule/acquire"
)

func main() {
// set up acquirer and add the auth to the request
acquirer := acquire.NewBasicAcquirerPlainText("testuser", "testpass")
request, err := http.NewRequest(http.MethodGet, "http://localhost:6000/test", nil)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create request: %v\n", err.Error())
os.Exit(1)
}
if err = acquire.AddAuth(request, acquirer); err != nil {
fmt.Fprintf(os.Stderr, "failed to add auth: %v\n", err.Error())
os.Exit(1)
}

httpclient := &http.Client{
Timeout: 5 * time.Second,
}
resp, err := httpclient.Do(request)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to send request: %v\n", err.Error())
os.Exit(1)
}
defer resp.Body.Close()

if resp.Body != nil {
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to read body: %v\n", err.Error())
os.Exit(1)
}
// output the body if it's good
fmt.Fprintf(os.Stdout, "Body: \n%s\n", respBody)
}
// output the code
fmt.Fprintf(os.Stdout, "Status code received: %v\n", resp.StatusCode)
os.Exit(0)
}
70 changes: 70 additions & 0 deletions bascule/examples/basculehttp/basculehttp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"context"
"net/http"
"os"

"github.com/Comcast/comcast-bascule/bascule"
"github.com/Comcast/comcast-bascule/bascule/basculehttp"
"github.com/Comcast/webpa-common/logging"
"github.com/go-kit/kit/log"
"github.com/gorilla/mux"
"github.com/justinas/alice"
)

func SetLogger(logger log.Logger) func(delegate http.Handler) http.Handler {
return func(delegate http.Handler) http.Handler {
return http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
ctx := r.WithContext(logging.WithLogger(r.Context(),
log.With(logger, "requestHeaders", r.Header, "requestURL", r.URL.EscapedPath(), "method", r.Method)))
delegate.ServeHTTP(w, ctx)
})
}
}

func GetLogger(ctx context.Context) bascule.Logger {
return log.With(logging.GetLogger(ctx), "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
}

// currently only sets up basic auth
func authChain(logger log.Logger) alice.Chain {
basicAllowed := map[string]string{
"testuser": "testpass",
"pls": "letmein",
}
options := []basculehttp.COption{
basculehttp.WithCLogger(GetLogger),
basculehttp.WithTokenFactory("Basic", basculehttp.BasicTokenFactory(basicAllowed)),
}

authConstructor := basculehttp.NewConstructor(options...)

basicRules := bascule.Validators{
bascule.CreateNonEmptyPrincipalCheck(),
bascule.CreateNonEmptyTypeCheck(),
bascule.CreateValidTypeCheck([]string{"basic"}),
}

authEnforcer := basculehttp.NewEnforcer(
basculehttp.WithELogger(GetLogger),
basculehttp.WithRules("Basic", basicRules),
)

return alice.New(SetLogger(logger), authConstructor, authEnforcer)
}

func simpleResponse(writer http.ResponseWriter, request *http.Request) {
writer.Write([]byte("good auth!"))
writer.WriteHeader(200)
return
}

func main() {
logger := log.NewJSONLogger(log.NewSyncWriter(os.Stdout))
router := mux.NewRouter()
authFuncs := authChain(logger)
router.Handle("/test", authFuncs.ThenFunc(simpleResponse))
http.ListenAndServe(":6000", router)
}
10 changes: 7 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ module github.com/Comcast/comcast-bascule
go 1.12

require (
github.com/Comcast/webpa-common v0.0.0-20190312224005-400bb4f8fc50
github.com/Comcast/webpa-common v1.1.0
github.com/SermoDigital/jose v0.9.2-0.20161205224733-f6df55f235c2
github.com/VividCortex/gohistogram v1.0.0 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/go-kit/kit v0.8.0
github.com/go-logfmt/logfmt v0.4.0 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/goph/emperror v0.17.1
github.com/gorilla/mux v1.7.3
github.com/influxdata/influxdb v1.7.7 // indirect
github.com/jtacoma/uritemplates v1.0.0 // indirect
github.com/justinas/alice v0.0.0-20171023064455-03f45bd4b7da
github.com/pkg/errors v0.8.0
github.com/spf13/viper v1.4.0 // indirect
github.com/stretchr/testify v1.3.0
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
)
Loading

0 comments on commit 3974295

Please sign in to comment.