refresh token

This commit is contained in:
afornerot 2024-02-15 22:36:10 +01:00
parent 9205735d1f
commit 70ea9bff97
3 changed files with 51 additions and 3 deletions

View File

@ -29,6 +29,8 @@ class ScrumController extends AbstractController
$em = $this->getDoctrine()->getManager();
$scrums = $em->getRepository($this->entity)->findByUser($this->getUser());
$this->giteaservice->needrefresh();
$giteacategorys=[];
$gitearepos=[];

View File

@ -33,14 +33,56 @@ class giteaService
$response=$this->api("POST",$apiurl,$body);
if(!$response||$response->code!="200") return false;
else {
$this->session->set('giteatoken', $response->body->access_token);
$this->session->set('gitearefreshtoken', $response->body->refresh_token);
$date = new \DateTime();
$date->modify('+'.$response->body->expires_in.' seconds');
$this->session->set('gitearefreshdate',$date);
return $response->body->access_token;
}
}
public function refreshtoken() {
$apiurl = $this->params->get("oauthTokenurl");
$query= [
"client_id" => $this->params->get("oauthClientid"),
"client_secret" => $this->params->get("oauthClientsecret"),
"code" => $this->session->get("giteacode"),
"grant_type" => "refresh_token",
"refresh_token" => $this->session->get('gitearefreshtoken'),
];
$body = \Unirest\Request\Body::json($query);
$response=$this->api("POST",$apiurl,$body);
if(!$response||$response->code!="200") return false;
else {
$this->session->set('giteatoken', $response->body->access_token);
$this->session->set('gitearefreshtoken', $response->body->refresh_token);
$date = new \DateTime();
$date->modify('+'.$response->body->expires_in.' seconds');
$this->session->set('gitearefreshdate',$date);
return $response->body->access_token;
}
}
public function needrefresh() {
$date = new \DateTime();
// On refresh 15 minutes avant le terme
$refreshdate=clone $this->session->get('gitearefreshdate');
$refreshdate->modify('-900 seconds');
if($date>$refreshdate) return $this->refreshtoken();
else return $this->session->get('giteatoken');
}
public function deletetoken($username) {
$apiurl=$this->url."/users/".$username."/tokens/".$this->session->get("giteatoken");
$response=$this->api("DELETE",$apiurl,null,$this->session->get("giteatoken"));
@ -289,7 +331,11 @@ class giteaService
'Accept' => 'application/json',
'Content-Type' => 'application/json',
];
if($token) $headers["Authorization"]="token ".$token;
if($token) {
$token=$this->needrefresh();
$headers["Authorization"]="token ".$token;
}
// Paramétrage unirest
\Unirest\Request::verifyPeer(false);

View File

@ -856,7 +856,7 @@
}
}
});
}, 6000);
}, 60000);
});
{% endblock %}