Skip to content

Commit

Permalink
feat: hardcode temporary secret; update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
zeldan committed Aug 5, 2024
1 parent 41f59c1 commit 029485c
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 60 deletions.
55 changes: 7 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,55 +69,14 @@ List events
gcal list
```

## Google Calendar API Authentication with OAuth2

This guide will help you set up OAuth2 authentication for your Google Calendar API. Follow these steps to create a project, enable the API, and obtain the necessary credentials.

### Step 1: Create a New Project
1. Go to the [Google Developer Console](https://console.developers.google.com/).
2. Click on the **Create Project** button.
3. Enter a name for your project and click **Create**.

### Step 2: Enable the Google Calendar API
1. In the [Google Developer Console](https://console.developers.google.com/), navigate to the **Library** section.
2. Search for "Google Calendar API".
3. Click on the **Google Calendar API** and then click **Enable**.

### Step 3: Create OAuth2 Consent Screen
1. In the [Google Developer Console](https://console.developers.google.com/), navigate to the **OAuth consent screen** section.
2. Choose **External** as the user type and click **Create**.
3. Fill out the required app information:
- **App name**: `gcalcli`
- **User support email**: `[email protected]`
4. Fill out the required developer contact information:
- **Email addresses**: `[email protected]`
5. Click **Save and continue**.
6. Under **Scopes**, click **Save and continue**.
7. Under **Test users**, add your email (`[email protected]`).
8. Click **Save and continue**.

### Step 4: Create OAuth Client ID
1. In the [Google Developer Console](https://console.developers.google.com/), navigate to the **Credentials** section.
2. Click **Create credentials** and select **OAuth client ID**.
3. Select **Application type: Desktop app**.
4. Click **Create**.
5. Download the JSON file containing your client ID and secret.

### Step 5: Configure gcalcli
1. Create a directory for gcalcli configuration:
```sh
mkdir -p ~/.gcal
```
2. Place the downloaded JSON file into the `~/.gcal` directory:
```sh
mv /path/to/your/downloaded/secret.json ~/.gcal/secret.json
```

### Step 6: Authenticate with Google

## Authentication

By default, a hardcoded and unverified Google secret is included in the project. This secret has a user cap limit and can be used temporarily. If you want to use your custom authentication (via Google Console), you can find step-by-step instructions [here](docs/custom_auth.md).

How it works in the background ?

1. Start gcalcli. The authentication process will begin automatically.
2. Follow the instructions to complete the authentication process.

The resulting token will be stored in the `~/.gcal/store.json` file.

By following these steps, you will have successfully set up OAuth2 authentication for your Google Calendar API using gcalcli.

43 changes: 43 additions & 0 deletions docs/custom_auth.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
## Google Calendar API Authentication with OAuth2

This guide will help you set up OAuth2 authentication for your Google Calendar API. Follow these steps to create a project, enable the API, and obtain the necessary credentials.

### Step 1: Create a New Project
1. Go to the [Google Developer Console](https://console.developers.google.com/).
2. Click on the **Create Project** button.
3. Enter a name for your project and click **Create**.

### Step 2: Enable the Google Calendar API
1. In the [Google Developer Console](https://console.developers.google.com/), navigate to the **Library** section.
2. Search for "Google Calendar API".
3. Click on the **Google Calendar API** and then click **Enable**.

### Step 3: Create OAuth2 Consent Screen
1. In the [Google Developer Console](https://console.developers.google.com/), navigate to the **OAuth consent screen** section.
2. Choose **External** as the user type and click **Create**.
3. Fill out the required app information:
- **App name**: `gcalcli`
- **User support email**: `[email protected]`
4. Fill out the required developer contact information:
- **Email addresses**: `[email protected]`
5. Click **Save and continue**.
6. Under **Scopes**, click **Save and continue**.
7. Under **Test users**, add your email (`[email protected]`).
8. Click **Save and continue**.

### Step 4: Create OAuth Client ID
1. In the [Google Developer Console](https://console.developers.google.com/), navigate to the **Credentials** section.
2. Click **Create credentials** and select **OAuth client ID**.
3. Select **Application type: Desktop app**.
4. Click **Create**.
5. Download the JSON file containing your client ID and secret.

### Step 5: Configure gcalcli
1. Create a directory for gcalcli configuration:
```sh
mkdir -p ~/.gcal
```
2. Place the downloaded JSON file into the `~/.gcal` directory:
```sh
mv /path/to/your/downloaded/secret.json ~/.gcal/secret.json
```
52 changes: 40 additions & 12 deletions src/util/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,39 @@ pub async fn auth() -> Result<CalendarHub<HttpsConnector<HttpConnector>>, Box<dy
let secret_absolute_path = file::get_absolute_path(".gcal/secret.json")?;
let secret_path = std::path::Path::new(&secret_absolute_path);
let _ = file::ensure_directory_exists(secret_path);
let secret = read_google_secret(secret_path).await?;
let auth_builder = match read_google_secret(secret_path).await {
Ok(secret) => oauth2::InstalledFlowAuthenticator::builder(
secret,
oauth2::InstalledFlowReturnMethod::HTTPRedirect,
),
Err(_) => {
let secret: oauth2::ApplicationSecret = ApplicationSecret {
auth_uri: "https://accounts.google.com/o/oauth2/auth".to_string(),
client_secret: "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".to_string(),
token_uri: "https://accounts.google.com/o/oauth2/token".to_string(),
redirect_uris: vec!["urn:ietf:wg:oauth:2.0:oob".to_string()],
client_id:
"602236549045-3gcv7m50sp1d6vvqklimb5oaasp9ihi9.apps.googleusercontent.com"
.to_string(),
auth_provider_x509_cert_url: Some(
"https://www.googleapis.com/oauth2/v1/certs".to_string(),
),
project_id: None,
client_email: None,
client_x509_cert_url: None,
};
oauth2::InstalledFlowAuthenticator::builder(
secret,
oauth2::InstalledFlowReturnMethod::HTTPRedirect,
)
}
};

let store_path = file::get_absolute_path(".gcal/store.json")?;
let auth = oauth2::InstalledFlowAuthenticator::builder(
secret,
oauth2::InstalledFlowReturnMethod::HTTPRedirect,
)
.persist_tokens_to_disk(&store_path)
.build()
.await?;
let auth = auth_builder
.persist_tokens_to_disk(&store_path)
.build()
.await?;

let scopes = &[
"https://www.googleapis.com/auth/calendar",
Expand All @@ -47,7 +70,7 @@ pub async fn auth() -> Result<CalendarHub<HttpsConnector<HttpConnector>>, Box<dy
];

match auth.token(scopes).await {
Ok(_) => {},
Ok(_) => {}
Err(e) => println!("Authentication error: {:?}", e),
}

Expand All @@ -63,12 +86,12 @@ pub async fn auth() -> Result<CalendarHub<HttpsConnector<HttpConnector>>, Box<dy
Ok(hub)
}


pub async fn get_default_timezone(hub: &CalendarHub<HttpsConnector<HttpConnector>>) -> Result<Tz> {
let result = hub.settings().list().doit().await;
let settings = result.unwrap().1.items.unwrap_or_default();

let timezone_setting = settings.iter()
let timezone_setting = settings
.iter()
.find(|setting| setting.id == Some("timezone".to_string()))
.ok_or("Timezone setting not found");

Expand Down Expand Up @@ -97,6 +120,11 @@ pub async fn get_default_timezone(hub: &CalendarHub<HttpsConnector<HttpConnector
async fn read_google_secret(path: &Path) -> Result<ApplicationSecret> {
let secret = oauth2::read_application_secret(path)
.await
.with_context(|| format!("Failed to read the Google application secret file from path {:?}.", path))?;
.with_context(|| {
format!(
"Failed to read the Google application secret file from path {:?}.",
path
)
})?;
Ok(secret)
}

0 comments on commit 029485c

Please sign in to comment.