Skip to content

Commit

Permalink
Merge branch 'main' into feat/192
Browse files Browse the repository at this point in the history
  • Loading branch information
Xemdo authored Dec 3, 2023
2 parents 2d3755d + 11a468d commit 5b3c938
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cmd/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func configureCmdRun(cmd *cobra.Command, args []string) error {
}

if err := viper.WriteConfigAs(configPath); err != nil {
fmt.Errorf("Failed to write configuration: %v", err.Error())
return fmt.Errorf("Failed to write configuration: %v", err.Error())
}

fmt.Println("Updated configuration.")
Expand Down
1 change: 1 addition & 0 deletions cmd/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ https://dev.twitch.tv/docs/eventsub/handling-webhook-events#processing-an-event`
Secret: secret,
Timestamp: timestamp,
EventID: eventID,
Version: version,
})

if err != nil {
Expand Down
16 changes: 11 additions & 5 deletions cmd/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var validateToken string
var overrideClientId string
var tokenServerPort int
var tokenServerIP string
var redirectHost string

// loginCmd represents the login command
var loginCmd = &cobra.Command{
Expand All @@ -37,20 +38,25 @@ func init() {
loginCmd.Flags().StringVarP(&revokeToken, "revoke", "r", "", "Instead of generating a new token, revoke the one passed to this parameter.")
loginCmd.Flags().StringVarP(&validateToken, "validate", "v", "", "Instead of generating a new token, validate the one passed to this parameter.")
loginCmd.Flags().StringVar(&overrideClientId, "client-id", "", "Override/manually set client ID for token actions. By default client ID from CLI config will be used.")
loginCmd.Flags().StringVar(&tokenServerIP, "ip", "localhost", "Manually set the IP address to be binded to for the User Token web server.")
loginCmd.Flags().StringVar(&tokenServerIP, "ip", "", "Manually set the IP address to be bound to for the User Token web server.")
loginCmd.Flags().IntVarP(&tokenServerPort, "port", "p", 3000, "Manually set the port to be used for the User Token web server.")
loginCmd.Flags().StringVar(&redirectHost, "redirect-host", "localhost", "Manually set the host to be used for the redirect URL")
}

func loginCmdRun(cmd *cobra.Command, args []string) error {
clientID = viper.GetString("clientId")
clientSecret = viper.GetString("clientSecret")

webserverPort := strconv.Itoa(tokenServerPort)
redirectURL := fmt.Sprintf("http://%v:%v", tokenServerIP, webserverPort)
redirectURL := fmt.Sprintf("http://%v:%v", redirectHost, webserverPort)

if clientID == "" || clientSecret == "" {
println("No Client ID or Secret found in configuration. Triggering configuration now.")
configureCmd.Run(cmd, args)
err := configureCmd.RunE(cmd, args)
if err != nil {
return err
}

clientID = viper.GetString("clientId")
clientSecret = viper.GetString("clientSecret")
}
Expand All @@ -76,7 +82,7 @@ func loginCmdRun(cmd *cobra.Command, args []string) error {
p.URL = login.ValidateTokenURL
r, err := login.ValidateCredentials(p)
if err != nil {
return fmt.Errorf("Failed to validate: %v", err.Error())
return fmt.Errorf("failed to validate: %v", err.Error())
}

tokenType := "App Access Token"
Expand Down Expand Up @@ -105,7 +111,7 @@ func loginCmdRun(cmd *cobra.Command, args []string) error {
fmt.Println(white("- %v\n", s))
}
}
} else if isUserToken == true {
} else if isUserToken {
p.URL = login.UserCredentialsURL
login.UserCredentialsLogin(p, tokenServerIP, webserverPort)
} else {
Expand Down
38 changes: 30 additions & 8 deletions docs/token.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ Access tokens can be revoked with:
twitch token -r 0123456789abcdefghijABCDEFGHIJ
```

## Alternate IP for User Token Webserver

If you'd like to bind the webserver used for user tokens (`-u` flag), you can override it with the `--ip` flag. For example:

```
twitch token -u --ip 127.0.0.1"
```

## Alternate Port

Port 3000 on localhost is used by default when fetching User Access Tokens. The `-p` flag can be used to change to another port if another service is already occupying that port. For example:
Expand All @@ -108,6 +116,19 @@ twitch token -u -p 3030 -s "moderator:manage:shoutouts moderator:manage:shield_m
NOTE: You must update the first entry in the _OAuth Redirect URLs_ section of your app's management page in the [Developer's Application Console](https://dev.twitch.tv/console/apps) to match the new port number. Make sure there is no `/` at the end of the URL (e.g. use `http://localhost:3030` and not `http://localhost:3030/`) and that the URL is the first entry in the list if there is more than one.


## Alternate Host

If you'd like to change the hostname for one reason or another (e.g. binding to a local domain), you can use the `--redirect-host` to change the domain. You should _not_ prefix it with `http` or `https`.

Example:

```
twitch token -u --redirect-host contoso.com
```

NOTE: You must update the first entry in the _OAuth Redirect URLs_ section of your app's management page in the [Developer's Application Console](https://dev.twitch.tv/console/apps) to match the new port number. Make sure there is no `/` at the end of the URL (e.g. use `http://localhost:3030` and not `http://localhost:3030/`) and that the URL is the first entry in the list if there is more than one.


## Errors

This error occurs when there's a problem with the OAuth Redirect URLs. Check in the app's management page in the [Developer's Application Console](https://dev.twitch.tv/console/apps) to ensure the first entry is set to `http://localhost:3000`. Specifically, verify that your using `http` and not `https` and that the URL does not end with a `/`. (If you've changed ports with the `-p` flag, ensure those numbers match as well)
Expand All @@ -126,14 +147,15 @@ None.

**Flags**

| Flag | Shorthand | Description | Example | Required? (Y/N) |
|----------------|-----------|-------------------------------------------------------------------------------------------------------|----------------------------------------------|-----------------|
| `--user-token` | `-u` | Whether to fetch a user token or not. Default is false. | `token -u` | N |
| `--scopes` | `-s` | The space separated scopes to use when getting a user token. | `-s "user:read:email user_read"` | N |
| `--revoke` | `-r` | Instead of generating a new token, revoke the one passed to this parameter. | `-r 0123456789abcdefghijABCDEFGHIJ` | N |
| `--port` | `-p` | Override/manually set the port for token actions. (The default is 3000) | `-p 3030` | N |
| `--client-id` | | Override/manually set client ID for token actions. By default client ID from CLI config will be used. | `--client-id uo6dggojyb8d6soh92zknwmi5ej1q2` | N |

| Flag | Shorthand | Description | Example | Required? (Y/N) |
|-------------------|-----------|----------------------------------------------------------------------------------------------------------------|----------------------------------------------|-----------------|
| `--user-token` | `-u` | Whether to fetch a user token or not. Default is false. | `token -u` | N |
| `--scopes` | `-s` | The space separated scopes to use when getting a user token. | `-s "user:read:email user_read"` | N |
| `--revoke` | `-r` | Instead of generating a new token, revoke the one passed to this parameter. | `-r 0123456789abcdefghijABCDEFGHIJ` | N |
| `--ip` | | Manually set the port to be used for the User Token web server. The default binds to all interfaces. (0.0.0.0) | `--ip 127.0.0.1` | N |
| `--port` | `-p` | Override/manually set the port for token actions. (The default is 3000) | `-p 3030` | N |
| `--client-id` | | Override/manually set client ID for token actions. By default client ID from CLI config will be used. | `--client-id uo6dggojyb8d6soh92zknwmi5ej1q2` | N |
| `--redirect-host` | | Override/manually set the redirect host token actions. The default is `localhost` | `--redirect-host contoso.com` | N |

## Notes

Expand Down

0 comments on commit 5b3c938

Please sign in to comment.