Skip to content

Commit

Permalink
Add dynoidtest example.
Browse files Browse the repository at this point in the history
  • Loading branch information
kennyp committed Oct 21, 2024
1 parent 4ad5994 commit ec9bcee
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 14 deletions.
61 changes: 54 additions & 7 deletions dynoid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ The [dynoid](<#dynoid>) package provides all of the functions needed to verify a

In the case that you want to verify a token outside of an [http.Handler][handler] you can leverage the [Verifier](<#Verifier>) directly.

### HTTP Middleware

The [dynoid/middleware](<#middleware>) package provides several `net/http` middleware that validate incoming requests are authenticated and adds the parsed token to the request context to be used further down the stack.

## Testing and Local Development

Expand Down Expand Up @@ -332,13 +335,7 @@ func init() {

func main() {
verifier := dynoid.New(AUDIENCE)
verifier.IssuerCallback = func(issuer string) error {
if issuer != "https://oidc.heroku.local/spaces/test" {
return fmt.Errorf("unexpected issuer %q", issuer)
}

return nil
}
verifier.IssuerCallback = dynoid.AllowHerokuHost("heroku.local") // heroku.com for production

t, err := verifier.Verify(ctx, token)
if err != nil {
Expand Down Expand Up @@ -526,6 +523,56 @@ type Issuer struct {
}
```

<details><summary>Example</summary>
<p>



```go
package main

import (
"context"
"fmt"

"github.com/heroku/x/dynoid"
"github.com/heroku/x/dynoid/dynoidtest"
)

const AUDIENCE = "testing"

func main() {
ctx, iss, err := dynoidtest.NewWithContext(context.Background())
if err != nil {
panic(err)
}

if err := dynoidtest.GenerateDefaultFS(iss, AUDIENCE); err != nil {
panic(err)
}

token, err := dynoid.ReadLocalToken(ctx, AUDIENCE)
if err != nil {
panic(err)
}

fmt.Println(token.Subject.AppID)
fmt.Println(token.Subject.AppName)
fmt.Println(token.Subject.Dyno)
}
```

#### Output

```
00000000-0000-0000-0000-000000000001
sushi
web.1
```

</p>
</details>

<a name="New"></a>
### func [New](<https://github.com/heroku/x/blob/master/dynoid/dynoidtest/dynoidtest.go#L86>)

Expand Down
35 changes: 35 additions & 0 deletions dynoid/dynoidtest/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package dynoidtest_test

import (
"context"
"fmt"

"github.com/heroku/x/dynoid"
"github.com/heroku/x/dynoid/dynoidtest"
)

const AUDIENCE = "testing"

func ExampleIssuer() {
ctx, iss, err := dynoidtest.NewWithContext(context.Background())
if err != nil {
panic(err)
}

if err := dynoidtest.GenerateDefaultFS(iss, AUDIENCE); err != nil {
panic(err)
}

token, err := dynoid.ReadLocalToken(ctx, AUDIENCE)
if err != nil {
panic(err)
}

fmt.Println(token.Subject.AppID)
fmt.Println(token.Subject.AppName)
fmt.Println(token.Subject.Dyno)
// Output:
// 00000000-0000-0000-0000-000000000001
// sushi
// web.1
}
8 changes: 1 addition & 7 deletions dynoid/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,7 @@ func init() {

func ExampleVerifier() {
verifier := dynoid.New(AUDIENCE)
verifier.IssuerCallback = func(issuer string) error {
if issuer != "https://oidc.heroku.local/spaces/test" {
return fmt.Errorf("unexpected issuer %q", issuer)
}

return nil
}
verifier.IssuerCallback = dynoid.AllowHerokuHost("heroku.local") // heroku.com for production

t, err := verifier.Verify(ctx, token)
if err != nil {
Expand Down

0 comments on commit ec9bcee

Please sign in to comment.