From 70ea9bff97b01f746b27a37bcbb1a4181228c47c Mon Sep 17 00:00:00 2001 From: afornerot Date: Thu, 15 Feb 2024 22:36:10 +0100 Subject: [PATCH] refresh token --- src/Controller/ScrumController.php | 2 ++ src/Service/giteaService.php | 50 ++++++++++++++++++++++++++++-- templates/Scrum/view.html.twig | 2 +- 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/Controller/ScrumController.php b/src/Controller/ScrumController.php index 350449b..1c561eb 100755 --- a/src/Controller/ScrumController.php +++ b/src/Controller/ScrumController.php @@ -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=[]; diff --git a/src/Service/giteaService.php b/src/Service/giteaService.php index 91c4944..d58832b 100644 --- a/src/Service/giteaService.php +++ b/src/Service/giteaService.php @@ -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); diff --git a/templates/Scrum/view.html.twig b/templates/Scrum/view.html.twig index afb567a..2a58893 100644 --- a/templates/Scrum/view.html.twig +++ b/templates/Scrum/view.html.twig @@ -856,7 +856,7 @@ } } }); - }, 6000); + }, 60000); }); {% endblock %} \ No newline at end of file