Skip to content

Commit f0edbcc

Browse files
authored
Use JWT for authentication (#229)
* Pull out the existing security config * POC extract user info from JWT token * Deploy from PR * Correctly extract JWT from Header * Allow egress to auth server * Remove startup probe * Exclude /ping from user auth paths * Null check on auth header * Factor out into private methods * Unauthorized response when no token supplied * Implement KlassUserMapperJwt * Return generic message with error response * Remove klass-shared from workflow triggers * Add TODOs * Remove User Admin tab since functionality is no longer available * Use only the initials as username * Handle disabled functionality elegantly * Remove commented code * Revert "Deploy from PR" This reverts commit fd6f766. * Remove commented code * Remove more commented and unused code * Remove more commented code
1 parent 6045927 commit f0edbcc

24 files changed

+263
-750
lines changed

.github/workflows/klass-forvaltning-build-and-deploy.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ on:
88
- nais-migration
99
paths:
1010
- "klass-forvaltning/**"
11-
- "klass-shared/**"
1211
- ".nais/**/klass-forvaltning.yaml"
1312
- ".github/workflows/klass-forvaltning-build-and-deploy.yaml"
1413

.nais/test/klass-forvaltning.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ spec:
1111

1212
ingresses:
1313
- https://klass-forvaltning.intern.test.ssb.no
14-
14+
accessPolicy:
15+
outbound:
16+
external:
17+
- host: auth.test.ssb.no
1518
login:
1619
provider: openid
1720
enforce:

klass-forvaltning/pom.xml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<github.repository>statisticsnorway/klass</github.repository>
1818
<!-- Klass dependencies are built with support for Java 8 and Spring Boot 1.4.4.
1919
Newer versions are not supported -->
20-
<klass-shared-version>2.2.1</klass-shared-version>
20+
<klass-shared-version>2.2.2-SNAPSHOT</klass-shared-version>
2121
<klass-solr-version>2.1.7</klass-solr-version>
2222
<!-- Vaadin 7.7.17 was the last public open-source version of the Vaadin 7 framework.
2323
See https://vaadin.com/support/vaadin-7-extended-maintenance -->
@@ -92,15 +92,19 @@
9292
<version>${postgresql.version}</version>
9393
</dependency>
9494

95+
<dependency>
96+
<groupId>org.springframework.boot</groupId>
97+
<artifactId>spring-boot-starter-security</artifactId>
98+
</dependency>
9599
<dependency>
96100
<groupId>org.springframework.security</groupId>
97101
<artifactId>spring-security-oauth2-resource-server</artifactId>
98-
<version>5.1.13.RELEASE</version>
102+
<version>5.3.5.RELEASE</version>
99103
</dependency>
100104
<dependency>
101105
<groupId>org.springframework.security</groupId>
102106
<artifactId>spring-security-oauth2-jose</artifactId>
103-
<version>5.1.13.RELEASE</version>
107+
<version>5.3.5.RELEASE</version>
104108
</dependency>
105109

106110
<!--TOMCAT-->
@@ -212,6 +216,10 @@
212216
<version>4.0.4</version>
213217
<scope>compile</scope>
214218
</dependency>
219+
<dependency>
220+
<groupId>org.springframework.boot</groupId>
221+
<artifactId>spring-boot-autoconfigure</artifactId>
222+
</dependency>
215223
</dependencies>
216224

217225
<dependencyManagement>

klass-forvaltning/src/main/java/no/ssb/klass/KlassForvaltningApplication.java

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@
77
import org.springframework.boot.web.support.SpringBootServletInitializer;
88
import org.springframework.context.annotation.Import;
99

10-
import javax.servlet.ServletContext;
11-
import javax.servlet.ServletContextEvent;
12-
import javax.servlet.ServletContextListener;
13-
import javax.servlet.ServletException;
14-
1510

1611
@SpringBootApplication
1712
@Import(EmbeddedServletContainerAutoConfiguration.EmbeddedTomcat.class)
@@ -22,26 +17,6 @@ protected SpringApplicationBuilder configure(SpringApplicationBuilder applicatio
2217
return application.sources(KlassForvaltningApplication.class);
2318
}
2419

25-
@Override
26-
public void onStartup(ServletContext servletContext) throws ServletException {
27-
servletContext.addListener(new ServletContextListener() {
28-
29-
@Override
30-
public void contextInitialized(ServletContextEvent servletContextEvent) {
31-
// Nothing to do here
32-
}
33-
34-
@Override
35-
public void contextDestroyed(ServletContextEvent sce) {
36-
// Explicitly deregister the driver to prevent race conditions with Tomcat de-registering the Driver.
37-
// This is fixed in Spring Boot versions >=2.3.0
38-
// Ref https://github.com/spring-projects/spring-boot/issues/21221
39-
org.mariadb.jdbc.Driver.unloadDriver();
40-
}
41-
});
42-
super.onStartup(servletContext);
43-
}
44-
4520

4621
public static void main(String[] args) {
4722
SpringApplication.run(KlassForvaltningApplication.class, args);

klass-forvaltning/src/main/java/no/ssb/klass/designer/LoginDesign.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

klass-forvaltning/src/main/java/no/ssb/klass/designer/LoginView.java

Lines changed: 0 additions & 116 deletions
This file was deleted.

klass-forvaltning/src/main/java/no/ssb/klass/designer/MainView.java

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@
1010
import com.vaadin.ui.NativeSelect;
1111
import com.vaadin.ui.UI;
1212
import no.ssb.klass.core.model.ClassificationType;
13-
import no.ssb.klass.core.model.User;
1413
import no.ssb.klass.designer.admin.AdminView;
1514
import no.ssb.klass.designer.admin.ContentUseStatView;
1615
import no.ssb.klass.designer.components.BreadcumbPanel.Breadcrumb;
1716
import no.ssb.klass.designer.components.ClassificationListViewSelection;
1817
import no.ssb.klass.designer.service.ClassificationFacade;
19-
import no.ssb.klass.designer.service.KlassLoginService;
2018
import no.ssb.klass.designer.user.UserContext;
2119
import no.ssb.klass.designer.util.ConfirmationDialog;
2220
import no.ssb.klass.designer.util.KlassTheme;
@@ -25,33 +23,30 @@
2523
import org.slf4j.Logger;
2624
import org.slf4j.LoggerFactory;
2725
import org.springframework.beans.factory.annotation.Autowired;
28-
import org.vaadin.spring.security.VaadinSecurity;
2926

3027
import java.util.List;
3128

3229
import static com.google.common.base.Preconditions.checkNotNull;
3330

3431
@SpringUI
35-
@SuppressWarnings("serial")
3632
public class MainView extends MainDesign implements ViewChangeListener {
3733
private static final Logger log = LoggerFactory.getLogger(MainView.class);
3834
private Navigator navigator;
3935

4036
private final SpringViewProvider springViewProvider;
41-
private final KlassLoginService klassLoginService;
42-
private final VaadinSecurity vaadinSecurity;
4337
private final UserContext userContext;
4438

39+
4540
private ConfirmationDialog confirmationDialog;
4641

4742
@Autowired
48-
public MainView(ClassificationFacade classificationFacade, SpringViewProvider springViewProvider,
49-
VaadinSecurity vaadinSecurity, KlassLoginService vaadinLoginService, UserContext userContext) {
43+
public MainView(
44+
ClassificationFacade classificationFacade,
45+
SpringViewProvider springViewProvider,
46+
UserContext userContext
47+
) {
5048
this.springViewProvider = springViewProvider;
51-
this.klassLoginService = vaadinLoginService;
52-
this.vaadinSecurity = vaadinSecurity;
5349
this.userContext = userContext;
54-
verifyUser();
5550
configureNavigator();
5651
MainFilterLogic.configureFilterPanel(selectKodeverk, selectSection, classificationFacade);
5752
configureTopPanel();
@@ -72,7 +67,7 @@ private void configureUserMenu() {
7267
}
7368

7469
private void logout(MenuBar.MenuItem menuItem) {
75-
vaadinSecurity.logout();
70+
// vaadinSecurity.logout();
7671
}
7772

7873
private void createAdminView() {
@@ -83,16 +78,6 @@ private void createInnholdBruksstatistikkView() {
8378
VaadinUtil.navigateTo(ContentUseStatView.NAME);
8479
}
8580

86-
private void verifyUser() {
87-
if (!userContext.hasUser()) {
88-
/* TODO https://statistics-norway.atlassian.net/browse/DPMETA-916
89-
Replace hardcoded user with user info extracted from the token
90-
*/
91-
log.debug("Set User {}", userContext);
92-
userContext.setUser(new User("kno@ssb.no", "Kari Nordmann", "854"));
93-
}
94-
}
95-
9681
public void configureNavigator() {
9782
navigator = new Navigator(UI.getCurrent(), content);
9883
navigator.addViewChangeListener(this);

klass-forvaltning/src/main/java/no/ssb/klass/designer/admin/AdminView.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package no.ssb.klass.designer.admin;
22

3-
import org.springframework.security.access.annotation.Secured;
4-
import org.vaadin.spring.annotation.PrototypeScope;
5-
63
import com.vaadin.navigator.View;
74
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
85
import com.vaadin.spring.annotation.SpringView;
6+
import no.ssb.klass.designer.user.KlassRoles;
7+
import org.springframework.security.access.annotation.Secured;
8+
import org.vaadin.spring.annotation.PrototypeScope;
99

10-
import no.ssb.klass.core.ldap.ActiveDirectoryRoles;
1110

1211
@SuppressWarnings("serial")
1312
@PrototypeScope
1413
@SpringView(name = AdminView.NAME)
15-
@Secured(ActiveDirectoryRoles.KLASS_ADMINISTRATOR)
14+
@Secured(KlassRoles.KLASS_ADMINISTRATOR)
1615
public class AdminView extends AdminDesign implements View {
1716

1817
public static final String NAME = "AdminView";
18+
1919
@Override
2020
public void enter(ViewChangeEvent event) {
2121
sectionsTab.init();

0 commit comments

Comments
 (0)