Skip to content
Snippets Groups Projects
Commit e53fdeea authored by Martin Weise's avatar Martin Weise
Browse files

Verify token at each service in the future, assign token upon requests

Former-commit-id: a1896f53
parent 4eee4437
Branches
Tags
1 merge request!42Fixed the query service tests
Showing with 111 additions and 44 deletions
...@@ -82,7 +82,6 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { ...@@ -82,7 +82,6 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
/* set permissions on endpoints */ /* set permissions on endpoints */
http.authorizeRequests() http.authorizeRequests()
/* our public endpoints */ /* our public endpoints */
// .antMatchers("/api/auth**").permitAll()
.antMatchers(HttpMethod.POST, "/api/user").permitAll() .antMatchers(HttpMethod.POST, "/api/user").permitAll()
.antMatchers(HttpMethod.POST, "/api/auth").permitAll() .antMatchers(HttpMethod.POST, "/api/auth").permitAll()
/* our private endpoints */ /* our private endpoints */
......
...@@ -26,3 +26,4 @@ eureka: ...@@ -26,3 +26,4 @@ eureka:
client.serviceUrl.defaultZone: http://fda-discovery-service:9090/eureka/ client.serviceUrl.defaultZone: http://fda-discovery-service:9090/eureka/
fda: fda:
ready.path: /ready ready.path: /ready
auth.url: http://fda-authentication-service:9097/api/auth
\ No newline at end of file
...@@ -26,3 +26,4 @@ eureka: ...@@ -26,3 +26,4 @@ eureka:
client.serviceUrl.defaultZone: http://localhost:9090/eureka/ client.serviceUrl.defaultZone: http://localhost:9090/eureka/
fda: fda:
ready.path: ./ready ready.path: ./ready
auth.url: http://localhost:9097/api/auth
\ No newline at end of file
package at.tuwien.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import javax.servlet.http.HttpServletResponse;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Value("${fda.auth.url}")
private String authUrl;
@Override
protected void configure(HttpSecurity http) throws Exception {
/* enable CORS and disable CSRF */
http = http.cors().and().csrf().disable();
/* set session management to stateless */
http = http
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and();
/* set unauthorized requests exception handler */
http = http
.exceptionHandling()
.authenticationEntryPoint(
(request, response, ex) -> {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED,
ex.getMessage()
);
}
).and();
/* set permissions on endpoints */
http.authorizeRequests()
/* our private endpoints */
.anyRequest().authenticated();
/* set auth url */
http.formLogin()
.loginProcessingUrl(authUrl);
}
@Bean
public CorsFilter corsFilter() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
final CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("*");
config.addAllowedHeader("*");
config.addAllowedMethod("*");
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
}
package at.tuwien.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(code = HttpStatus.FORBIDDEN, reason = "Persistence error")
public class AuthenticationException extends Exception {
public AuthenticationException(String msg) {
super(msg);
}
public AuthenticationException(String msg, Throwable thr) {
super(msg, thr);
}
public AuthenticationException(Throwable thr) {
super(thr);
}
}
...@@ -70,7 +70,7 @@ public class ContainerServiceImpl implements ContainerService { ...@@ -70,7 +70,7 @@ public class ContainerServiceImpl implements ContainerService {
container.setPort(availableTcpPort); container.setPort(availableTcpPort);
container.setName(createDto.getName()); container.setName(createDto.getName());
container.setInternalName(containerMapper.containerToInternalContainerName(container)); container.setInternalName(containerMapper.containerToInternalContainerName(container));
log.debug("will create host config {} and container {}", hostConfig, container); log.trace("will create host config {} and container {}", hostConfig, container);
/* create the container */ /* create the container */
final CreateContainerResponse response; final CreateContainerResponse response;
try { try {
...@@ -81,8 +81,12 @@ public class ContainerServiceImpl implements ContainerService { ...@@ -81,8 +81,12 @@ public class ContainerServiceImpl implements ContainerService {
.withHostConfig(hostConfig) .withHostConfig(hostConfig)
.exec(); .exec();
} catch (ConflictException e) { } catch (ConflictException e) {
log.error("conflicting names for container {}, reason: {}", createDto, e.getMessage()); log.error("Conflicting names {}", createDto.getName());
throw new DockerClientException("Unexpected behavior", e); throw new DockerClientException("Unexpected behavior", e);
} catch (NotFoundException e) {
log.error("The image {}:{} not available on the container service", createDto.getRepository(), createDto.getTag());
log.debug("payload was {}", createDto);
throw new DockerClientException("Image not available", e);
} }
container.setHash(response.getId()); container.setHash(response.getId());
container = containerRepository.save(container); container = containerRepository.save(container);
......
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
<v-btn <v-btn
class="mr-2 white--text" class="mr-2 white--text"
color="blue-grey" color="blue-grey"
@click="loginDialog = true"> to="/login">
<v-icon left>mdi-login</v-icon> Login <v-icon left>mdi-login</v-icon> Login
</v-btn> </v-btn>
<v-menu bottom offset-y left> <v-menu bottom offset-y left>
...@@ -44,7 +44,7 @@ ...@@ -44,7 +44,7 @@
</v-btn> </v-btn>
</template> </template>
<v-list> <v-list>
<v-list-item @click="registerDialog = true"> <v-list-item to="/signup">
<v-list-item-icon> <v-list-item-icon>
<v-icon left>mdi-account-plus</v-icon> <v-icon left>mdi-account-plus</v-icon>
</v-list-item-icon> </v-list-item-icon>
...@@ -79,18 +79,6 @@ ...@@ -79,18 +79,6 @@
</v-card-text> </v-card-text>
</v-card> </v-card>
</v-footer> </v-footer>
<v-dialog
v-model="loginDialog"
persistent
max-width="640">
<Login @close="loginDialog = false" />
</v-dialog>
<v-dialog
v-model="registerDialog"
persistent
max-width="640">
<Register @close="registerDialog = false" />
</v-dialog>
</v-app> </v-app>
</template> </template>
...@@ -104,20 +92,12 @@ import { ...@@ -104,20 +92,12 @@ import {
mdiNewspaperVariantOutline, mdiNewspaperVariantOutline,
mdiCog mdiCog
} from '@mdi/js' } from '@mdi/js'
import Login from '../components/dialogs/Login'
import Register from '../components/dialogs/Register'
export default { export default {
name: 'DefaultLayout', name: 'DefaultLayout',
components: {
Login,
Register
},
data () { data () {
return { return {
drawer: false, drawer: false,
loginDialog: null,
registerDialog: null,
items: [ items: [
{ {
icon: mdiHome, icon: mdiHome,
......
...@@ -76,6 +76,9 @@ export default { ...@@ -76,6 +76,9 @@ export default {
computed: { computed: {
loadingColor () { loadingColor () {
return this.error ? 'red lighten-2' : 'primary' return this.error ? 'red lighten-2' : 'primary'
},
token () {
return this.$store.state.token
} }
}, },
mounted () { mounted () {
...@@ -86,7 +89,9 @@ export default { ...@@ -86,7 +89,9 @@ export default {
this.createDbDialog = false this.createDbDialog = false
try { try {
this.loading = true this.loading = true
let res = await this.$axios.get('/api/container/') let res = await this.$axios.get('/api/container/', {
headers: { Authorization: `Bearer ${this.token}` }
})
this.containers = res.data this.containers = res.data
console.debug('containers', this.containers) console.debug('containers', this.containers)
for (const container of this.containers) { for (const container of this.containers) {
......
...@@ -30,11 +30,6 @@ ...@@ -30,11 +30,6 @@
</v-card-text> </v-card-text>
<v-card-actions> <v-card-actions>
<v-spacer /> <v-spacer />
<v-btn
class="mb-2"
@click="cancel">
Cancel
</v-btn>
<v-btn <v-btn
id="login" id="login"
class="mb-2" class="mb-2"
...@@ -69,17 +64,14 @@ export default { ...@@ -69,17 +64,14 @@ export default {
beforeMount () { beforeMount () {
}, },
methods: { methods: {
cancel () {
this.$parent.$parent.$parent.$parent.loginDialog = false
},
async login () { async login () {
const url = '/api/auth' const url = '/api/auth'
try { try {
this.loading = true this.loading = true
const res = await this.$axios.post(url, this.loginAccount) const res = await this.$axios.post(url, this.loginAccount)
console.debug('login user', res.data) console.debug('login user', res.data)
this.$store.commit('SET_TOKEN', res.data.token)
this.$toast.success('Welcome back!') this.$toast.success('Welcome back!')
this.cancel()
} catch (err) { } catch (err) {
console.error('login user failed', err) console.error('login user failed', err)
this.$toast.error('Failed to login user') this.$toast.error('Failed to login user')
......
...@@ -101,9 +101,6 @@ export default { ...@@ -101,9 +101,6 @@ export default {
beforeMount () { beforeMount () {
}, },
methods: { methods: {
cancel () {
this.$parent.$parent.$parent.$parent.registerDialog = false
},
async register () { async register () {
const url = '/api/user' const url = '/api/user'
try { try {
...@@ -111,7 +108,6 @@ export default { ...@@ -111,7 +108,6 @@ export default {
const res = await this.$axios.post(url, this.createAccount) const res = await this.$axios.post(url, this.createAccount)
console.debug('create user', res.data) console.debug('create user', res.data)
this.$toast.success('Success. Check your inbox!') this.$toast.success('Success. Check your inbox!')
this.cancel()
} catch (err) { } catch (err) {
console.error('create user failed', err) console.error('create user failed', err)
this.$toast.error('Failed to create user') this.$toast.error('Failed to create user')
......
export const state = () => ({ export const state = () => ({
db: null db: null,
token: null
}) })
export const mutations = { export const mutations = {
SET_DATABASE (state, db) { SET_DATABASE (state, db) {
state.db = db state.db = db
}, },
SET_THEME (state, theme) { SET_TOKEN (state, token) {
state.theme = theme state.token = token
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment