package Ġapp . d apk . st . matrix . auth . internal Ċ Ċ import Ġapp . d apk . st . matrix . auth . Auth Config Ċ import Ġapp . d apk . st . matrix . common . Credentials Store Ċ import Ġapp . d apk . st . matrix . common . Home Server Url Ċ import Ġapp . d apk . st . matrix . common . User Credentials Ċ import Ġapp . d apk . st . matrix . common . UserId Ċ import Ġapp . d apk . st . matrix . http . Matrix HttpClient Ċ import Ġapp . d apk . st . matrix . http . ensure Trailing Slash Ċ Ċ private Ġconst Ġval ĠMAT RIX _ DOT _ ORG _ DOMAIN Ġ= Ġ" matrix . org " Ċ Ċ class ĠLogin UseCase ( ĊĠĠĠ Ġprivate Ġval ĠhttpClient : ĠMatrix HttpClient , ĊĠĠĠ Ġprivate Ġval Ġcredentials Provider : ĠCredentials Store , ĊĠĠĠ Ġprivate Ġval Ġfetch Well Known UseCase : ĠFetch Well Known UseCase , ĊĠĠĠ Ġprivate Ġval Ġauth Config : ĠAuth Config Ċ ) Ġ{ ĊĊĠĠĠ Ġsuspend Ġfun Ġlogin ( userName : ĠString , Ġpassword : ĠString ): ĠUser Credentials Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġ( domain Url , Ġfull UserId ) Ġ= Ġgenerate User Access Info ( userName ) ĊĠĠĠĠĠĠĠ Ġval ĠbaseUrl Ġ= Ġfetch Well Known UseCase ( domain Url ). home Server . baseUrl . ensure Trailing Slash () ĊĠĠĠĠĠĠĠ Ġval Ġauth Response Ġ= ĠhttpClient . execute ( login Request ( full UserId , Ġpassword , ĠbaseUrl . value )) ĊĠĠĠĠĠĠĠ Ġreturn ĠUser Credentials ( ĊĠĠĠĠĠĠĠĠĠĠĠ Ġauth Response . accessToken , ĊĠĠĠĠĠĠĠĠĠĠĠ ĠbaseUrl , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġauth Response . userId , ĊĠĠĠĠĠĠĠĠĠĠĠ Ġauth Response . deviceId , ĊĠĠĠĠĠĠĠ Ġ). also Ġ{ Ġcredentials Provider . update ( it ) Ġ} ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun Ġgenerate User Access Info ( userName : ĠString ): ĠPair < String , ĠUserId > Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġcleaned UserName Ġ= ĠuserName . ensure StartsWith At (). trim () ĊĠĠĠĠĠĠĠ Ġval Ġdomain Ġ= Ġcleaned UserName . find Domain ( fallback Ġ= ĠMAT RIX _ DOT _ ORG _ DOMAIN ) ĊĠĠĠĠĠĠĠ Ġval Ġdomain Url Ġ= Ġdomain . as Https Url () ĊĠĠĠĠĠĠĠ Ġval Ġfull UserId Ġ= Ġcleaned UserName . ensure Has Domain ( domain ) ĊĠĠĠĠĠĠĠ Ġreturn ĠPair ( domain Url , ĠUserId ( full UserId )) ĊĠĠĠ Ġ} ĊĊĠĠĠ Ġprivate Ġfun ĠString . find Domain ( fallback : ĠString ) Ġ= Ġthis . substring After (" :", Ġmissing Delimiter Value Ġ= Ġfallback ) ĊĊĠĠĠ Ġprivate Ġfun ĠString . as Https Url (): ĠString Ġ{ ĊĠĠĠĠĠĠĠ Ġval Ġschema Ġ= Ġwhen Ġ( auth Config . force Http ) Ġ{ ĊĠĠĠĠĠĠĠĠĠĠĠ Ġtrue Ġ-> Ġ" http ://" ĊĠĠĠĠĠĠĠĠĠĠĠ Ġfalse Ġ-> Ġ" https ://" ĊĠĠĠĠĠĠĠ Ġ} ĊĠĠĠĠĠĠĠ Ġreturn Ġ"$ schema $ this ". ensure Trailing Slash () ĊĠĠĠ Ġ} Ċ } Ċ Ċ private Ġfun ĠHome Server Url . ensure Trailing Slash () Ġ= ĠHome Server Url ( this . value . ensure Trailing Slash ()) Ċ Ċ private Ġfun ĠString . ensure Has Domain ( domain : ĠString ) Ġ= Ġif Ġ( this . endsWith ( domain )) Ġthis Ġelse Ġ"$ this :$ domain " Ċ Ċ private Ġfun ĠString . ensure StartsWith At (): ĠString Ġ{ ĊĠĠĠ Ġreturn Ġwhen Ġ( this . startsWith ("@ ")) Ġ{ ĊĠĠĠĠĠĠĠ Ġtrue Ġ-> Ġthis ĊĠĠĠĠĠĠĠ Ġfalse Ġ-> Ġ"@ $ this " ĊĠĠĠ Ġ} Ċ } Ċ