Skip to content

Commit

Permalink
docs: clean up TokenValid leftovers - closes #310
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeneas Rekkas (arekkas) authored and arekkas committed Nov 22, 2016
1 parent 17d2434 commit 994b596
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
27 changes: 23 additions & 4 deletions docs/sdk/go.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,43 @@ var keySet, err = hydra.JWK.GetKeySet("app-tls-keys")
var err = hydra.JWK.DeleteKeySet("app-tls-keys")
```

Validate access token, uses [`ory-am/hydra/warden.HTTPWarden`](warden/warden_http.go):

```go
import "golang.org/x/net/context"
import "github.com/ory-am/hydra/firewall"

func anyHttpHandler(w http.ResponseWriter, r *http.Request) {
// Check if a token is valid and the token's subject fulfills the policy based access request.
ctx, err := hydra.Introspection.IntrospectToken(context.Background(), "access-token", "photos", "files")
fmt.Sprintf("%s", ctx.Subject)
}
```

Validate requests with the Warden, uses [`ory-am/hydra/warden.HTTPWarden`](warden/warden_http.go):

```go
import "golang.org/x/net/context"
import "github.com/ory-am/hydra/firewall"

func anyHttpHandler(w http.ResponseWriter, r *http.Request) {
// Check if a token is valid and is allowed to operate given scopes
ctx, err := hydra.Warden.TokenValid(context.Background(), firewall.TokenFromRequest(r), "photos", "files")
fmt.Sprintf("%s", ctx.Subject)

// Check if a token is valid and the token's subject fulfills the policy based access request.
ctx, err := hydra.Warden.TokenAllowed(context.Background(), "access-token", &firewall.TokenAccessRequest{
Resource: "matrix",
Action: "create",
Context: ladon.Context{},
}, "photos", "files")
fmt.Sprintf("%s", ctx.Subject)

// Do the same thing but without a token
ctx, err := hydra.Warden.IsAllowed(context.Background(), &firewall.AccessRequest{
// Because no token is defined, we need to specify the subject manually
Subject: "peter",
Resource: "matrix",
Action: "create",
Context: ladon.Context{},
})
fmt.Sprintf("%s", ctx.Subject)
}
```

Expand Down
2 changes: 1 addition & 1 deletion firewall/warden.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ type Firewall interface {
// TokenFromRequest returns an access token from the HTTP Authorization header.
//
// func anyHttpHandler(w http.ResponseWriter, r *http.Request) {
// ctx, err := firewall.TokenValid(context.Background(), firewall.TokenFromRequest(r), "photos", "files")
// ctx, err := firewall.TokenAllowed(context.Background(), firewall.TokenFromRequest(r), "photos", "files")
// fmt.Sprintf("%s", ctx.Subject)
// }
TokenFromRequest(r *http.Request) string
Expand Down

0 comments on commit 994b596

Please sign in to comment.