// ĠCopyright Ġ 2 0 1 9 ĠAmazon . com , ĠInc . Ġor Ġits Ġaffiliates . ĠAll ĠRights ĠReserved . Ċ // ĠSPDX - License - Identifier : ĠApache - 2 . 0 Ċ Ċ package Ġsoftware . aws . tool k its . jetbrains . services . telemetry Ċ Ċ import Ġsoftware . amazon . aw ssdk . auth . credentials . Aws Credentials Ċ import Ġsoftware . amazon . aw ssdk . auth . credentials . Aws Credentials Provider Ċ import Ġsoftware . amazon . aw ssdk . auth . credentials . Aws Session Credentials Ċ import Ġsoftware . amazon . aw ssdk . services . cognit oid entity . C ognito Identity Client Ċ import Ġsoftware . amazon . aw ssdk . services . cognit oid entity . model . Credentials Ċ import Ġsoftware . amazon . aw ssdk . services . cognit oid entity . model . Get Credentials For Identity Request Ċ import Ġsoftware . amazon . aw ssdk . services . cognit oid entity . model . GetId Request Ċ import Ġsoftware . amazon . aw ssdk . utils . Sdk Auto Closeable Ċ import Ġsoftware . amazon . aw ssdk . utils . cache . Cached Supplier Ċ import Ġsoftware . amazon . aw ssdk . utils . cache . Non Blocking Ċ import Ġsoftware . amazon . aw ssdk . utils . cache . Refresh Result Ċ import Ġsoftware . aws . tool k its . core . telemetry . Cached Identity Storage Ċ import Ġjava . time . temporal . Ch rono Unit Ċ Ċ /** Ċ Ġ* Ġ[ Aws Credentials Provider ] Ġimplementation Ġthat Ġuses Ġthe ĠAmazon ĠC ognito ĠIdentity Ċ Ġ* Ġservice Ġto Ġcreate Ġtemporary , Ġshort - li ved Ġsessions Ġto Ġuse Ġfor Ġauthentication Ċ Ġ* Ċ Ġ* Ġ@ constructor ĠCreates Ġa Ġnew ĠAws Credentials Provider Ġthat Ġuses Ġcredentials Ġfrom Ġa ĠC ognito ĠIdentity Ġpool . Ċ Ġ* Ġ@ property Ġidentity Pool ĠThe Ġname Ġof Ġthe Ġpool Ġto Ġcreate Ġusers Ġfrom Ċ Ġ* Ġ@ param Ġcache Storage ĠA Ġstorage Ġsolution Ġto Ġcache Ġan Ġidentity ĠID , Ġdisabled Ġif Ġnull Ċ Ġ*/ Ċ class ĠAws C ognito Credentials Provider ( ĊĠĠĠ Ġprivate Ġval Ġidentity Pool : ĠString , ĊĠĠĠ Ġprivate Ġval Ġc ognito Client : ĠC ognito Identity Client , ĊĠĠĠ Ġcache Storage : ĠCached Identity Storage ? Ġ= Ġnull Ċ ) Ġ: ĠAws Credentials Provider , ĠS dk Auto Closeable Ġ{ ĊĠĠĠ Ġprivate Ġval Ġidentity Id Provider Ġ= ĠAws C ognito Identity Provider ( cognito Client , Ġidentity Pool , Ġcache Storage ) ĊĠĠĠ Ġprivate Ġval Ġcache Supplier Ġ= ĠCached Supplier . builder ( this :: update C ognito Credentials ) ĊĠĠĠĠĠĠĠ Ġ. prefetch Strategy ( Non Blocking (" C ognito ĠIdentity ĠCredential ĠRefresh ")) ĊĠĠĠĠĠĠĠ Ġ. build () ĊĊĠĠĠ Ġoverride Ġfun Ġresolve Credentials (): ĠAws Credentials Ġ= Ġcache Supplier . get () ĊĊĠĠĠ Ġprivate Ġfun Ġupdate C ognito Credentials (): ĠRefresh Result < Aws Session Credentials > Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġcredentials For Identity Ġ= Ġcredentials For Identity () ĊĠĠĠĠĠĠĠ Ġval Ġsession Credentials Ġ= ĠAws Session Credentials . create ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcredentials For Identity . access KeyId (), ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcredentials For Identity . secret Key (), ĊĠĠĠĠĠĠĠĠĠĠĠ Ġcredentials For Identity . session Token () ĊĠĠĠĠĠĠĠ Ġ) ĊĠĠĠĠĠĠĠ Ġval Ġactual Expiration Ġ= Ġcredentials For Identity . expiration () ĊĊĠĠĠĠĠĠĠ Ġreturn ĠRefresh Result . builder ( session Credentials ) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. st ale Time ( actual Expiration . minus ( 1 , ĠCh rono Unit . MIN UTES )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. prefetch Time ( actual Expiration . minus ( 5 , ĠCh rono Unit . MIN UTES )) ĊĠĠĠĠĠĠĠĠĠĠĠ Ġ. build () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġcredentials For Identity (): ĠCredentials Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġidentity Id Ġ= Ġidentity Id Provider . identity Id ĊĠĠĠĠĠĠĠ Ġval Ġrequest Ġ= ĠGet Credentials For Identity Request . builder (). identity Id ( identity Id ). build () ĊĊĠĠĠĠĠĠĠ Ġreturn Ġc ognito Client . get Credentials For Identity ( request ). credentials () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġoverride Ġfun Ġclose () Ġ{ ĊĠĠĠĠĠĠĠ Ġc ognito Client . close () ĊĠĠĠĠĠĠĠ Ġcache Supplier . close () ĊĠĠĠ Ġ} Ċ } Ċ Ċ private Ġclass ĠAws C ognito Identity Provider ( ĊĠĠĠ Ġprivate Ġval Ġc ognito Client : ĠC ognito Identity Client , ĊĠĠĠ Ġprivate Ġval Ġidentity Pool Id : ĠString , ĊĠĠĠ Ġprivate Ġval Ġcache Storage : ĠCached Identity Storage ? Ġ= Ġnull Ċ ) Ġ{ ĊĠĠĠ Ġval Ġidentity Id : ĠString Ġby Ġlazy Ġ{ ĊĠĠĠĠĠĠĠ Ġload From Cache () Ġ?: Ġcreate New Identity () ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġload From Cache (): ĠString ? Ġ= Ġcache Storage ?. load Identity ( identity Pool Id ) ĊĊĠĠĠ Ġprivate Ġfun Ġcreate New Identity (): ĠString Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġrequest Ġ= ĠGet Id Request . builder (). identity Pool Id ( identity Pool Id ). build () ĊĠĠĠĠĠĠĠ Ġval Ġnew Identity Id Ġ= Ġc ognito Client . getId ( request ). identity Id () ĊĊĠĠĠĠĠĠĠ Ġcache Storage ?. store Identity ( identity Pool Id , Ġnew Identity Id ) ĊĊĠĠĠĠĠĠĠ Ġreturn Ġnew Identity Id ĊĠĠĠ Ġ} Ċ } Ċ