package Ġcom . j eno . react spring app . security . oauth 2 Ċ Ċ import Ġcom . j eno . react spring app . data . model . Auth Provider Type Ċ import Ġcom . j eno . react spring app . data . model . Picture Ċ import Ġcom . j eno . react spring app . data . model . User Ċ import Ġcom . j eno . react spring app . data . repository . Picture Repository Ċ import Ġcom . j eno . react spring app . data . repository . UserRepository Ċ import Ġcom . j eno . react spring app . error . exception . OAuth 2 Authentication Processing Exception Ċ import Ġcom . j eno . react spring app . security . User Principal Ċ import Ġcom . j eno . react spring app . security . oauth 2 . user . OAuth 2 UserInfo Ċ import Ġorg . springframework . security . authentication . Internal Authentication ServiceException Ċ import Ġorg . springframework . security . core . Authentication Exception Ċ import Ġorg . springframework . security . oauth 2 . client . userinfo . Default OAuth 2 UserService Ċ import Ġorg . springframework . security . oauth 2 . client . userinfo . OAuth 2 UserRequest Ċ import Ġorg . springframework . security . oauth 2 . core . user . OAuth 2 User Ċ import Ġorg . springframework . stereotype . Component Ċ import Ġorg . springframework . util . StringUtils Ċ import Ġjava . util . Objects Ċ Ċ @ Component Ċ class ĠOAuth 2 UserService ( Ċĉ ĉ val Ġuser Repository : ĠUser Repository , Ċĉ ĉ val Ġpicture Repository : ĠPicture Repository ): ĠDefault OAuth 2 UserService () Ġ{ ĊĊ ĉ override Ġfun Ġload User ( user Request : ĠOAuth 2 UserRequest ?): ĠOAuth 2 User Ġ{ Ċĉ ĉ val Ġoauth 2 User Ġ= Ġsuper . load User ( user Request ) Ċĉ ĉ return Ġtry Ġ{ Ċĉĉ ĉ process OAuth 2 User ( user Request , Ġoauth 2 User ) Ċĉ ĉ } Ġcatch Ġ( ex : ĠAuthentication Exception ) Ġ{ Ċĉĉ ĉ throw Ġex Ċĉ ĉ } Ġcatch Ġ( ex : ĠException ) Ġ{ Ċĉĉ ĉ // ĠThrow ing Ġan Ġinstance Ġof ĠAuthentication Exception Ġwill Ġtrigger Ġthe ĠOAuth 2 Authentication Failure Handler Ċĉĉ ĉ throw ĠInternal Authentication ServiceException ( ex . message , Ġex . cause ) Ċĉ ĉ } Ċ ĉ } ĊĊ ĉ private Ġfun Ġprocess OAuth 2 User ( o Auth 2 UserRequest : ĠOAuth 2 UserRequest ?, Ġo Auth 2 User : ĠOAuth 2 User ): ĠOAuth 2 User Ġ{ Ċĉ ĉ if Ġ( o Auth 2 UserRequest Ġ== Ġnull ) Ġ{ Ċĉĉ ĉ throw ĠOAuth 2 Authentication Processing Exception (" OAuth 2 Ġrequest Ġis Ġnull ") Ċĉ ĉ } Ċĉ ĉ val Ġo Auth 2 UserInfo : ĠOAuth 2 UserInfo Ġ= ĠOAuth 2 UserInfo . get OAuth 2 UserInfo ( o Auth 2 UserRequest . client Registration . registration Id , Ġo Auth 2 User . attributes ) Ċĉ ĉ if Ġ(! StringUtils . has Text ( o Auth 2 UserInfo . getEmail ()) Ġ|| Ġ! StringUtils . has Text ( o Auth 2 UserInfo . getId ())) Ġ{ Ċĉĉ ĉ throw ĠOAuth 2 Authentication Processing Exception (" Ins ufficient Ġinfo Ġprovided Ġfrom ĠOAuth 2 Ġprovider Ġ${ o Auth 2 UserRequest . client Registration . registration Id }") Ċĉ ĉ } Ċĉ ĉ val Ġuser Ġ= Ġuser Repository . findBy Provider Id ( o Auth 2 UserInfo . getId () !! ) Ċĉĉĉ ĉ ?. let Ġ{ Ġuser Ġ-> Ġupdate Existing User ( user , Ġo Auth 2 UserInfo ) Ġ} Ċĉĉĉ ĉ ?: Ġregister New User ( o Auth 2 UserRequest , Ġo Auth 2 UserInfo ) Ċĉ ĉ return ĠUser Principal . create ( user , Ġo Auth 2 User . attributes ) Ċ ĉ } ĊĊ ĉ private Ġfun Ġregister New User ( o Auth 2 UserRequest : ĠOAuth 2 UserRequest , Ġo Auth 2 UserInfo : ĠOAuth 2 UserInfo ): ĠUser Ġ{ Ċĉ ĉ val Ġpicture Ġ= ĠPicture ( o Auth 2 UserInfo . getId () Ġ+ Ġ"_ profile _ picture ", Ġurl Ġ= Ġo Auth 2 UserInfo . getImage Url ()) Ċĉ ĉ val Ġuser Ġ= ĠUser ( Ċĉĉĉ ĉ o Auth 2 UserInfo . getName () Ġ?: Ġ"", Ċĉĉĉ ĉ o Auth 2 UserInfo . getEmail () !! , Ċĉĉĉ ĉ provider Ġ= ĠAuth Provider Type . valueOf ( o Auth 2 UserRequest . client Registration . registration Id . toUpperCase ()), Ċĉĉĉ ĉ provider Id Ġ= Ġo Auth 2 UserInfo . getId (), Ċĉĉĉ ĉ profile Picture Ġ= Ġpicture Repository . save ( picture ) Ċĉ ĉ ) Ċĉ ĉ return Ġuser Repository . save ( user ) Ċ ĉ } ĊĊ ĉ private Ġfun Ġupdate Existing User ( existing User : ĠUser , Ġo Auth 2 UserInfo : ĠOAuth 2 UserInfo ): ĠUser Ġ{ Ċĉ ĉ val Ġname Ġ= Ġo Auth 2 UserInfo . getName () Ġ?: Ġ"" Ċĉ ĉ if Ġ(! Objects . equals ( existing User . name , Ġname )) Ġ{ Ċĉĉ ĉ existing User . name Ġ= Ġname Ċĉ ĉ } Ċĉ ĉ if Ġ( existing User . profile Picture Ġ!= Ġnull Ġ&& Ġ! Objects . equals ( existing User . profile Picture !!. url , Ġo Auth 2 UserInfo . getImage Url ())) Ġ{ Ċĉĉ ĉ existing User . profile Picture !!. url Ġ= Ġo Auth 2 UserInfo . getImage Url () Ċĉĉ ĉ picture Repository . save ( existing User . profile Picture !! ) Ċĉ ĉ } Ċĉ ĉ return Ġuser Repository . save ( existing User ) Ċ ĉ } Ċ Ċ }