package org.marsik.elshelves.backend.app;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;

@Configuration
@EnableResourceServer
public class ApplicationOauth2Resources extends ResourceServerConfigurerAdapter {
    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        resources
                .resourceId("elshelves");
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                    // Anybody can try to authenticate
                    .antMatchers("/oauth/token").anonymous()

                    // Status and website icon are open
                    .antMatchers("/status", "/favicon.ico").permitAll()

                    // Mail api is verified using HMAC
                    .antMatchers("/mail/**").permitAll()

                    // Schema is needed for clients to start properly
                    .antMatchers("/schema").permitAll()

                    // User registration is open
                    .antMatchers(HttpMethod.POST, "/users").permitAll()

                    // Verification of registration emails is open
                    .antMatchers(HttpMethod.POST, "/users/verify/**").permitAll()

                    // OPTIONS calls must be open for CORS to work properly
                    .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()

                    // Test endpoints are open
                    .antMatchers("/test/**").permitAll()

                    // The rest of the API requires valid token
                    .anyRequest().authenticated();
    }
}

@@ -981,16 +981,99 @@
    http%0A
+%09%09%09%09// Enable anonymous access%0A%09%09%09%09.anonymous()%0A%09%09%09%09.and()%0A%09%09%09%09// Disable sessions%0A
         
@@ -1320,25 +1320,25 @@
 token%22).
-anonymous
+permitAll
 ()%0A%0A    
@@ -2330,22 +2330,27 @@
 t().
-authenticated(
+hasAuthority(%22USER%22
 );%0A 

Fix authorization rules