Skip to content

Commit 567a62e

Browse files
committed
setting up auth with mock-oauth2
1 parent 5e13dfa commit 567a62e

2 files changed

Lines changed: 99 additions & 9 deletions

File tree

jdk_17_maven/em/embedded/rest/familie-ba-sak/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@
4040
<groupId>org.springframework</groupId>
4141
<artifactId>spring-context</artifactId>
4242
</dependency>
43+
<dependency>
44+
<groupId>no.nav.security</groupId>
45+
<artifactId>mock-oauth2-server</artifactId>
46+
<version>2.0.1</version>
47+
</dependency>
4348
</dependencies>
4449

50+
51+
4552
</project>

jdk_17_maven/em/embedded/rest/familie-ba-sak/src/main/java/em/embedded/familie/ba/sak/EmbeddedEvoMasterController.java

Lines changed: 92 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package em.embedded.familie.ba.sak;
22

3-
import no.nav.familie.ba.sak.ApplicationKt;
3+
import com.nimbusds.jose.JOSEObjectType;
4+
import no.nav.security.mock.oauth2.MockOAuth2Server;
5+
import no.nav.security.mock.oauth2.token.DefaultOAuth2TokenCallback;
6+
import org.evomaster.client.java.controller.AuthUtils;
47
import org.evomaster.client.java.controller.EmbeddedSutController;
58
import org.evomaster.client.java.controller.InstrumentedSutStarter;
69
import org.evomaster.client.java.controller.api.dto.SutInfoDto;
710
import org.evomaster.client.java.controller.api.dto.auth.AuthenticationDto;
811
import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType;
912
import org.evomaster.client.java.controller.problem.ProblemInfo;
1013
import org.evomaster.client.java.controller.problem.RestProblem;
11-
import org.evomaster.client.java.sql.DbCleaner;
1214
import org.evomaster.client.java.sql.DbSpecification;
1315
import org.springframework.boot.SpringApplication;
1416
import org.springframework.context.ConfigurableApplicationContext;
@@ -17,10 +19,8 @@
1719
import java.sql.Connection;
1820
import java.sql.DriverManager;
1921
import java.sql.SQLException;
20-
import java.util.Arrays;
21-
import java.util.Collections;
22-
import java.util.List;
23-
import java.util.Map;
22+
import java.util.*;
23+
2424

2525
public class EmbeddedEvoMasterController extends EmbeddedSutController {
2626

@@ -39,6 +39,14 @@ public class EmbeddedEvoMasterController extends EmbeddedSutController {
3939

4040
private ConfigurableApplicationContext ctx;
4141

42+
private MockOAuth2Server oAuth2Server;
43+
44+
private final String ISSUER_ID = "azuread";
45+
46+
private final String DEFAULT_AUDIENCE = "some-audience";
47+
48+
private final String PROSESSERING_ROLLE = "928636f4-fd0d-4149-978e-a6fb68bb19de";
49+
4250
private Connection sqlConnection;
4351
private List<DbSpecification> dbSpecification;
4452

@@ -75,10 +83,65 @@ public String getPackagePrefixesToCover() {
7583

7684
@Override
7785
public List<AuthenticationDto> getInfoForAuthentication() {
78-
//TODO seems like it uses auth
79-
return null;
86+
87+
//see RolletilgangTest
88+
String token_veileder = getToken(Arrays.asList("VEILEDER"),"Z0000", "Mock McMockface");
89+
String token_saksbehandler = getToken(Arrays.asList("SAKSBEHANDLER"),"Z0001", "Foo Bar");
90+
String token_beslutter = getToken(Arrays.asList("BESLUTTER"),"Z0002", "John Smith");
91+
String token_forvalter = getToken(Arrays.asList("FORVALTER"),"Z0003", "Mario Rossi");
92+
String token_kode6 = getToken(Arrays.asList("KODE6"),"Z0004", "Kode Six");
93+
String token_kode7 = getToken(Arrays.asList("KODE7"),"Z0005", "Kode Seven");
94+
95+
/*
96+
TODO check
97+
enum class BehandlerRolle(val nivå: Int) {
98+
SYSTEM(4),
99+
BESLUTTER(3),
100+
SAKSBEHANDLER(2),
101+
VEILEDER(1),
102+
UKJENT(0),
103+
}
104+
*/
105+
106+
/*
107+
FIXME
108+
prosessering_rolle is only for endpoints under:
109+
"/api/task"
110+
TODO need to check how others are validated...
111+
*/
112+
113+
return Arrays.asList(
114+
AuthUtils.getForAuthorizationHeader("Veileder", "Bearer " + token_veileder),
115+
AuthUtils.getForAuthorizationHeader("Saksbehandler", "Bearer " + token_saksbehandler),
116+
AuthUtils.getForAuthorizationHeader("Beslutter", "Bearer " + token_beslutter),
117+
AuthUtils.getForAuthorizationHeader("Forvalter", "Bearer " + token_forvalter),
118+
AuthUtils.getForAuthorizationHeader("Kode6", "Bearer " + token_kode6),
119+
AuthUtils.getForAuthorizationHeader("Kode7", "Bearer " + token_kode7)
120+
);
80121
}
81122

123+
private String getToken(List<String> groups, String id, String name) {
124+
Map<String,Object> claims = new HashMap<>();
125+
claims.put("groups",groups);
126+
claims.put("name",name);
127+
claims.put("NAVident", id);
128+
129+
String token = oAuth2Server.issueToken(
130+
ISSUER_ID,
131+
id,
132+
new DefaultOAuth2TokenCallback(
133+
ISSUER_ID,
134+
"subject",
135+
JOSEObjectType.JWT.getType(),
136+
Arrays.asList(DEFAULT_AUDIENCE),
137+
claims,
138+
360000
139+
)
140+
).serialize();
141+
return token;
142+
}
143+
144+
82145
@Override
83146
public ProblemInfo getProblemInfo() {
84147
return new RestProblem(
@@ -96,6 +159,12 @@ public SutInfoDto.OutputFormat getPreferredOutputFormat() {
96159
public String startSut() {
97160
postgresContainer.start();
98161

162+
oAuth2Server = new MockOAuth2Server();
163+
oAuth2Server.start(8081); //TODO fixed until we handle dynamic headers in generated tests
164+
165+
String wellKnownUrl = oAuth2Server.wellKnownUrl(ISSUER_ID).toString();
166+
167+
99168
String postgresURL = "jdbc:postgresql://" + postgresContainer.getHost() + ":" + postgresContainer.getMappedPort(POSTGRES_PORT) + "/familiebasak";
100169

101170
//TODO should go through all the environment variables in application properties
@@ -105,7 +174,7 @@ public String startSut() {
105174
System.setProperty("NAIS_APP_NAME","bar");
106175
System.setProperty("UNLEASH_SERVER_API_URL","http://fake-unleash-server-api.no:8080");
107176
System.setProperty("UNLEASH_SERVER_API_TOKEN","bar");
108-
177+
System.setProperty("BA_SAK_CLIENT_ID", DEFAULT_AUDIENCE);
109178

110179
ctx = SpringApplication.run(no.nav.familie.ba.sak.FamilieBaSakApplication.class, new String[]{
111180
"--server.port=0",
@@ -123,6 +192,19 @@ public String startSut() {
123192
"--logging.level.root=OFF",
124193
"--logging.config=classpath:logback-spring.xml",
125194
"--logging.level.org.springframework=INFO",
195+
"--no.nav.security.jwt.issuer.azuread.discoveryurl="+wellKnownUrl,
196+
"--prosessering.rolle=" + PROSESSERING_ROLLE,
197+
"--FAMILIE_EF_SAK_API_URL=http://fake-familie-ef-sak/api",
198+
"--FAMILIE_KLAGE_URL=http://fake-familie-klage",
199+
"--FAMILIE_BREV_API_URL=http://fake-familie-brev",
200+
"--FAMILIE_BA_INFOTRYGD_FEED_API_URL=http://fake-familie-ba-infotrygd-feed/api",
201+
"--FAMILIE_BA_INFOTRYGD_API_URL=http://fake-familie-ba-infotrygd",
202+
"--FAMILIE_TILBAKE_API_URL=http://fake-familie-tilbake/api",
203+
"--PDL_URL=http://fake-pdl-api.default",
204+
"--FAMILIE_INTEGRASJONER_API_URL=http://fake-familie-integrasjoner/api",
205+
"--FAMILIE_OPPDRAG_API_URL=http://fake-familie-oppdrag/api",
206+
"--SANITY_FAMILIE_API_URL=http://fake-xsrv1mh6.apicdn.sanity.io/v2021-06-07/data/query/ba-brev",
207+
"--ECB_API_URL=http://fake-data-api.ecb.europa.eu/service/data/EXR/"
126208
});
127209

128210
if (sqlConnection != null) {
@@ -154,6 +236,7 @@ protected int getSutPort() {
154236
@Override
155237
public void stopSut() {
156238
postgresContainer.stop();
239+
if(oAuth2Server!=null) oAuth2Server.shutdown();
157240
if(ctx!=null)ctx.stop();
158241
}
159242

0 commit comments

Comments
 (0)