Skip to content

Commit

Permalink
chg: [core] Improved managements of arguments and scripts.
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricbonhomme committed Oct 28, 2024
1 parent 903221e commit 6aeb31d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 38 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ A client to gather vulnerability-related information from the Fediverse.

```bash
$ poetry install
$ poetry shell
$ cp config.py.sample config.py
```

Expand All @@ -21,7 +20,8 @@ Set the configuration variables in config.py as appropriate for your environment
### Register your application

```bash
$ python register.py
$ poetry shell
$ FediVuln-Register
```

This script uses OAuth in order to retrieve the access token. This is achieved in several steps.
Expand All @@ -35,15 +35,16 @@ This script uses OAuth in order to retrieve the access token. This is achieved i
You only have to execute it once.


## Streaming
### Streaming


```bash
$ poetry shell
$ FediVuln-Stream --user
```


## Publishing
### Publishing

```bash
$ python publish.py
Expand Down
8 changes: 6 additions & 2 deletions fedivuln/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ def publish(message):
mastodon.status_post(message)


if __name__ == "__main__":
# Point of entry in execution mode.
def main():
parser = argparse.ArgumentParser(prog="FediVuln-Publish")
parser.add_argument(
"-i",
Expand All @@ -30,3 +29,8 @@ def publish(message):
arguments = parser.parse_args()

publish(arguments.message)


if __name__ == "__main__":
# Point of entry in execution mode.
main()
71 changes: 39 additions & 32 deletions fedivuln/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,42 @@

from fedivuln import config

# Step 1: Register the application with Mastodon instance, including all necessary scopes
Mastodon.create_app(
"Vulnerability-Lookup",
api_base_url=config.api_base_url,
to_file="mastodon_clientcred.secret",
scopes=config.scopes,
)

# Step 2: Instantiate Mastodon client with client credentials
mastodon = Mastodon(client_id="mastodon_clientcred.secret")

# Step 3: Log in - Generate authorization URL with the exact same scopes
login_url = mastodon.auth_request_url(
client_id="mastodon_clientcred.secret",
scopes=["read", "write", "follow", "push"], # Match scopes here
redirect_uris="urn:ietf:wg:oauth:2.0:oob",
)
print("Go to this URL to authorize: ", login_url)

# # Step 4: Once the user authorizes, prompt for the authorization code
authorization_code = input("Enter the code you got after authorization: ")

# Step 5: Use the authorization code to retrieve the access token, with the same scopes
access_token = mastodon.log_in(
code=authorization_code,
scopes=["read", "write", "follow", "push"], # Match scopes here
to_file="mastodon_usercred.secret",
)

# Example API call: Fetch the authenticated user's profile
# user_profile = mastodon.account_verify_credentials()
# print(f"Authenticated as: {user_profile['username']}")

def main():
# Step 1: Register the application with Mastodon instance, including all necessary scopes
Mastodon.create_app(
"Vulnerability-Lookup",
api_base_url=config.api_base_url,
to_file="mastodon_clientcred.secret",
scopes=config.scopes,
)

# Step 2: Instantiate Mastodon client with client credentials
mastodon = Mastodon(client_id="mastodon_clientcred.secret")

# Step 3: Log in - Generate authorization URL with the exact same scopes
login_url = mastodon.auth_request_url(
client_id="mastodon_clientcred.secret",
scopes=["read", "write", "follow", "push"], # Match scopes here
redirect_uris="urn:ietf:wg:oauth:2.0:oob",
)
print("Go to this URL to authorize: ", login_url)

# # Step 4: Once the user authorizes, prompt for the authorization code
authorization_code = input("Enter the code you got after authorization: ")

# Step 5: Use the authorization code to retrieve the access token, with the same scopes
mastodon.log_in(
code=authorization_code,
scopes=["read", "write", "follow", "push"], # Match scopes here
to_file="mastodon_usercred.secret",
)

# Example API call: Fetch the authenticated user's profile
# user_profile = mastodon.account_verify_credentials()
# print(f"Authenticated as: {user_profile['username']}")


if __name__ == "__main__":
# Point of entry in execution mode.
main()
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ classifiers = [


[tool.poetry.scripts]
FediVuln-Register = "fedivuln.register:main"
FediVuln-Stream = "fedivuln.stream:main"
FediVuln-Publish = "fedivuln.publish:main"


[tool.poetry.dependencies]
Expand Down

0 comments on commit 6aeb31d

Please sign in to comment.