Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor:
hydra token introspect
command
BREAKING CHANGE: The `hydra token introspect` command has been renamed to `hydra introspect token` and now supports structured output (JSON, tables, ...).
- Loading branch information
Showing
13 changed files
with
177 additions
and
104 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 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,19 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/ory/x/cmdx" | ||
) | ||
|
||
func NewIntrospectCmd(root *cobra.Command) *cobra.Command { | ||
var cmd = &cobra.Command{ | ||
Use: "introspect", | ||
Aliases: []string{"ls"}, | ||
Short: "Introspect resources", | ||
} | ||
cmd.AddCommand(NewIntrospectTokenCmd(root)) | ||
cmdx.RegisterHTTPClientFlags(cmd.PersistentFlags()) | ||
cmdx.RegisterFormatFlags(cmd.PersistentFlags()) | ||
return cmd | ||
} |
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,59 @@ | ||
/* | ||
* Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* @author Aeneas Rekkas <aeneas+oss@aeneas.io> | ||
* @copyright 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io> | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/ory/hydra/cmd/cliclient" | ||
"github.com/ory/x/cmdx" | ||
"github.com/ory/x/flagx" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewIntrospectTokenCmd(parent *cobra.Command) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "token the-token", | ||
Args: cobra.ExactArgs(1), | ||
Example: fmt.Sprintf(`%s introspect token --client-id a0184d6c-b313-4e70-a0b9-905b581e9218 --client-secret Hh1BjioNNm ciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNT`, parent.Use), | ||
Short: "Introspect an OAuth 2.0 Access or Refresh Token", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
client, err := cliclient.NewClient(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
result, _, err := client.AdminApi.IntrospectOAuth2Token(cmd.Context()). | ||
Token(args[0]). | ||
Scope(strings.Join(flagx.MustGetStringSlice(cmd, "scope"), " ")).Execute() | ||
if err != nil { | ||
return cmdx.PrintOpenAPIError(cmd, err) | ||
} | ||
|
||
cmdx.PrintRow(cmd, outputOAuth2TokenIntrospection(*result)) | ||
return nil | ||
}, | ||
} | ||
cmd.Flags().StringSlice("scope", []string{}, "Additionally check if the scope was granted.") | ||
return cmd | ||
} |
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,47 @@ | ||
package cmd_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"golang.org/x/oauth2/clientcredentials" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/tidwall/gjson" | ||
|
||
"github.com/ory/hydra/cmd" | ||
"github.com/ory/x/cmdx" | ||
) | ||
|
||
func TestIntrospectToken(t *testing.T) { | ||
c := cmd.NewIntrospectTokenCmd(new(cobra.Command)) | ||
public, admin, reg := setupRoutes(t, c) | ||
require.NoError(t, c.Flags().Set(cmdx.FlagEndpoint, admin.URL)) | ||
|
||
expected := createClientCredentialsClient(t, reg) | ||
cc := clientcredentials.Config{ | ||
ClientID: expected.GetID(), | ||
ClientSecret: expected.Secret, | ||
TokenURL: public.URL + "/oauth2/token", | ||
Scopes: []string{}, | ||
} | ||
|
||
t.Run("case=checks valid token", func(t *testing.T) { | ||
token, err := cc.Token(context.Background()) | ||
require.NoError(t, err) | ||
|
||
actual := gjson.Parse(cmdx.ExecNoErr(t, c, token.AccessToken)) | ||
assert.Equal(t, expected.GetID(), actual.Get("sub").String()) | ||
assert.Equal(t, expected.GetID(), actual.Get("client_id").String()) | ||
assert.True(t, actual.Get("active").Bool()) | ||
}) | ||
|
||
t.Run("case=checks invalid token", func(t *testing.T) { | ||
actual := gjson.Parse(cmdx.ExecNoErr(t, c, "invalid-token")) | ||
assert.Empty(t, actual.Get("sub").String()) | ||
assert.Empty(t, actual.Get("client_id").String()) | ||
assert.False(t, actual.Get("active").Bool()) | ||
}) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
hydra "github.com/ory/hydra-client-go" | ||
) | ||
|
||
type ( | ||
outputOAuth2TokenIntrospection hydra.OAuth2TokenIntrospection | ||
) | ||
|
||
func (_ outputOAuth2TokenIntrospection) Header() []string { | ||
return []string{"ACTIVE", "SUBJECT", "CLIENT ID", "SCOPE", "EXPIRY", "TOKEN USE"} | ||
} | ||
|
||
func (i outputOAuth2TokenIntrospection) Columns() []string { | ||
return []string{ | ||
fmt.Sprintf("%v", i.Active), | ||
fmt.Sprintf("%v", i.Sub), | ||
fmt.Sprintf("%v", i.ClientId), | ||
fmt.Sprintf("%v", i.Scope), | ||
fmt.Sprintf("%v", i.Scope), | ||
fmt.Sprintf("%v", i.TokenUse), | ||
fmt.Sprintf("%v", time.Unix(*i.Exp, 0).String()), | ||
} | ||
} | ||
|
||
func (i outputOAuth2TokenIntrospection) Interface() interface{} { | ||
return i | ||
} |
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 was deleted.
Oops, something went wrong.