package Ġuk . gov . just ice . digital . h mp ps . oauth 2 server . security Ċ Ċ import Ġcom . microsoft . application insights . Telemetry Client Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . mock Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . verify Ċ import Ġcom . n ha ar man . mock it ok otlin 2 . wh enever Ċ import Ġorg . assertj . core . api . Assertions . assertThat Thrown By Ċ import Ġorg . junit . jupiter . api . Test Ċ import Ġorg . springframework . security . core . Authentication Ċ import Ġorg . springframework . security . oauth 2 . core . oidc . Oid c Id Token Ċ import Ġorg . springframework . security . oauth 2 . core . oidc . user . Default Oid c User Ċ import Ġorg . springframework . security . oauth 2 . core . oidc . user . Oid c User Authority Ċ import Ġorg . springframework . web . client . Rest Template Ċ import Ġuk . gov . just ice . digital . h mp ps . oauth 2 server . azure . Azure User Person Details Ċ import Ġuk . gov . just ice . digital . h mp ps . oauth 2 server . config . Cookie Request Cache Ċ import Ġuk . gov . just ice . digital . h mp ps . oauth 2 server . service . Client Service Ċ import Ġuk . gov . just ice . digital . h mp ps . oauth 2 server . verify . Verify Email Service Ċ import Ġjava . time . Instant Ċ import Ġjava . util . ArrayList Ċ import Ġjavax . servlet . http . HttpServletRequest Ċ import Ġjavax . servlet . http . HttpServletResponse Ċ Ċ internal Ġclass ĠO id c Jwt Authentication Success Handler Test Ġ{ ĊĠ Ġprivate Ġval Ġmock Jwt Cookie Helper : ĠJwt Cookie Helper Ġ= Ġmock () ĊĠ Ġprivate Ġval Ġmock Jwt Authentication Helper : ĠJwt Authentication Helper Ġ= Ġmock () ĊĠ Ġprivate Ġval Ġcookie Request Cache : ĠCookie Request Cache Ġ= Ġmock () ĊĠ Ġprivate Ġval Ġverify Email Service : ĠVerify Email Service Ġ= Ġmock () ĊĠ Ġprivate Ġval Ġclient Service : ĠClient Service Ġ= Ġmock () ĊĠ Ġprivate Ġval Ġrest Template : ĠRest Template Ġ= Ġmock () ĊĠ Ġprivate Ġval Ġuser Retries Service : ĠUser Retries Service Ġ= Ġmock () ĊĠ Ġprivate Ġval Ġtelemetry Client : ĠTe lemetry Client Ġ= Ġmock () ĊĠ Ġprivate Ġval Ġmock Request : ĠHttpServletRequest Ġ= Ġmock () ĊĠ Ġprivate Ġval Ġresponse : ĠHttpServletResponse Ġ= Ġmock () ĊĠ Ġprivate Ġval Ġauthentication : ĠAuthentication Ġ= Ġmock () ĊĊĠ Ġprivate Ġval Ġoid c Jwt Authentication Success Handler Ġ= ĠO id c Jwt Authentication Success Handler ( ĊĠĠĠ Ġmock Jwt Cookie Helper , ĊĠĠĠ Ġmock Jwt Authentication Helper , ĊĠĠĠ Ġcookie Request Cache , ĊĠĠĠ Ġverify Email Service , ĊĠĠĠ Ġclient Service , ĊĠĠĠ Ġrest Template , ĊĠĠĠ Ġtoken Verification Enabled Ġ= Ġfalse , ĊĠĠĠ Ġuser Retries Service , ĊĠĠĠ Ġtelemetry Client , ĊĠ Ġ) ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` on Authentication Success Ġuses ĠGiven Name Ġand ĠFamily Name Ġwhen Ġavailable `() Ġ{ ĊĠĠĠ Ġval Ġoid c Token Ġ= ĠO id c Id Token ( ĊĠĠĠĠĠ Ġ" token Value ", ĊĠĠĠĠĠ ĠInstant . now (), ĊĠĠĠĠĠ ĠInstant . now (). plus Seconds ( 1 0 0 0 ), ĊĠĠĠĠĠ Ġ// Ġid Ġclaims ĊĠĠĠĠĠ Ġmap Of ( ĊĠĠĠĠĠĠĠ Ġ" sub " Ġto Ġ" 6 C 2 x 9 v so M 3 F gi 9 Q me YZ G UT 4 hd Y me 3 Dw 1 5 6 6 tp 8 yc 1 v E ", ĊĠĠĠĠĠĠĠ Ġ" given _ name " Ġto Ġ" Jo e ", ĊĠĠĠĠĠĠĠ Ġ" family _ name " Ġto Ġ" Blog gs ", ĊĠĠĠĠĠĠĠ Ġ" oid " Ġto Ġ" d 6 1 6 5 ad 0 - aed 3 - 4 1 4 6 - 9 ef 7 - 2 2 2 8 7 6 b 5 7 5 4 9 ", ĊĠĠĠĠĠĠĠ Ġ" preferred _ username " Ġto Ġ" Jo e . Blog gs @ just ice . gov . uk ", ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġwhenever ( authentication . principal ) ĊĠĠĠĠĠ Ġ. thenReturn ( Default Oid c User ( listOf ( Oid c User Authority ( oidc Token )), Ġoid c Token )) ĊĠĠĠ Ġwhenever ( authentication . name ). thenReturn (" Bob ") ĊĠĠĠ Ġoid c Jwt Authentication Success Handler . on Authentication Success ( mock Request , Ġresponse , Ġauthentication ) ĊĊĠĠĠ Ġverify ( user Retries Service ). reset Retries And Record Login ( ĊĠĠĠĠĠ ĠAzure User Person Details ( ĊĠĠĠĠĠĠĠ ĠArrayList (), ĊĠĠĠĠĠĠĠ Ġtrue , ĊĠĠĠĠĠĠĠ Ġ" D 6 1 6 5 AD 0 - A ED 3 - 4 1 4 6 - 9 EF 7 - 2 2 2 8 7 6 B 5 7 5 4 9 ", ĊĠĠĠĠĠĠĠ Ġ" Jo e ", ĊĠĠĠĠĠĠĠ Ġ" Blog gs ", ĊĠĠĠĠĠĠĠ Ġ" jo e . blog gs @ just ice . gov . uk ", ĊĠĠĠĠĠĠĠ Ġtrue , ĊĠĠĠĠĠĠĠ Ġaccount Non Expired Ġ= Ġtrue , ĊĠĠĠĠĠĠĠ Ġaccount Non Locked Ġ= Ġtrue ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` on Authentication Success Ġuses ĠName Ġif ĠGiven Name Ġand ĠFamily Name Ġnot Ġavailable `() Ġ{ ĊĠĠĠ Ġval Ġoid c Token Ġ= ĠO id c Id Token ( ĊĠĠĠĠĠ Ġ" token Value ", ĊĠĠĠĠĠ ĠInstant . now (), ĊĠĠĠĠĠ ĠInstant . now (). plus Seconds ( 1 0 0 0 ), ĊĠĠĠĠĠ Ġ// Ġid Ġclaims ĊĠĠĠĠĠ Ġmap Of ( ĊĠĠĠĠĠĠĠ Ġ" sub " Ġto Ġ" 6 C 2 x 9 v so M 3 F gi 9 Q me YZ G UT 4 hd Y me 3 Dw 1 5 6 6 tp 8 yc 1 v E ", ĊĠĠĠĠĠĠĠ Ġ" name " Ġto Ġ" Blog gs , ĠJo e ", ĊĠĠĠĠĠĠĠ Ġ" oid " Ġto Ġ" d 6 1 6 5 ad 0 - aed 3 - 4 1 4 6 - 9 ef 7 - 2 2 2 8 7 6 b 5 7 5 4 9 ", ĊĠĠĠĠĠĠĠ Ġ" preferred _ username " Ġto Ġ" jo e . blog gs @ just ice . gov . uk ", ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġwhenever ( authentication . principal ) ĊĠĠĠĠĠ Ġ. thenReturn ( Default Oid c User ( listOf ( Oid c User Authority ( oidc Token )), Ġoid c Token )) ĊĠĠĠ Ġwhenever ( authentication . name ). thenReturn (" Bob ") ĊĊĠĠĠ Ġoid c Jwt Authentication Success Handler . on Authentication Success ( mock Request , Ġresponse , Ġauthentication ) ĊĊĠĠĠ Ġverify ( user Retries Service ). reset Retries And Record Login ( ĊĠĠĠĠĠ ĠAzure User Person Details ( ĊĠĠĠĠĠĠĠ ĠArrayList (), ĊĠĠĠĠĠĠĠ Ġtrue , ĊĠĠĠĠĠĠĠ Ġ" D 6 1 6 5 AD 0 - A ED 3 - 4 1 4 6 - 9 EF 7 - 2 2 2 8 7 6 B 5 7 5 4 9 ", ĊĠĠĠĠĠĠĠ Ġ" Jo e ", ĊĠĠĠĠĠĠĠ Ġ" Blog gs ", ĊĠĠĠĠĠĠĠ Ġ" jo e . blog gs @ just ice . gov . uk ", ĊĠĠĠĠĠĠĠ Ġtrue , ĊĠĠĠĠĠĠĠ Ġaccount Non Expired Ġ= Ġtrue , ĊĠĠĠĠĠĠĠ Ġaccount Non Locked Ġ= Ġtrue ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` on Authentication Success Ġgenerates Ġauthentication Ġsuccess Ġevent `() Ġ{ ĊĠĠĠ Ġval Ġoid c Token Ġ= ĠO id c Id Token ( ĊĠĠĠĠĠ Ġ" token Value ", ĊĠĠĠĠĠ ĠInstant . now (), ĊĠĠĠĠĠ ĠInstant . now (). plus Seconds ( 1 0 0 0 ), ĊĠĠĠĠĠ Ġ// Ġid Ġclaims ĊĠĠĠĠĠ Ġmap Of ( ĊĠĠĠĠĠĠĠ Ġ" sub " Ġto Ġ" 6 C 2 x 9 v so M 3 F gi 9 Q me YZ G UT 4 hd Y me 3 Dw 1 5 6 6 tp 8 yc 1 v E ", ĊĠĠĠĠĠĠĠ Ġ" name " Ġto Ġ" Blog gs , ĠJo e ", ĊĠĠĠĠĠĠĠ Ġ" oid " Ġto Ġ" d 6 1 6 5 ad 0 - aed 3 - 4 1 4 6 - 9 ef 7 - 2 2 2 8 7 6 b 5 7 5 4 9 ", ĊĠĠĠĠĠĠĠ Ġ" preferred _ username " Ġto Ġ" jo e . blog gs @ just ice . gov . uk ", ĊĠĠĠĠĠ Ġ) ĊĠĠĠ Ġ) ĊĊĠĠĠ Ġwhenever ( authentication . principal ) ĊĠĠĠĠĠ Ġ. thenReturn ( Default Oid c User ( listOf ( Oid c User Authority ( oidc Token )), Ġoid c Token )) ĊĠĠĠ Ġwhenever ( authentication . name ). thenReturn (" Bob ") ĊĊĠĠĠ Ġoid c Jwt Authentication Success Handler . on Authentication Success ( mock Request , Ġresponse , Ġauthentication ) ĊĊĠĠĠ Ġverify ( telemetry Client ). track Event ( ĊĠĠĠĠĠ Ġ" Authenticate Success ", ĊĠĠĠĠĠ Ġmap Of (" username " Ġto Ġ" jo e . blog gs @ just ice . gov . uk ", Ġ" auth Source " Ġto Ġ" azure ad "), ĊĠĠĠĠĠ Ġnull ĊĠĠĠ Ġ) ĊĠ Ġ} ĊĊĠ Ġ@ Test ĊĠ Ġfun Ġ` on Authentication Success Ġthrows Ġexception Ġif Ġprincipal Ġnot ĠDefault Oid c User `() Ġ{ ĊĠĠĠ Ġwhenever ( authentication . principal ) ĊĠĠĠĠĠ Ġ. thenReturn (" Dummy Ġprincipal ") ĊĊĠĠĠ ĠassertThat Thrown By Ġ{ ĊĠĠĠĠĠ Ġoid c Jwt Authentication Success Handler ĊĠĠĠĠĠĠĠ Ġ. on Authentication Success ( mock Request , Ġresponse , Ġauthentication ) ĊĠĠĠ Ġ}. isInstanceOf ( RuntimeException :: class . java ) ĊĠ Ġ} Ċ } Ċ