11package em .embedded .rest .tiltaksgjennomforing .api ;
22
3+ import com .nimbusds .jose .JOSEObjectType ;
34import no .nav .security .mock .oauth2 .MockOAuth2Server ;
45import no .nav .security .mock .oauth2 .OAuth2Config ;
56import no .nav .security .mock .oauth2 .token .RequestMapping ;
67import no .nav .security .mock .oauth2 .token .RequestMappingTokenCallback ;
78import no .nav .tag .tiltaksgjennomforing .TiltaksgjennomforingApplication ;
9+ import no .nav .tag .tiltaksgjennomforing .autorisasjon .TokenUtils ;
810import org .evomaster .client .java .controller .EmbeddedSutController ;
911import org .evomaster .client .java .controller .InstrumentedSutStarter ;
1012import org .evomaster .client .java .controller .api .dto .SutInfoDto ;
1113import org .evomaster .client .java .controller .api .dto .auth .AuthenticationDto ;
14+ import org .evomaster .client .java .controller .api .dto .auth .HttpVerb ;
15+ import org .evomaster .client .java .controller .api .dto .auth .LoginEndpointDto ;
16+ import org .evomaster .client .java .controller .api .dto .auth .TokenHandlingDto ;
1217import org .evomaster .client .java .controller .api .dto .database .schema .DatabaseType ;
1318import org .evomaster .client .java .controller .problem .ProblemInfo ;
1419import org .evomaster .client .java .controller .problem .RestProblem ;
@@ -45,8 +50,7 @@ public class EmbeddedEvoMasterController extends EmbeddedSutController {
4550 private List <DbSpecification > dbSpecification ;
4651
4752 private MockOAuth2Server oAuth2Server ;
48- private final String ISSUER_ID = "azuread" ;
49- private final String DEFAULT_AUDIENCE = "some-audience" ;
53+ private final String BESLUTTER_AD_GROUP = "99ea78dc-db77-44d0-b193-c5dc22f01e1d" ;
5054
5155
5256 public EmbeddedEvoMasterController () {
@@ -79,10 +83,39 @@ public String getPackagePrefixesToCover() {
7983 return "no.nav.tag.tiltaksgjennomforing." ;
8084 }
8185
86+ private AuthenticationDto getAuthenticationDto (String label , String keyValue , String oauth2Url ){
87+
88+ AuthenticationDto dto = new AuthenticationDto (label );
89+ LoginEndpointDto x = new LoginEndpointDto ();
90+ dto .loginEndpointAuth = x ;
91+
92+ x .externalEndpointURL = oauth2Url ;
93+ x .payloadRaw = keyValue +"&grant_type=client_credentials&code=foo&client_id=foo&client_secret=secret" ;
94+ x .verb = HttpVerb .POST ;
95+ x .contentType = "application/x-www-form-urlencoded" ;
96+ x .expectCookies = false ;
97+
98+ TokenHandlingDto token = new TokenHandlingDto ();
99+ token .headerPrefix = "Bearer " ;
100+ token .httpHeaderName = "Authorization" ;
101+ token .extractFromField = "/access_token" ;
102+ x .token = token ;
103+
104+ return dto ;
105+ }
106+
82107 @ Override
83108 public List <AuthenticationDto > getInfoForAuthentication () {
84- //TODO
85- return null ;
109+ String urlAad = oAuth2Server .baseUrl () + "aad/token" ;
110+ String urlSystem = oAuth2Server .baseUrl () + "system/token" ;
111+ String urlTokenX = oAuth2Server .baseUrl () + "tokenx/token" ;
112+
113+ return Arrays .asList (
114+ getAuthenticationDto ("aad" ,"NAVident=Q987654" , urlAad ),
115+ getAuthenticationDto ("system" ,"sub=system" , urlSystem ),
116+ getAuthenticationDto ("tokenxLevel3" ,"pid=88888888888" , urlTokenX ),
117+ getAuthenticationDto ("tokenxLevel4" ,"pid=99999999999" , urlTokenX )
118+ );
86119 }
87120
88121 @ Override
@@ -98,32 +131,71 @@ public SutInfoDto.OutputFormat getPreferredOutputFormat() {
98131 return SutInfoDto .OutputFormat .JAVA_JUNIT_5 ;
99132 }
100133
134+ private RequestMapping getRequestMapping (String key , String value , String issuer , String subject , List <String > audience , String navIdent , String acrLevel , List <String > groups , String pid ) {
135+ Map <String ,Object > claims = new HashMap <>();
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 ());
149+
150+ return rm ;
151+ }
101152
102153 private OAuth2Config getOAuth2Config (){
103154
104155 List <RequestMapping > mappings = Arrays .asList (
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" )
105166 );
106167
107168 RequestMappingTokenCallback callback = new RequestMappingTokenCallback (
108- ISSUER_ID ,
169+ "aad" ,
109170 mappings ,
110171 360000
111172 );
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+ );
112184
113185 Set <RequestMappingTokenCallback > callbacks = Set .of (
114- callback
186+ callback ,
187+ callbackSystem ,
188+ callbackTokenx
115189 );
116190
117- OAuth2Config config = new OAuth2Config (
191+ return new OAuth2Config (
118192 true ,
119193 null ,
120194 null ,
121195 false ,
122196 new no .nav .security .mock .oauth2 .token .OAuth2TokenProvider (),
123197 callbacks
124198 );
125-
126- return config ;
127199 }
128200
129201 @ Override
@@ -134,12 +206,14 @@ public String startSut() {
134206
135207 oAuth2Server = new MockOAuth2Server (getOAuth2Config ());
136208 oAuth2Server .start (8081 ); //ephemeral gives issues in generated tests
137- String wellKnownUrl = oAuth2Server .wellKnownUrl (ISSUER_ID ).toString ();
209+ String wellKnownUrl = oAuth2Server .wellKnownUrl ("aad" ).toString ();
210+ String wellKnownUrlSystem = oAuth2Server .wellKnownUrl ("system" ).toString ();
211+ String wellKnownUrlTokenX = oAuth2Server .wellKnownUrl ("tokenx" ).toString ();
138212
139213 //TODO should go through all the environment variables in application properties
140214 //TODO some of these might not be needed any more after change of profile
141215 System .setProperty ("AZURE_APP_WELL_KNOWN_URL" ,wellKnownUrl );
142- System .setProperty ("TOKEN_X_WELL_KNOWN_URL" ,wellKnownUrl );
216+ System .setProperty ("TOKEN_X_WELL_KNOWN_URL" ,wellKnownUrlTokenX );
143217 System .setProperty ("VAULT_TOKEN" ,"VAULT_TOKEN" );
144218 System .setProperty ("KAFKA_BROKERS" ,"KAFKA_BROKERS" );
145219 System .setProperty ("KAFKA_TRUSTSTORE_PATH" ,"KAFKA_TRUSTSTORE_PATH" );
@@ -150,9 +224,9 @@ public String startSut() {
150224 System .setProperty ("KAFKA_SCHEMA_REGISTRY_USER" ,"KAFKA_SCHEMA_REGISTRY_USER" );
151225 System .setProperty ("KAFKA_SCHEMA_REGISTRY_PASSWORD" ,"KAFKA_SCHEMA_REGISTRY_PASSWORD" );
152226 System .setProperty ("AZURE_APP_TENANT_ID" ,"AZURE_APP_TENANT_ID" );
153- System .setProperty ("AZURE_APP_CLIENT_ID" ,"AZURE_APP_CLIENT_ID " );
154- System .setProperty ("AZURE_APP_CLIENT_SECRET" ,"AZURE_APP_CLIENT_SECRET " );
155- System .setProperty ("beslutter.ad.gruppe" ,"99ea78dc-db77-44d0-b193-c5dc22f01e1d" );
227+ System .setProperty ("AZURE_APP_CLIENT_ID" ,"aad " );
228+ System .setProperty ("AZURE_APP_CLIENT_SECRET" ,"secret " );
229+ System .setProperty ("beslutter.ad.gruppe" ,BESLUTTER_AD_GROUP );
156230
157231 ctx = SpringApplication .run (TiltaksgjennomforingApplication .class , new String []{
158232 "--server.port=0" ,
@@ -161,7 +235,11 @@ public String startSut() {
161235 "--spring.datasource.driverClassName=org.postgresql.Driver" ,
162236 "--spring.sql.init.platform=postgres" ,
163237 "--no.nav.security.jwt.issuer.aad.discoveryurl=" + wellKnownUrl ,
164- "--no.nav.security.jwt.issuer.tokenx.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" ,
241+ "--no.nav.security.jwt.issuer.tokenx.discoveryurl=" + wellKnownUrlTokenX ,
242+ "--no.nav.security.jwt.issuer.tokenx.accepted_audience=tokenx" ,
165243 "--management.server.port=-1" ,
166244 "--server.ssl.enabled=false" ,
167245 "--spring.datasource.url=" + postgresURL ,
0 commit comments