Skip to content

Commit

Permalink
Some minor refactoring to remove duplicates/warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
rfc3092 committed Sep 11, 2024
1 parent a8470a1 commit 54167ab
Show file tree
Hide file tree
Showing 20 changed files with 243 additions and 242 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package no.nav.testnav.mocks.maskinporten;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.reactive.config.EnableWebFlux;

import no.nav.testnav.libs.reactivecore.config.CoreConfig;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;

@Import({
CoreConfig.class,
})
@EnableWebFlux
@SpringBootApplication
public class MaskinportenMockApplicationStarter {
public static void main(String[] args) {
SpringApplication.run(MaskinportenMockApplicationStarter.class, args);
}

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public static class Utils {

@SneakyThrows
public static String loadJson(String path) {
try (final InputStreamReader stream = new InputStreamReader(new ClassPathResource(path).getInputStream(), StandardCharsets.UTF_8)) {
return new BufferedReader(stream)
.lines()
.collect(Collectors.joining("\n"));
}
}

}

}
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package no.nav.testnav.mocks.tokendingsmock.config;
package no.nav.testnav.mocks.maskinporten.config;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import no.nav.testnav.libs.reactivecore.config.ApplicationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import no.nav.testnav.libs.reactivecore.config.ApplicationProperties;


@Configuration
public class OpenApiConfig {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package no.nav.testnav.mocks.tokendingsmock.config;
package no.nav.testnav.mocks.maskinporten.config;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -18,6 +18,9 @@ public class SecurityConfig {

@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
return http.csrf(ServerHttpSecurity.CsrfSpec::disable).build();
return http
.csrf(ServerHttpSecurity.CsrfSpec::disable)
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package no.nav.testnav.mocks.maskinporten.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import no.nav.testnav.mocks.maskinporten.domain.AccessToken;
import no.nav.testnav.mocks.maskinporten.domain.Arguments;
import no.nav.testnav.mocks.maskinporten.service.JwtService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/mock")
public class MockController {

private final JwtService service;

@PostMapping(
value = "/token",
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
public Mono<AccessToken> getToken(Arguments arguments) {
return Mono.just(service.createAccessToken(arguments.getAudience()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package no.nav.testnav.mocks.maskinporten.controller;


import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import no.nav.testnav.mocks.maskinporten.domain.AccessToken;
import no.nav.testnav.mocks.maskinporten.domain.Arguments;
import no.nav.testnav.mocks.maskinporten.service.JwtService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import static no.nav.testnav.mocks.maskinporten.MaskinportenMockApplicationStarter.Utils.loadJson;

@Slf4j
@RestController
@RequiredArgsConstructor
public class OauthAuthorizationServiceController {

private static final String JWKS;
private static final String WELL_KNOWN;

static {
JWKS = loadJson("static/jwks.json");
WELL_KNOWN = loadJson("static/well-known.json");
}

private final JwtService jwtService;

@GetMapping(value = "/jwks", produces = MediaType.APPLICATION_JSON_VALUE)
public Mono<String> getJWKS() {
return Mono.just(JWKS);
}

@GetMapping(value = "/.well-known/oauth-authorization-server", produces = MediaType.APPLICATION_JSON_VALUE)
public Mono<String> getWellKnown() {
return Mono.just(WELL_KNOWN);
}

@PostMapping(value = "/token", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public Mono<AccessToken> createToken(Arguments arguments) {
return Mono.just(jwtService.createAccessToken(arguments.getAudience()));
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package no.nav.testnav.mocks.tokendingsmock.domain;
package no.nav.testnav.mocks.maskinporten.domain;

import com.fasterxml.jackson.annotation.JsonProperty;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package no.nav.testnav.mocks.tokendingsmock.domain;
package no.nav.testnav.mocks.maskinporten.domain;

import lombok.Getter;
import lombok.Setter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,32 @@
package no.nav.testnav.mocks.tokendingsmock.service;
package no.nav.testnav.mocks.maskinporten.service;


import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.nimbusds.jose.jwk.RSAKey;
import lombok.SneakyThrows;
import org.springframework.core.io.ClassPathResource;
import no.nav.testnav.mocks.maskinporten.domain.AccessToken;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.security.interfaces.RSAPrivateKey;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;
import java.util.stream.Collectors;

import no.nav.testnav.mocks.tokendingsmock.domain.AccessToken;
import static no.nav.testnav.mocks.maskinporten.MaskinportenMockApplicationStarter.Utils.loadJson;

@Service
public class JwtService {

private static final String jwtSecret;
private static final String JWK;

static {
jwtSecret = loadJson("static/jwk.json");
}

private static String loadJson(String path) {
var resource = new ClassPathResource(path);
try (final InputStreamReader stream = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)) {
return new BufferedReader(stream)
.lines().collect(Collectors.joining("\n"));

} catch (IOException e) {
throw new RuntimeException("Feil med paring av " + path + ".", e);
}
JWK = loadJson("static/jwk.json");
}

@SneakyThrows
public AccessToken createAccessToken(String audience) {

var date = Calendar.getInstance();
var expiresAt = date.getTimeInMillis() + (60 * 60 * 1000);
var builder = JWT
Expand All @@ -52,16 +37,14 @@ public AccessToken createAccessToken(String audience) {
.withAudience(audience)
.withJWTId(UUID.randomUUID().toString())
.withExpiresAt(new Date(expiresAt));

var privateKey = RSAKey.parse(jwtSecret).toPrivateKey();

var privateKey = RSAKey.parse(JWK).toPrivateKey();
return new AccessToken(
builder.sign(Algorithm.RSA256(null, (RSAPrivateKey) privateKey)),
"Bearer",
60 * 60 * 1000,
audience

);

}

}

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 54167ab

Please sign in to comment.