Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: add token validation #134

Merged
merged 1 commit into from
Jul 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/cli/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Handler struct {
Connections *ConnectionHandler
Policies *PolicyHandler
Keys *JWKHandler
Warden *WardenHandler
}

func NewHandler(c *config.Config) *Handler {
Expand All @@ -17,5 +18,6 @@ func NewHandler(c *config.Config) *Handler {
Connections: newConnectionHandler(c),
Policies: newPolicyHandler(c),
Keys: newJWKHandler(c),
Warden: newWardenHandler(c),
}
}
47 changes: 47 additions & 0 deletions cmd/cli/handler_warden.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cli

import (
"encoding/json"
"fmt"

"github.com/ory-am/hydra/config"
"github.com/ory-am/hydra/pkg"
"github.com/ory-am/hydra/warden"
"github.com/spf13/cobra"
"golang.org/x/net/context"
)

type WardenHandler struct {
Config *config.Config
M *warden.HTTPWarden
}

func newWardenHandler(c *config.Config) *WardenHandler {
return &WardenHandler{
Config: c,
M: &warden.HTTPWarden{},
}
}

func (h *WardenHandler) IsAuthorized(cmd *cobra.Command, args []string) {
h.M.Client = h.Config.OAuth2Client(cmd)
h.M.Endpoint = h.Config.Resolve("/connections")

if len(args) != 1 {
fmt.Print(cmd.UsageString())
return
}

scopes, _ := cmd.Flags().GetStringSlice("scopes")
if len(scopes) == 0 {
scopes = []string{"core"}
}

res, err := h.M.Authorized(context.Background(), args[0], scopes...)
pkg.Must(err, "Could not validate token: %s", err)

out, err := json.MarshalIndent(res, "", "\t")
pkg.Must(err, "Could not marshall keys: %s", err)

fmt.Printf("%s\n", out)
}
9 changes: 5 additions & 4 deletions cmd/server/handler_oauth2_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package server
import (
"net/url"

"time"

"github.com/Sirupsen/logrus"
"github.com/go-errors/errors"
"github.com/julienschmidt/httprouter"
Expand All @@ -27,7 +29,6 @@ import (
"github.com/ory-am/hydra/pkg"
"golang.org/x/net/context"
r "gopkg.in/dancannon/gorethink.v2"
"time"
)

func injectFositeStore(c *config.Config, clients client.Manager) {
Expand Down Expand Up @@ -174,10 +175,10 @@ func newOAuth2Handler(c *config.Config, router *httprouter.Router, km jwk.Manage
Hasher: &hash.BCrypt{},
},
Consent: &oauth2.DefaultConsentStrategy{
Issuer: c.Issuer,
KeyManager: km,
Issuer: c.Issuer,
KeyManager: km,
DefaultChallengeLifespan: time.Hour,
DefaultIDTokenLifespan: time.Hour * 24,
DefaultIDTokenLifespan: time.Hour * 24,
},
ConsentURL: *consentURL,
}
Expand Down
17 changes: 17 additions & 0 deletions cmd/token_validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package cmd

import (
"github.com/spf13/cobra"
)

// validateCmd represents the validate command
var tokenValidatorCmd = &cobra.Command{
Use: "validate <token>",
Short: "Check if an access token is valid.",
Run: cmdHandler.Warden.IsAuthorized,
}

func init() {
tokenCmd.AddCommand(tokenValidatorCmd)
tokenValidatorCmd.Flags().StringSlice("scopes", []string{"core"}, "Additionally check if scope was granted")
}
8 changes: 4 additions & 4 deletions oauth2/consent_strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ const (
type DefaultConsentStrategy struct {
Issuer string

DefaultIDTokenLifespan time.Duration
DefaultIDTokenLifespan time.Duration
DefaultChallengeLifespan time.Duration
KeyManager jwk.Manager
KeyManager jwk.Manager
}

func (s *DefaultConsentStrategy) ValidateResponse(a fosite.AuthorizeRequester, token string) (claims *Session, err error) {
Expand Down Expand Up @@ -86,9 +86,9 @@ func (s *DefaultConsentStrategy) ValidateResponse(a fosite.AuthorizeRequester, t
func toStringSlice(i interface{}) []string {
if r, ok := i.([]string); ok {
return r
} else if r, ok := i.(fosite.Arguments); ok {
} else if r, ok := i.(fosite.Arguments); ok {
return r
} else if r, ok := i.([]interface{}); ok {
} else if r, ok := i.([]interface{}); ok {
ret := make([]string, 0)
for _, y := range r {
s, ok := y.(string)
Expand Down
5 changes: 3 additions & 2 deletions oauth2/consent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package oauth2

import (
"testing"
"github.com/stretchr/testify/assert"

"github.com/ory-am/fosite"
"github.com/stretchr/testify/assert"
)

func TestToStringSlice(t *testing.T) {
Expand All @@ -16,4 +17,4 @@ func TestToStringSlice(t *testing.T) {
assert.Equal(t, []string{"foo"}, toStringSlice((map[string]interface{}{
"scp": []interface{}{"foo", 123},
})["scp"]))
}
}
6 changes: 3 additions & 3 deletions oauth2/oauth2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ var handler = &Handler{
Hasher: hasher,
},
Consent: &DefaultConsentStrategy{
Issuer: "https://hydra.localhost",
KeyManager: keyManager,
Issuer: "https://hydra.localhost",
KeyManager: keyManager,
DefaultChallengeLifespan: time.Hour,
DefaultIDTokenLifespan: time.Hour * 24,
DefaultIDTokenLifespan: time.Hour * 24,
},
}

Expand Down
2 changes: 1 addition & 1 deletion sdk/client_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"io/ioutil"
"net/url"

"gopkg.in/yaml.v1"
"gopkg.in/yaml.v2"
)

// ClusterURL sets Hydra service URL
Expand Down
3 changes: 2 additions & 1 deletion sdk/client_opts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"os"
"testing"

"gopkg.in/yaml.v2"

"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v1"
)

func TestClusterURLOption(t *testing.T) {
Expand Down