From 9cc257f7ef8a2c66c91e71af2e178754c1305dc9 Mon Sep 17 00:00:00 2001
From: qkqpttgf <45693631+qkqpttgf@users.noreply.github.com>
Date: Sun, 2 Feb 2020 20:02:12 +0800
Subject: [PATCH] refresh token expires in 90 day
---
function/common.php | 33 +++++++++++++++++----------------
function/heroku.php | 2 +-
function/normal.php | 2 +-
function/scf.php | 3 ++-
theme/classic.php | 45 ++++++++++++++++++++-------------------------
5 files changed, 41 insertions(+), 44 deletions(-)
diff --git a/function/common.php b/function/common.php
index 4e22b69..bfc27fe 100644
--- a/function/common.php
+++ b/function/common.php
@@ -17,9 +17,6 @@ function savecache($key, $value, $exp = 3300)
function getconstStr($str)
{
global $constStr;
- $constStr['language'] = $_COOKIE['language'];
- if ($constStr['language']=='') $constStr['language'] = getConfig('language');
- if ($constStr['language']=='') $constStr['language'] = 'en-us';
if ($constStr[$str][$constStr['language']]!='') return $constStr[$str][$constStr['language']];
return $constStr[$str]['en-us'];
}
@@ -353,7 +350,11 @@ function bigfileupload($path)
function main($path)
{
global $exts;
+ global $constStr;
config_oauth();
+ $constStr['language'] = $_COOKIE['language'];
+ if ($constStr['language']=='') $constStr['language'] = getConfig('language');
+ if ($constStr['language']=='') $constStr['language'] = 'en-us';
$_SERVER['list_path'] = getListpath($_SERVER['HTTP_HOST']);
if ($_SERVER['list_path']=='') $_SERVER['list_path'] = '/';
$_SERVER['is_guestup_path'] = is_guestup_path($path);
@@ -364,6 +365,19 @@ function main($path)
$refresh_token = getConfig('refresh_token');
if (!$refresh_token) return get_refresh_token();
+ if (!($_SERVER['access_token'] = getcache('access_token'))) {
+ $response = curl_request( $_SERVER['oauth_url'] . 'token', 'client_id='. $_SERVER['client_id'] .'&client_secret='. $_SERVER['client_secret'] .'&grant_type=refresh_token&requested_token_use=on_behalf_of&refresh_token=' . $refresh_token );
+ if ($response['stat']==200) $ret = json_decode($response['body'], true);
+ if (!isset($ret['access_token'])) {
+ error_log('failed to get access_token. response' . json_encode($ret));
+ throw new Exception($response['stat'].'failed to get access_token.'.$response['body']);
+ }
+ error_log('Get access token:'.json_encode($ret, JSON_PRETTY_PRINT));
+ $_SERVER['access_token'] = $ret['access_token'];
+ savecache('access_token', $_SERVER['access_token']);
+ if (time()>getConfig('token_expires')) setConfig([ 'refresh_token' => $ret['refresh_token'], 'token_expires' => time()+30*24*60*60 ]);
+ }
+
if (getConfig('adminloginpage')=='') {
$adminloginpage = 'admin';
} else {
@@ -400,19 +414,6 @@ function main($path)
}
$_SERVER['retry'] = 0;
- if (!($_SERVER['access_token'] = getcache('access_token'))) {
- $ret = json_decode(curl_request(
- $_SERVER['oauth_url'] . 'token',
- 'client_id='. $_SERVER['client_id'] .'&client_secret='. $_SERVER['client_secret'] .'&grant_type=refresh_token&requested_token_use=on_behalf_of&refresh_token=' . $refresh_token
- )['body'], true);
- if (!isset($ret['access_token'])) {
- error_log('failed to get access_token. response' . json_encode($ret));
- throw new Exception('failed to get access_token.');
- }
- $_SERVER['access_token'] = $ret['access_token'];
- savecache('access_token', $_SERVER['access_token']);
- }
-
if ($_SERVER['ajax']) {
if ($_GET['action']=='del_upload_cache'&&substr($_GET['filename'],-4)=='.tmp') {
// del '.tmp' without login. 无需登录即可删除.tmp后缀文件
diff --git a/function/heroku.php b/function/heroku.php
index 552f6de..449204e 100644
--- a/function/heroku.php
+++ b/function/heroku.php
@@ -66,7 +66,7 @@ function get_refresh_token()
}
document.cookie=\'language=; path=/\';
';
- setConfig([ 'refresh_token' => $tmptoken ]);
+ setConfig([ 'refresh_token' => $tmptoken, 'token_expires' => time()+30*24*60*60 ]);
savecache('access_token', $ret['access_token'], $ret['expires_in'] - 60);
$str .= '
';
diff --git a/function/normal.php b/function/normal.php
index 42a3a83..cc57894 100644
--- a/function/normal.php
+++ b/function/normal.php
@@ -65,7 +65,7 @@ function get_refresh_token()
}
document.cookie=\'language=; path=/\';
';
- setConfig([ 'refresh_token' => $tmptoken ]);
+ setConfig([ 'refresh_token' => $tmptoken, 'token_expires' => time()+30*24*60*60 ]);
savecache('access_token', $ret['access_token'], $ret['expires_in'] - 60);
$str .= '
';
diff --git a/function/scf.php b/function/scf.php
index 3d9d09c..3d8cc51 100644
--- a/function/scf.php
+++ b/function/scf.php
@@ -23,6 +23,7 @@ function GetGlobalVariable($event)
$pos = strpos($cookievalues,"=");
$_COOKIE[urldecode(substr($cookievalues,0,$pos))]=urldecode(substr($cookievalues,$pos+1));
}
+ $_SERVER['USER'] = 'qcloud';
}
function GetPathSetting($event, $context)
@@ -100,7 +101,7 @@ function get_refresh_token()
}
document.cookie=\'language=; path=/\';
';
- setConfig([ 'refresh_token' => $tmptoken ]);
+ setConfig([ 'refresh_token' => $tmptoken, 'token_expires' => time()+30*24*60*60 ]);
savecache('access_token', $ret['access_token'], $ret['expires_in'] - 60);
$trynum = 0;
while( json_decode(getfunctioninfo($_SERVER['function_name'], $_SERVER['Region'], $_SERVER['namespace'], getConfig('SecretId'), getConfig('SecretKey')),true)['Response']['Status']!='Active' ) echo '
diff --git a/theme/classic.php b/theme/classic.php
index b822682..0440827 100644
--- a/theme/classic.php
+++ b/theme/classic.php
@@ -55,17 +55,17 @@
-
+