package Ġcom . mir on 4 dev . sandbox . jwt . web Ċ Ċ import Ġcom . github . ben man es . caffe ine . cache . C affe ine Ċ import Ġcom . mir on 4 dev . sandbox . jwt . config . Facebook Config Ċ import Ġcom . mir on 4 dev . sandbox . jwt . config . Security Config . Companion . JWT _ TOKEN _ COOKIE Ċ import Ġcom . mir on 4 dev . sandbox . jwt . security . Facebook Service Ċ import Ġorg . springframework . security . authentication . Authentication ServiceException Ċ import Ġorg . springframework . stereotype . Controller Ċ import Ġorg . springframework . web . bind . annotation . GetMapping Ċ import Ġorg . springframework . web . bind . annotation . RequestMapping Ċ import Ġorg . springframework . web . servlet . support . Servlet Uri Components Builder Ċ import Ġorg . springframework . web . util . Uri Components Builder Ċ import Ġjava . util .* Ċ import Ġjava . util . concurrent . TimeUnit Ċ import Ġjavax . servlet . http . Cookie Ċ import Ġjavax . servlet . http . HttpServletRequest Ċ import Ġjavax . servlet . http . HttpServletResponse Ċ Ċ @ Controller Ċ @ RequestMapping ("/ facebook ") Ċ class ĠFacebook Controller ( private Ġval Ġfacebook Config : ĠFacebook Config , Ċ ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġprivate Ġval Ġfacebook Service : ĠFacebook Service ) Ġ{ ĊĊĠĠĠ Ġprivate Ġval Ġvalid State Ġ= ĠC affe ine . newBuilder (). expire After Write ( 1 , ĠTimeUnit . MIN UTES ). build < String , ĠBoolean >() ĊĊĠĠĠ Ġ@ GetMapping ĊĠĠĠ Ġfun Ġlogin ( response : ĠHttpServletResponse ) Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġuuid Ġ= ĠUUID . randomUUID (). toString () ĊĠĠĠĠĠĠĠ Ġval Ġstate Ġ= ĠBase 6 4 . get Encoder (). encode ToString ( uuid . toByteArray ()) ĊĠĠĠĠĠĠĠ Ġvalid State . put ( uuid , Ġtrue ) ĊĊĠĠĠĠĠĠĠ Ġval Ġauthorization Uri Ġ= ĠUri Components Builder . from Uri String ( facebook Config . user Authorization Uri ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. query Param (" client _ id ", Ġfacebook Config . clientId ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. query Param (" redirect _ uri ", ĠServlet Uri Components Builder . from Current Request Uri (). path ("/ callback "). to Uri String ()) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. query Param (" state ", Ġstate ) ĊĠĠĠĠĠĠĠĠĠĠĠĠĠĠĠ Ġ. to Uri String () ĊĊĠĠĠĠĠĠĠ Ġresponse . send Redirect ( authorization Uri ) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġ@ GetMapping ("/ callback ") ĊĠĠĠ Ġfun Ġlogin Callback ( request : ĠHttpServletRequest , Ġresponse : ĠHttpServletResponse ) Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġcode Ġ= Ġrequest . getParameter (" code ") ĊĠĠĠĠĠĠĠ Ġval Ġstate Ġ= ĠString ( Base 6 4 . get Decoder (). decode ( request . getParameter (" state "))) ĊĊĠĠĠĠĠĠĠ Ġif Ġ( valid State . get If Present ( state ) Ġ== Ġnull ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġthrow ĠAuthentication ServiceException (" Provided Ġstate Ġis Ġincorrect Ġor Ġexpired ") ĊĠĠĠĠĠĠĠ Ġ} ĊĊĠĠĠĠĠĠĠ Ġval Ġredirect Url Ġ= ĠServlet Uri Components Builder . from Current Request Uri (). to Uri String () ĊĠĠĠĠĠĠĠ Ġval Ġjwt Token Ġ= Ġfacebook Service . generate Jwt Token ( code , Ġredirect Url ) ĊĊĠĠĠĠĠĠĠ Ġval Ġcookie Ġ= ĠCookie ( JWT _ TOKEN _ COOKIE , Ġjwt Token ) ĊĠĠĠĠĠĠĠ Ġcookie . path Ġ= Ġ"/" ĊĠĠĠĠĠĠĠ Ġresponse . add Cookie ( cookie ) ĊĠĠĠĠĠĠĠ Ġresponse . send Redirect ("/") ĊĠĠĠ Ġ} Ċ }