66import no .nav .security .mock .oauth2 .token .RequestMapping ;
77import no .nav .security .mock .oauth2 .token .RequestMappingTokenCallback ;
88import no .nav .tag .tiltaksgjennomforing .TiltaksgjennomforingApplication ;
9+ import no .nav .tag .tiltaksgjennomforing .autorisasjon .TokenUtils ;
910import org .evomaster .client .java .controller .EmbeddedSutController ;
1011import org .evomaster .client .java .controller .InstrumentedSutStarter ;
1112import org .evomaster .client .java .controller .api .dto .SutInfoDto ;
@@ -49,11 +50,7 @@ public class EmbeddedEvoMasterController extends EmbeddedSutController {
4950 private List <DbSpecification > dbSpecification ;
5051
5152 private MockOAuth2Server oAuth2Server ;
52- private final String ISSUER_ID = "aad" ;
53- private final String DEFAULT_AUDIENCE = "some-audience" ;
5453 private final String BESLUTTER_AD_GROUP = "99ea78dc-db77-44d0-b193-c5dc22f01e1d" ;
55- private final String TOKEN_PARAM = "NAVident" ;
56- private static final String NAV1 = "Q987654" ;
5754
5855
5956 public EmbeddedEvoMasterController () {
@@ -86,14 +83,14 @@ public String getPackagePrefixesToCover() {
8683 return "no.nav.tag.tiltaksgjennomforing." ;
8784 }
8885
89- private AuthenticationDto getAuthenticationDto (String label , String oauth2Url ){
86+ private AuthenticationDto getAuthenticationDto (String label , String keyValue , String oauth2Url ){
9087
9188 AuthenticationDto dto = new AuthenticationDto (label );
9289 LoginEndpointDto x = new LoginEndpointDto ();
9390 dto .loginEndpointAuth = x ;
9491
9592 x .externalEndpointURL = oauth2Url ;
96- x .payloadRaw = TOKEN_PARAM + "=" + label +"&grant_type=client_credentials&code=foo&client_id=foo&client_secret=secret" ;
93+ x .payloadRaw = keyValue +"&grant_type=client_credentials&code=foo&client_id=foo&client_secret=secret" ;
9794 x .verb = HttpVerb .POST ;
9895 x .contentType = "application/x-www-form-urlencoded" ;
9996 x .expectCookies = false ;
@@ -109,10 +106,15 @@ private AuthenticationDto getAuthenticationDto(String label, String oauth2Url){
109106
110107 @ Override
111108 public List <AuthenticationDto > getInfoForAuthentication () {
112- // NAVident=Q987654&grant_type=client_credentials&code=foo&client_id=foo&client_secret=secret
113- String url = oAuth2Server .baseUrl () + ISSUER_ID + "/token" ;
109+ String urlAad = oAuth2Server .baseUrl () + "aad/token" ;
110+ String urlSystem = oAuth2Server .baseUrl () + "system/token" ;
111+ String urlTokenX = oAuth2Server .baseUrl () + "tokenx/token" ;
112+
114113 return Arrays .asList (
115- getAuthenticationDto (NAV1 ,url )
114+ getAuthenticationDto ("aad" ,"NAVident=Q987654" , urlAad ),
115+ getAuthenticationDto ("system" ,"sub=system" , urlSystem ),
116+ getAuthenticationDto ("tokenxLevel3" ,"pid=88888888888" , urlTokenX ),
117+ getAuthenticationDto ("tokenxLevel4" ,"pid=99999999999" , urlTokenX )
116118 );
117119 }
118120
@@ -129,49 +131,71 @@ public SutInfoDto.OutputFormat getPreferredOutputFormat() {
129131 return SutInfoDto .OutputFormat .JAVA_JUNIT_5 ;
130132 }
131133
132- private RequestMapping getRequestMapping (String id , List <String > groups , String name ) {
134+ private RequestMapping getRequestMapping (String key , String value , String issuer , String subject , List <String > audience , String navIdent , String acrLevel , List < String > groups , String pid ) {
133135 Map <String ,Object > claims = new HashMap <>();
134- claims .put ("groups" ,groups );
135- claims .put ("name" ,name );
136- claims .put ("NAVident" , id );
137- claims .put ("sub" ,"sub" );
138- claims .put ("aud" ,Arrays .asList ("fake-aad" ));
139- claims .put ("tid" ,ISSUER_ID );
140- claims .put ("azp" ,id );
141- claims .put ("acr" ,"Level4" );
142- claims .put ("nonce" ,"myNonce" );
143-
144- RequestMapping rm = new RequestMapping ("NAVident" ,id , claims , JOSEObjectType .JWT .getType ());
136+ claims .put ("groups" , groups );
137+ claims .put ("NAVident" , navIdent );
138+ claims .put ("sub" , subject );
139+ claims .put ("aud" , audience );
140+ claims .put ("roles" , Arrays .asList ("access_as_application" ));
141+ claims .put ("pid" , pid );
142+ claims .put ("tid" , issuer );
143+ claims .put ("azp" , navIdent );
144+ claims .put ("acr" , acrLevel );
145+ claims .put ("ver" , "1.0" );
146+ claims .put ("nonce" , "myNonce" );
147+
148+ RequestMapping rm = new RequestMapping (key , value , claims , JOSEObjectType .JWT .getType ());
145149
146150 return rm ;
147151 }
148152
149153 private OAuth2Config getOAuth2Config (){
150154
151155 List <RequestMapping > mappings = Arrays .asList (
152- getRequestMapping (NAV1 , Arrays .asList (BESLUTTER_AD_GROUP ),"Mock McMockface" )
156+ getRequestMapping ("NAVident" , "Q987654" , "aad" ,"blablabla" , Arrays .asList ("aad" ), "Q987654" , "Level4" , Arrays .asList (BESLUTTER_AD_GROUP ), "aad" )
157+ );
158+
159+ List <RequestMapping > mappingsSystem = Arrays .asList (
160+ getRequestMapping ("sub" , "system" , "system" ,"system" , Arrays .asList ("system" ), null , null , null , "system" )
161+ );
162+
163+ List <RequestMapping > mappingsTokenx = Arrays .asList (
164+ getRequestMapping ("pid" , "88888888888" , "tokenx" ,"tokenx" , Arrays .asList ("tokenx" ), null , "Level3" , null , "88888888888" ),
165+ getRequestMapping ("pid" , "99999999999" , "tokenx" ,"tokenx" , Arrays .asList ("tokenx" ), null , "Level4" , null , "99999999999" )
153166 );
154167
155168 RequestMappingTokenCallback callback = new RequestMappingTokenCallback (
156- ISSUER_ID ,
169+ "aad" ,
157170 mappings ,
158171 360000
159172 );
173+ RequestMappingTokenCallback callbackSystem = new RequestMappingTokenCallback (
174+ "system" ,
175+ mappingsSystem ,
176+ 360000
177+ );
178+
179+ RequestMappingTokenCallback callbackTokenx = new RequestMappingTokenCallback (
180+ "tokenx" ,
181+ mappingsTokenx ,
182+ 360000
183+ );
160184
161185 Set <RequestMappingTokenCallback > callbacks = Set .of (
162- callback
186+ callback ,
187+ callbackSystem ,
188+ callbackTokenx
163189 );
164190
165- OAuth2Config config = new OAuth2Config (
191+ return new OAuth2Config (
166192 true ,
167193 null ,
168194 null ,
169195 false ,
170196 new no .nav .security .mock .oauth2 .token .OAuth2TokenProvider (),
171197 callbacks
172198 );
173-
174- return config ;
175199 }
176200
177201 @ Override
@@ -182,7 +206,8 @@ public String startSut() {
182206
183207 oAuth2Server = new MockOAuth2Server (getOAuth2Config ());
184208 oAuth2Server .start (8081 ); //ephemeral gives issues in generated tests
185- String wellKnownUrl = oAuth2Server .wellKnownUrl (ISSUER_ID ).toString ();
209+ String wellKnownUrl = oAuth2Server .wellKnownUrl ("aad" ).toString ();
210+ String wellKnownUrlSystem = oAuth2Server .wellKnownUrl ("system" ).toString ();
186211 String wellKnownUrlTokenX = oAuth2Server .wellKnownUrl ("tokenx" ).toString ();
187212
188213 //TODO should go through all the environment variables in application properties
@@ -210,7 +235,11 @@ public String startSut() {
210235 "--spring.datasource.driverClassName=org.postgresql.Driver" ,
211236 "--spring.sql.init.platform=postgres" ,
212237 "--no.nav.security.jwt.issuer.aad.discoveryurl=" + wellKnownUrl ,
238+ "--no.nav.security.jwt.issuer.aad.accepted_audience=aad" ,
239+ "--no.nav.security.jwt.issuer.system.discoveryurl=" + wellKnownUrlSystem ,
240+ "--no.nav.security.jwt.issuer.system.accepted_audience=system" ,
213241 "--no.nav.security.jwt.issuer.tokenx.discoveryurl=" + wellKnownUrlTokenX ,
242+ "--no.nav.security.jwt.issuer.tokenx.accepted_audience=tokenx" ,
214243 "--management.server.port=-1" ,
215244 "--server.ssl.enabled=false" ,
216245 "--spring.datasource.url=" + postgresURL ,
0 commit comments