-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
258 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.