-
Notifications
You must be signed in to change notification settings - Fork 55
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
Add the get-tokens command #56
Changes from 2 commits
fc8025e
1092016
9d34c32
d610b12
c8d7cb0
ba17144
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,12 +3,15 @@ package cli | |
import ( | ||
"fmt" | ||
|
||
"encoding/json" | ||
"github.com/auth0/auth0-cli/internal/ansi" | ||
"github.com/auth0/auth0-cli/internal/auth/authutil" | ||
"github.com/auth0/auth0-cli/internal/auth0" | ||
"github.com/auth0/auth0-cli/internal/open" | ||
"github.com/auth0/auth0-cli/internal/prompt" | ||
"gopkg.in/auth0.v5/management" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
const ( | ||
|
@@ -23,6 +26,47 @@ var ( | |
cliLoginTestingScopes []string = []string{"openid", "profile"} | ||
) | ||
|
||
// runClientCredentialsFlow runs an M2M client credentials flow without opening a browser | ||
func runClientCredentialsFlow(cli *cli, c *management.Client, clientID string, audience string, tenant tenant) (*authutil.TokenResponse, error) { | ||
|
||
var tokenResponse *authutil.TokenResponse | ||
|
||
url := "https://" + tenant.Domain + "/oauth/token" | ||
|
||
cli.renderer.Infof("Domain: " + tenant.Domain) | ||
cli.renderer.Infof("ClientID: " + clientID) | ||
cli.renderer.Infof("Type: Machine to Machine") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be useful at the command-level, so it works for both types of flow, have you considered moving it up there? |
||
fmt.Println() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will print to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
However, we could switch to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 actually, it would be nice if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So that's what this line is doing https://github.com/auth0/auth0-cli/pull/56/files#diff-32da9d2d97d78dec736cae68c2195111921918696567466ad99f78a291237298R52
A There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome! Okay that helps. Thanks Paddy There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The STDOUT split seems to work
|
||
|
||
client_secret := c.GetClientSecret() | ||
|
||
// TODO: Check if the audience is valid, and suggest a different client if it is wrong. | ||
|
||
payload := strings.NewReader("grant_type=client_credentials&client_id=" + clientID + "&client_secret=" + client_secret + "&audience=" + audience) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Neat, I've switched! |
||
|
||
err := ansi.Spinner("Waiting for token", func() error { | ||
req, _ := http.NewRequest("POST", url, payload) | ||
|
||
req.Header.Add("content-type", "application/x-www-form-urlencoded") | ||
|
||
res, err := http.DefaultClient.Do(req) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
defer res.Body.Close() | ||
|
||
err = json.NewDecoder(res.Body).Decode(&tokenResponse) | ||
if err != nil { | ||
return fmt.Errorf("cannot decode response: %w", err) | ||
} | ||
return nil | ||
}) | ||
|
||
return tokenResponse, err | ||
} | ||
|
||
// runLoginFlowPreflightChecks checks if we need to make any updates to the | ||
// client being tested in order to log in successfully. If so, it asks the user | ||
// to confirm whether to proceed. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use
url.URL
here, might be safer/cleaner than building the URL manually, there's an example at https://github.com/auth0/auth0-cli/blob/main/internal/auth/authutil/login.go#L34There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes! I was feeling like there must be a better way!