Skip to content

Commit aec73ce

Browse files
authored
Merge pull request #110 from WebFuzzing/tiltak-auth
tiltaksgjennomforing-api auth
2 parents d800b8e + 7fca253 commit aec73ce

7 files changed

Lines changed: 325 additions & 32 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
auth:
2+
- name: "aad"
3+
loginEndpointAuth:
4+
externalEndpointURL: "http://localhost:8083/aad/token"
5+
payloadRaw: "NAVident=Q987654&grant_type=client_credentials&code=foo&client_id=foo&client_secret=secret"
6+
- name: "system"
7+
loginEndpointAuth:
8+
externalEndpointURL: "http://localhost:8083/system/token"
9+
payloadRaw: "sub=system&grant_type=client_credentials&code=foo&client_id=foo&client_secret=secret"
10+
- name: "tokenxLevel3"
11+
loginEndpointAuth:
12+
externalEndpointURL: "http://localhost:8083/tokenx/token"
13+
payloadRaw: "pid=88888888888&grant_type=client_credentials&code=foo&client_id=foo&client_secret=secret"
14+
- name: "tokenxLevel4"
15+
loginEndpointAuth:
16+
externalEndpointURL: "http://localhost:8083/tokenx/token"
17+
payloadRaw: "pid=99999999999&grant_type=client_credentials&code=foo&client_id=foo&client_secret=secret"
18+
19+
authTemplate:
20+
loginEndpointAuth:
21+
verb: POST
22+
contentType: application/x-www-form-urlencoded
23+
token:
24+
extractFromField: /access_token
25+
httpHeaderName: Authorization
26+
headerPrefix: "Bearer "

dockerfiles/tiltaksgjennomforing-api.dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ ENTRYPOINT \
1414
# -javaagent:jacocoagent.jar=destfile=./jacoco/tiltaksgjennomforing-api__${TOOL}__${RUN}__jacoco.exec,append=false,dumponexit=true \
1515
-javaagent:jacocoagent.jar=output=tcpserver,address=*,port=6300,append=false,dumponexit=false \
1616
-jar tiltaksgjennomforing-api-sut.jar \
17-
--server.port=8080 --spring.profiles.active=dev-gcp-labs --spring.datasource.driverClassName=org.postgresql.Driver --spring.sql.init.platform=postgres --no.nav.security.jwt.issuer.aad.discoveryurl=http://mock-oauth2-server:8083/azuread/.well-known/openid-configuration --no.nav.security.jwt.issuer.tokenx.discoveryurl=http://mock-oauth2-server:8083/azuread/.well-known/openid-configuration --management.server.port=-1 --server.ssl.enabled=false --spring.datasource.url=jdbc:postgresql://db:5432/tiltaksgjennomforing --spring.datasource.username=postgres --spring.datasource.password=password --sentry.logging.enabled=false --sentry.environment=local --logging.level.root=OFF --logging.config=classpath:logback-spring.xml --logging.level.org.springframework=INFO
17+
--server.port=8080 --spring.profiles.active=dev-gcp-labs --spring.datasource.driverClassName=org.postgresql.Driver --spring.sql.init.platform=postgres --no.nav.security.jwt.issuer.aad.discoveryurl=http://mock-oauth2-server:8083/aad/.well-known/openid-configuration --no.nav.security.jwt.issuer.aad.accepted_audience=aad --no.nav.security.jwt.issuer.system.discoveryurl=http://mock-oauth2-server:8083/system/.well-known/openid-configuration --no.nav.security.jwt.issuer.system.accepted_audience=system --no.nav.security.jwt.issuer.tokenx.discoveryurl=http://mock-oauth2-server:8083/tokenx/.well-known/openid-configuration --no.nav.security.jwt.issuer.tokenx.accepted_audience=tokenx --management.server.port=-1 --server.ssl.enabled=false --spring.datasource.url=jdbc:postgresql://db:5432/tiltaksgjennomforing --spring.datasource.username=postgres --spring.datasource.password=password --sentry.logging.enabled=false --sentry.environment=local --logging.level.root=OFF --logging.config=classpath:logback-spring.xml --logging.level.org.springframework=INFO

dockerfiles/tiltaksgjennomforing-api.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ services:
3535
JSON_CONFIG_PATH: /app/mockoauth2.json
3636
volumes:
3737
- ../scripts/dockerize/data/additional_files/tiltaksgjennomforing-api/mockoauth2.json:/app/mockoauth2.json
38-
38+
ports:
39+
- "8083:8083"

jdk_17_maven/em/embedded/rest/tiltaksgjennomforing-api/src/main/java/em/embedded/rest/tiltaksgjennomforing/api/EmbeddedEvoMasterController.java

Lines changed: 93 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package em.embedded.rest.tiltaksgjennomforing.api;
22

3+
import com.nimbusds.jose.JOSEObjectType;
34
import no.nav.security.mock.oauth2.MockOAuth2Server;
45
import no.nav.security.mock.oauth2.OAuth2Config;
56
import no.nav.security.mock.oauth2.token.RequestMapping;
67
import no.nav.security.mock.oauth2.token.RequestMappingTokenCallback;
78
import no.nav.tag.tiltaksgjennomforing.TiltaksgjennomforingApplication;
9+
import no.nav.tag.tiltaksgjennomforing.autorisasjon.TokenUtils;
810
import org.evomaster.client.java.controller.EmbeddedSutController;
911
import org.evomaster.client.java.controller.InstrumentedSutStarter;
1012
import org.evomaster.client.java.controller.api.dto.SutInfoDto;
1113
import 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;
1217
import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType;
1318
import org.evomaster.client.java.controller.problem.ProblemInfo;
1419
import 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

Comments
 (0)