// ĠCopyright Ġ 2 0 0 0 - 2 0 2 0 ĠJetBrains Ġs . r . o . ĠUse Ġof Ġthis Ġsource Ġcode Ġis Ġgoverned Ġby Ġthe ĠApache Ġ 2 . 0 Ġlicense Ġthat Ġcan Ġbe Ġfound Ġin Ġthe ĠLICENSE Ġfile . Ċ package Ġorg . jetbrains . plugins . github . authentication . accounts Ċ Ċ import Ġcom . intellij . credential Store . Credential Attributes Ċ import Ġcom . intellij . credential Store . Credentials Ċ import Ġcom . intellij . credential Store . Password Safe Settings Listener Ċ import Ġcom . intellij . credential Store . generate ServiceName Ċ import Ġcom . intellij . ide . password Safe . Password Safe Ċ import Ġcom . intellij . openapi . application . Application Manager Ċ import Ġcom . intellij . openapi . components . Persistent State Component Ċ import Ġcom . intellij . openapi . components . State Ċ import Ġcom . intellij . openapi . components . Storage Ċ import Ġcom . intellij . openapi . components . service Ċ import Ġcom . intellij . openapi . diagnostic . logger Ċ import Ġcom . intellij . util . messages . Topic Ċ import Ġorg . jetbrains . plugins . github . api . Github Server Path Ċ import Ġorg . jetbrains . plugins . github . util . Github Util Ċ import Ġkotlin . properties . Deleg ates . observable Ċ Ċ /** Ċ Ġ* ĠHandles Ġapplication - level ĠGithub Ġaccounts Ċ Ġ*/ Ċ @ State ( name Ġ= Ġ" Github Accounts ", Ġstorage s Ġ= Ġ[ ĊĠ ĠStorage ( value Ġ= Ġ" github . xml "), ĊĠ ĠStorage ( value Ġ= Ġ" github _ settings . xml ", Ġdeprecated Ġ= Ġtrue ) Ċ ]) Ċ internal Ġclass ĠGithub Account Manager Ġ: ĠPersistent State Component < Array < Github Account >> Ġ{ ĊĠ Ġvar Ġaccounts : ĠSet < Github Account > Ġby Ġobservable ( set Of ()) Ġ{ Ġ_, ĠoldValue , ĠnewValue Ġ-> ĊĠĠĠ ĠoldValue . filter Ġ{ Ġit Ġ! in ĠnewValue Ġ}. forEach ( this :: account Removed ) ĊĠĠĠ ĠLOG . debug (" Account Ġlist Ġchanged Ġto : Ġ$ newValue ") ĊĠ Ġ} ĊĊĠ Ġprivate Ġfun Ġaccount Removed ( account : ĠGithub Account ) Ġ{ ĊĠĠĠ Ġupdate Account Token ( account , Ġnull ) ĊĠĠĠ ĠApplication Manager . getApplication () ĊĠĠĠĠĠ Ġ. message Bus ĊĠĠĠĠĠ Ġ. sync Publisher ( ACCOUNT _ RE MOVED _ TOPIC ). account Removed ( account ) ĊĠ Ġ} ĊĊĠ Ġ/** ĊĠĠ Ġ* ĠAdd / update / remove ĠGithub ĠOAuth Ġtoken Ġfrom Ġapplication ĊĠĠ Ġ*/ ĊĠ Ġfun Ġupdate Account Token ( account : ĠGithub Account , Ġtoken : ĠString ?) Ġ{ ĊĠĠĠ ĠPassword Safe . instance . set ( create Credential Attributes ( account . id ), Ġtoken ?. let Ġ{ Ġcreate Credentials ( account . id , Ġit ) Ġ}) ĊĠĠĠ ĠLOG . debug (( if Ġ( token Ġ== Ġnull ) Ġ" Cle ared " Ġelse Ġ" Updated ") Ġ+ Ġ" ĠOAuth Ġtoken Ġfor Ġaccount : Ġ$ account ") ĊĠĠĠ ĠApplication Manager . getApplication () ĊĠĠĠĠĠ Ġ. message Bus ĊĠĠĠĠĠ Ġ. sync Publisher ( ACCOUNT _ TOKEN _ CHANGED _ TOPIC ). token Changed ( account ) ĊĠ Ġ} ĊĊĠ Ġ/** ĊĠĠ Ġ* ĠRetrieve ĠOAuth Ġtoken Ġfor Ġaccount Ġfrom Ġpassword Ġsafe ĊĠĠ Ġ*/ ĊĠ Ġfun ĠgetToken For Account ( account : ĠGithub Account ): ĠString ? Ġ= ĠPassword Safe . instance . get ( create Credential Attributes ( account . id )) ?. getPassword AsString () ĊĊĠ Ġoverride Ġfun ĠgetState () Ġ= Ġaccounts . to Typed Array () ĊĊĠ Ġoverride Ġfun Ġload State ( state : ĠArray < Github Account >) Ġ{ ĊĠĠĠ Ġaccounts Ġ= Ġstate . to HashSet () ĊĠ Ġ} ĊĊĠ Ġcompanion Ġobject Ġ{ ĊĠĠĠ Ġprivate Ġval ĠLOG Ġ= Ġlogger < Github Account Manager >() ĊĊĠĠĠ Ġ@ Jvm Static ĊĠĠĠ Ġval ĠAC COUNT _ RE MOVED _ TOPIC Ġ= ĠTopic (" GITHUB _ ACCOUNT _ RE MOVED ", ĠAccount Removed Listener :: class . java ) ĊĊĠĠĠ Ġ@ Jvm Static ĊĠĠĠ Ġval ĠAC COUNT _ TOKEN _ CHANGED _ TOPIC Ġ= ĠTopic (" GITHUB _ ACCOUNT _ TOKEN _ CHANGED ", ĠAccount Token Changed Listener :: class . java ) ĊĊĠĠĠ Ġfun Ġcreate Account ( name : ĠString , Ġserver : ĠGithub Server Path ) Ġ= ĠGithub Account ( name , Ġserver ) ĊĠ Ġ} ĊĊĠ Ġclass ĠPassword Storage Cle ared Listener Ġ: ĠPassword Safe Settings Listener Ġ{ ĊĠĠĠ Ġoverride Ġfun Ġcredential Store Cle ared () Ġ{ ĊĠĠĠĠĠ Ġval Ġpublisher Ġ= ĠApplication Manager . getApplication () ĊĠĠĠĠĠĠĠ Ġ. message Bus ĊĠĠĠĠĠĠĠ Ġ. sync Publisher ( ACCOUNT _ TOKEN _ CHANGED _ TOPIC ) ĊĠĠĠĠĠ Ġservice < Github Account Manager >(). accounts . forEach ( publisher :: token Changed ) ĊĠĠĠ Ġ} ĊĠ Ġ} Ċ } Ċ Ċ private Ġfun Ġcreate Credential Attributes ( accountId : ĠString ) Ġ= ĠCredential Attributes ( create ServiceName ( accountId )) Ċ Ċ private Ġfun Ġcreate Credentials ( accountId : ĠString , Ġtoken : ĠString ) Ġ= ĠCredentials ( accountId , Ġtoken ) Ċ Ċ private Ġfun Ġcreate ServiceName ( accountId : ĠString ): ĠString Ġ= Ġgenerate ServiceName ( Github Util . SERVICE _ DISPLAY _ NAME , ĠaccountId ) Ċ Ċ interface ĠAccount Removed Listener Ġ{ ĊĠ Ġfun Ġaccount Removed ( removed Account : ĠGithub Account ) Ċ } Ċ Ċ interface ĠAccount Token Changed Listener Ġ{ ĊĠ Ġfun Ġtoken Changed ( account : ĠGithub Account ) Ċ }