Skip to content

Commit

Permalink
Add app authentication method and update README with OAuth setup inst…
Browse files Browse the repository at this point in the history
…ructions and images.
  • Loading branch information
santiagomed committed Dec 31, 2024
1 parent 120eafe commit dea95bf
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 16 deletions.
33 changes: 17 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,26 @@ Optional environment variables:

### Authentication

You must have a developer account and app to use this tool.

App authentication:
```bash
xurl auth app --bearer-token BEARER_TOKEN
```

OAuth 2.0 authentication:
```bash
xurl auth oauth2
```

**Note:** For OAuth 2.0 authentication, you must specify the redirect URI in the [X API developer portal](https://developer.x.com/en/portal/dashboard).

1. Create an app at the [X API developer portal](https://developer.x.com/en/portal/dashboard).
2. Go to authentication settings and set the redirect URI to `http://localhost:8080/callback`.
![Setup](./assets/setup.png)
![Redirect URI](./assets/callback.png)
3. Set the client ID and secret in your environment variables.

OAuth 1.0a authentication:
```bash
xurl auth oauth1 --consumer-key KEY --consumer-secret SECRET --access-token TOKEN --token-secret SECRET
Expand All @@ -54,6 +69,7 @@ Clear authentication:
xurl auth clear --all # Clear all tokens
xurl auth clear --oauth1 # Clear OAuth 1.0a tokens
xurl auth clear --oauth2-username USERNAME # Clear specific OAuth 2.0 token
xurl auth clear --bearer # Clear bearer token
```

### Making Requests
Expand All @@ -77,6 +93,7 @@ Specify authentication type:
```bash
xurl --auth oauth2 /2/users/me
xurl --auth oauth1 /2/tweets
xurl --auth app /2/users/me
```

Use specific OAuth 2.0 account:
Expand All @@ -88,16 +105,6 @@ xurl --username johndoe /2/users/me

Tokens are stored securely in `~/.xurl` in your home directory.

## Development

The project uses the following structure:
- `src/main.rs`: Entry point and command handling
- `src/auth/`: Authentication implementation
- `src/api/`: API client implementation
- `src/cli/`: Command-line interface definitions
- `src/config/`: Configuration management
- `src/error/`: Error types and handling

## Testing

Run the test suite:
Expand All @@ -108,11 +115,5 @@ cargo test
## Contributing
Contributions are welcome!

1. Fork the repository
2. Create your feature branch
3. Commit your changes
4. Push to the branch
5. Create a new Pull Request

## License
This project is open-sourced under the MIT License - see the LICENSE file for details.
Binary file added assets/callback.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/setup.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions src/auth/token_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ impl TokenStore {
self.oauth1_tokens.is_some()
}

pub fn has_bearer_token(&self) -> bool {
self.bearer_token.is_some()
}

fn save_to_file(&self) -> Result<(), TokenStoreError> {
let content =
serde_json::to_string(&self).map_err(|_| TokenStoreError::JSONSerializationError)?;
Expand Down
8 changes: 8 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ async fn main() -> Result<(), Error> {
"Not configured"
}
);
println!(
"App Auth: {}",
if store.has_bearer_token() {
"Configured"
} else {
"Not configured"
}
);
}

AuthCommands::Clear {
Expand Down

0 comments on commit dea95bf

Please sign in to comment.