-
Notifications
You must be signed in to change notification settings - Fork 6
feat: improve auth with client credentials #296
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
56fe881
feat: improve auth with client credentials
jrschumacher 1335792
feat: add profiles
jrschumacher 24b10bd
resolve changes and missing set default
jrschumacher f50f086
Merge branch 'main' of github.com:opentdf/otdfctl into iss-171-add-pr…
jrschumacher cf1511b
rebase with main
jrschumacher 87b7eda
remove unused submodules
jrschumacher b7ff27d
update profiles
jrschumacher 8e110f5
working auth code flow
jakedoublev 33d1603
clear creds from profile
jakedoublev 66f4394
working logout
jakedoublev c68ec39
Merge branch 'main' into prof/auth-code
jakedoublev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 contains hidden or 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,60 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "github.com/opentdf/otdfctl/pkg/auth" | ||
| "github.com/opentdf/otdfctl/pkg/cli" | ||
| "github.com/opentdf/otdfctl/pkg/man" | ||
| "github.com/opentdf/otdfctl/pkg/profiles" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func auth_codeLogin(cmd *cobra.Command, args []string) { | ||
| fh := cli.NewFlagHelper(cmd) | ||
| clientID := fh.GetOptionalString("client-id") | ||
| tlsNoVerify := fh.GetOptionalBool("tls-no-verify") | ||
|
|
||
| cp := InitProfile(cmd, false) | ||
| printer := cli.NewPrinter(true) | ||
|
|
||
| printer.Println("Initiating login...") | ||
| tok, publicClientID, err := auth.LoginWithPKCE(cp.GetEndpoint(), clientID, tlsNoVerify) | ||
| if err != nil { | ||
| cli.ExitWithError("could not authenticate", err) | ||
| } | ||
| printer.Println("ok") | ||
|
|
||
| // Set the auth credentials to profile | ||
| if err := cp.SetAuthCredentials(profiles.AuthCredentials{ | ||
| AuthType: profiles.PROFILE_AUTH_TYPE_ACCESS_TOKEN, | ||
| AccessToken: profiles.AuthCredentialsAccessToken{ | ||
| PublicClientID: publicClientID, | ||
| AccessToken: tok.AccessToken, | ||
| Expiration: tok.Expiry.Unix(), | ||
| RefreshToken: tok.RefreshToken, | ||
| }, | ||
| }); err != nil { | ||
| cli.ExitWithError("failed to set auth credentials", err) | ||
| } | ||
|
|
||
| printer.Println("Storing credentials to profile in keyring...") | ||
| if err := cp.Save(); err != nil { | ||
| printer.Println("failed") | ||
| cli.ExitWithError("An error occurred while storing authentication credentials", err) | ||
| } | ||
| printer.Println("ok") | ||
| } | ||
|
|
||
| var codeLoginCmd *man.Doc | ||
|
|
||
| func init() { | ||
| codeLoginCmd = man.Docs.GetCommand("auth/login", | ||
| man.WithRun(auth_codeLogin), | ||
| ) | ||
| codeLoginCmd.Flags().StringP( | ||
| codeLoginCmd.GetDocFlag("client-id").Name, | ||
| codeLoginCmd.GetDocFlag("client-id").Shorthand, | ||
| codeLoginCmd.GetDocFlag("client-id").Default, | ||
| codeLoginCmd.GetDocFlag("client-id").Description, | ||
| ) | ||
| authCmd.AddCommand(&codeLoginCmd.Command) | ||
| } |
This file contains hidden or 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,42 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "github.com/opentdf/otdfctl/pkg/auth" | ||
| "github.com/opentdf/otdfctl/pkg/cli" | ||
| "github.com/opentdf/otdfctl/pkg/man" | ||
| "github.com/opentdf/otdfctl/pkg/profiles" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func auth_logout(cmd *cobra.Command, args []string) { | ||
| fh := cli.NewFlagHelper(cmd) | ||
| tlsNoVerify := fh.GetOptionalBool("tls-no-verify") | ||
| cp := InitProfile(cmd, false) | ||
| printer := cli.NewPrinter(true) | ||
| printer.Println("Initiating logout...") | ||
|
|
||
| // we can only revoke access tokens stored for the code login flow, not client credentials | ||
| creds := cp.GetAuthCredentials() | ||
| if creds.AuthType == profiles.PROFILE_AUTH_TYPE_ACCESS_TOKEN { | ||
| printer.Println("Revoking access token...") | ||
| if err := auth.RevokeAccessToken(cp.GetEndpoint(), creds.AccessToken.PublicClientID, creds.AccessToken.RefreshToken, tlsNoVerify); err != nil { | ||
| printer.Println("failed") | ||
| cli.ExitWithError("An error occurred while revoking the access token", err) | ||
| } | ||
| } | ||
|
|
||
| if err := cp.SetAuthCredentials(profiles.AuthCredentials{}); err != nil { | ||
| printer.Println("failed") | ||
| cli.ExitWithError("An error occurred while logging out", err) | ||
| } | ||
| printer.Println("ok") | ||
| } | ||
|
|
||
| var codeLogoutCmd *man.Doc | ||
|
|
||
| func init() { | ||
| codeLogoutCmd = man.Docs.GetCommand("auth/logout", | ||
| man.WithRun(auth_logout), | ||
| ) | ||
| authCmd.AddCommand(&codeLogoutCmd.Command) | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,21 +1,18 @@ | ||
| --- | ||
| title: Open a browser and login with Auth Code PKCE | ||
| title: Open a browser and login | ||
|
|
||
| command: | ||
| name: code-login | ||
| name: login | ||
| flags: | ||
| - name: client-id | ||
| description: A clientId for use in auth code flow (default = platform well-known public_client_id) | ||
| shorthand: i | ||
| required: false | ||
| - name: no-cache | ||
| description: Do not cache credentials on the native OS (print access token to stdout) | ||
| default: false | ||
| --- | ||
|
|
||
| Authenticate for use of the OpenTDF Platform through a browser (required). | ||
|
|
||
| Provide a specific public 'client-id' known to support the Auth Code PKCE flow and recognized | ||
| by the OpenTDF Platform, or use the default public client in the platform well-known configuration if not specified. | ||
|
|
||
| The OIDC Access Token will be stored in the OS-specific keychain by default, otherwise printed to `stdout` if `--no-cache` is passed. | ||
| The OIDC Access Token will be stored in the OS-specific keychain by default (Linux not yet supported). |
This file contains hidden or 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,9 @@ | ||
| --- | ||
| title: Clear credentials from profile | ||
|
|
||
| command: | ||
| name: logout | ||
| --- | ||
|
|
||
| Removes any auth credentials (Client Credentials or an Access Token from a login) | ||
| from the current profile. |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.