Skip to content

Commit

Permalink
add examples to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
marcia-piccione committed Sep 11, 2024
1 parent 5d4e1f7 commit 92e5ece
Showing 1 changed file with 102 additions and 1 deletion.
103 changes: 102 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,112 @@ A library for authentication and authorization workflows.
Bascule provides authentication and authorization workflows, particularly focused on the needs of an HTTP server.

## Table of Contents

- [Usage](#usage)
- [Code of Conduct](#code-of-conduct)
- [Install](#install)
- [Contributing](#contributing)


## Usage

### Create Basic Auth Middleware For Http Requests

> tp, err := basculehttp.NewAuthorizationParser(
basculehttp.WithBasic(),
)
if err != nil {
fmt.Prrintln(err)
}

m, err := basculehttp.NewMiddleware(
basculehttp.UseAuthenticator(

basculehttp.NewAuthenticator(
bascule.WithTokenParsers(tp),
bascule.WithValidators(
bascule.AsValidator[*http.Request](
func(token bascule.Token) error {
if basic, ok := token.(basculehttp.BasicToken); ok && basic.UserName() == "some-username" && basic.Password() == "some-password" {
return nil
}

return bascule.ErrBadCredentials
},
),
),
),
),
)

### Create JWT Auth Middleware For Http Requests

> // get public keys with automatic refresh
keyUrl := "http://localhost/keys"
cache := jwk.NewCache(context.Background())
err := cache.Register(keyUrl, jwk.WithRefreshInterval(time.Duration(p.refreshIntervalHours)*time.Hour))
keys, err := jwk.NewCachedSet(cache, keyUrl), err
if err != nil {
fmt.Println("error getting public keys")
}

// create token parser

jwtp, err := basculejwt.NewTokenParser(jwt.WithKeySet(keys))
if err != nil {
fmt.Println("error creating token parser")
}

tp, err := basculehttp.NewAuthorizationParser(
basculehttp.WithScheme(basculehttp.SchemeBearer, jwtp),
)
if err != nil {
fmt.Println("error creating authoritization parser")
}


// create middleware

_, err = basculehttp.NewMiddleware(
basculehttp.UseAuthenticator(
basculehttp.NewAuthenticator(
bascule.WithTokenParsers(tp),
bascule.WithValidators(
bascule.AsValidator[*http.Request](
func(token bascule.Token) error {
_, ok := token.(basculejwt.Claims)
if !ok {
return bascule.ErrBadCredentials
}

capabilities, _ := bascule.GetCapabilities(token)

// perform capability and other validation checks here

fmt.Println(capabilities)

return nil
},
),
),
),
),
)

if (err != nil) {
fmt.Println("error creating middleware")
}


### Use Middleware To Intercept Http Requests

> func getHandlers(m *basculehttp.Middleware, next http.Handler) http.Handler {
return m.ThenFunc(
next.ServeHTTP,
)
}



## Code of Conduct

This project and everyone participating in it are governed by the [XMiDT Code Of Conduct](https://xmidt.io/code_of_conduct/).
Expand Down

0 comments on commit 92e5ece

Please sign in to comment.