Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track token expiration #149

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions controllers/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@
$user->token_endpoint = $tokenEndpoint;
$user->micropub_endpoint = $micropubEndpoint;
$user->micropub_access_token = $token['response']['access_token'];
if(is_numeric($token['response']['expires_in'])) {
$expiration = time() + $token['response']['expires_in'];
$user->micropub_token_expiration = date('Y-m-d H:i:s', $expiration);
}
$user->micropub_scope = $token['response']['scope'];
$user->micropub_response = $token['raw_response'];
$user->save();
Expand Down Expand Up @@ -236,6 +240,7 @@
$user->micropub_media_endpoint = '';
$user->micropub_scope = '';
$user->micropub_access_token = '';
$user->micropub_token_expiration = '';
$user->syndication_targets = '';
$user->supported_post_types = '';
$user->save();
Expand Down
12 changes: 11 additions & 1 deletion controllers/controllers.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,17 @@ function require_login(&$app, $redirect=true) {
$app->redirect('/', 302);
return false;
} else {
return ORM::for_table('users')->find_one($_SESSION['user_id']);
$user = ORM::for_table('users')->find_one($_SESSION['user_id']);
if(isset($user->micropub_token_expiration)) {
$now = new DateTime();
$expiration = new DateTime($user->micropub_token_expiration);
if($now > $expiration) {
header('X-Error: TokenExpired');
$app->redirect('/auth/start?'.http_build_query(array('me' => $user->url)), 302);
return false;
}
}
return $user;
}
}

Expand Down
2 changes: 2 additions & 0 deletions schema/migrations/0013.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE users
ADD COLUMN `micropub_token_expiration` datetime DEFAULT NULL;
1 change: 1 addition & 0 deletions schema/mysql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CREATE TABLE `users` (
`micropub_endpoint` varchar(255) DEFAULT NULL,
`micropub_media_endpoint` varchar(255) DEFAULT NULL,
`micropub_access_token` text,
`micropub_token_expiration` datetime DEFAULT NULL,
`micropub_scope` varchar(255) DEFAULT NULL,
`micropub_response` text,
`micropub_slug_field` varchar(255) NOT NULL DEFAULT 'mp-slug',
Expand Down
1 change: 1 addition & 0 deletions schema/sqlite.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CREATE TABLE users (
micropub_endpoint TEXT,
micropub_media_endpoint TEXT,
micropub_access_token TEXT,
micropub_token_expiration datetime,
micropub_scope TEXT,
micropub_response TEXT,
micropub_slug_field TEXT default 'mp-slug',
Expand Down