Skip to content

Commit

Permalink
fix(auth): Persist refresh token after used
Browse files Browse the repository at this point in the history
Close #2
Remove deprecated Client credentials grant type to obtain token
Fix versions in docker-compose
  • Loading branch information
nioc committed Dec 5, 2023
1 parent 8c46a74 commit b4687e8
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 51 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
vendor
mock/
netatmo-collect.log
tokens.json
38 changes: 7 additions & 31 deletions Netatmo.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,38 +88,14 @@ public function getExistingTokens()
unset($tokens['access_token']);
}
$this->client->setTokensFromStore($tokens);
$this->logger->debug('Using existing tokens');
}
return true;
}

/**
* Authentication with Netatmo server (OAuth2)
*
* @param string $username Netatmo account username
* @param string $password Netatmo account password
*
* @return Authentication result
*/
public function getToken($username, $password)
{
if (!$this->isMocked) {
try {
$this->logger->debug('Request token with provided username and password');
$this->client->setVariable('username', $username);
$this->client->setVariable('password', $password);
$tokens = $this->client->getAccessToken();
} catch (Netatmo\Exceptions\NAClientException $e) {
$this->logger->error('An error occured while trying to get tokens, check your username and password');
$this->logger->debug('Reason: '.$e->getMessage());
return false;
$newTokens = $this->client->getAccessToken();
if (array_key_exists('refresh_token', $newTokens)) {
$this->logger->debug('Refresh token updated');
$newTokens['expires_at'] = time() + $newTokens['expires_in'] - 30;
$this->logger->trace('Update tokens: '.json_encode($newTokens));
file_put_contents($this::TOKENS_FILE, json_encode($newTokens));
}
$this->logger->debug('Token received');
$this->logger->trace('Token: '.json_encode($tokens));
// Store tokens
$tokens['expires_at'] = time() + $tokens['expires_in'] - 30;
$this->logger->debug('Access token expires at ' . $tokens['expires_at']);
file_put_contents($this::TOKENS_FILE, json_encode($tokens));
$this->logger->debug('Using existing tokens');
}
return true;
}
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ Netatmo collector is a PHP script for requesting measures from Netatmo devices.

Download this project and extract files to directory of your choice.

Configure a Netatmo [application](https://dev.netatmo.com/myaccount/createanapp) and user account informations in `config.php`.
In order to authenticate yourself on the Netatmo API, you must:

- create a Netatmo [application](https://dev.netatmo.com/myaccount/createanapp),
- generate a token with `read_station` scope,
- enter your `client_id` and `client_secret` values in `config.php`,
- create a `tokens.json` file with this content (replace the 2 values between `<>` with your tokens):

``` json
{"access_token":"<yourAccessToken>","refresh_token":"<yourRefreshToken >","scope":["read_station"],"expires_in":10800,"expire_in":10800,"expires_at":0}
```

### Dependencies

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "nioc/netatmo-collector",
"description": "Netatmo collector is a script for requesting measures from Netatmo devices",
"homepage": "https://github.com/nioc/netatmo-collector",
"version": "0.2.0",
"version": "0.4.0",
"license": "AGPL-3.0-only",
"require": {
"influxdb/influxdb-php": "^1.14",
Expand Down
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ version: '3.2'

services:
php:
image: php:alpine
image: php:7.4-alpine
working_dir: /usr/src/app
command: php index.php 2020-11-19
command: php index.php 2023-12-05
depends_on:
- influxdb
volumes:
Expand All @@ -17,7 +17,7 @@ services:
- .:/app

influxdb:
image: influxdb:alpine
image: influxdb:1.8-alpine
restart: unless-stopped
ports:
- "8086:8086"
Expand Down
17 changes: 2 additions & 15 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,8 @@

// Authentication with Netatmo server (OAuth2)
if (!$netatmo->getExistingTokens()) {
$logger->warn('No existing token found, try to get it with username and password');
// No existing token found, connect with username and password read from stdin
function prompt($question)
{
echo "\r\n$question: ";
$stdin = fopen('php://stdin', 'r');
$response = fgets($stdin);
fclose($stdin);
return trim($response);
}
$username = prompt('Your netatmo account username');
$password = prompt('Your netatmo account password');
if (!$netatmo->getToken($username, $password)) {
exit(1);
}
$logger->error('No existing token found, go to https://dev.netatmo.com/apps to get a token');
exit(1);
}

// Retrieve user's Weather Stations Information
Expand Down

0 comments on commit b4687e8

Please sign in to comment.