<commit_before>package de.hska.ld.core.config.security.openidconnect;

import de.hska.ld.core.config.security.AjaxAuthenticationFailureHandler;
import de.hska.ld.core.config.security.AjaxAuthenticationSuccessHandler;
import de.hska.ld.core.config.security.AjaxLogoutSuccessHandler;
import de.hska.ld.core.config.security.FormAuthenticationProvider;
import de.hska.ld.core.persistence.domain.Role;
import de.hska.ld.core.persistence.domain.User;
import de.hska.ld.core.service.RoleService;
import de.hska.ld.core.service.UserService;
import org.mitre.oauth2.service.impl.DefaultClientUserDetailsService;
import org.mitre.openid.connect.client.*;
import org.mitre.openid.connect.client.service.impl.*;
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
import org.mitre.openid.connect.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.security.web.authentication.preauth.AbstractPreAuthenticatedProcessingFilter;
import org.springframework.security.web.util.matcher.RegexRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, proxyTargetClass = true)
public class OIDCSecurityConfig extends WebSecurityConfigurerAdapter {

    // client configuration wiki
    // @see https://github.com/mitreid-connect/OpenID-Connect-Java-Spring-Server/wiki/Client-configuration

    // converted from servlet-context.xml
    // @see https://github.com/mitreid-connect/simple-web-app/blob/master/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml#L400

    // executed 'java -jar json-web-key-generator-0.1-SNAPSHOT-jar-with-dependencies.jar -t RSA -s 1024 -S -i rsa1' to generate keystore
    // @see https://github.com/mitreid-connect/OpenID-Connect-Java-Spring-Server/wiki/Key-generation
    private AuthenticationManager authenticationManager;

    @Autowired
    private HybridIssuerService hybridIssuerService;

    @Autowired
    private DynamicServerConfigurationService dynamicServerConfigurationService;

    @Autowired
    private DynamicRegistrationClientConfigurationService dynamicClientConfigurationService;

    @Autowired
    private StaticAuthRequestOptionsService staticAuthRequestOptionsService;

    @Autowired
    private PlainAuthRequestUrlBuilder plainAuthRequestUrlBuilder;

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private Environment env;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService)
                .passwordEncoder(passwordEncoder());
    }

    @Override
    @SuppressWarnings("unchecked")
    protected void configure(HttpSecurity http) throws Exception {
        OIDCAuthenticationFilter oidcFilter = openIdConnectAuthenticationFilter();
        oidcFilter.setAuthenticationSuccessHandler(new AuthenticationSuccessHandler() {
            @Override
            public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
                response.sendRedirect(env.getProperty("module.core.oidc.redirect.to.client"));
            }
        });
        oidcFilter.setApplicationEventPublisher(new ApplicationEventPublisher() {
            @Override
            public void publishEvent(ApplicationEvent event) {
                Object source = event.getSource();
                OIDCAuthenticationToken token = null;
                if (source != null) {
                    token = (OIDCAuthenticationToken) source;
                }
                if (token != null) {
                    Map map = (Map) token.getPrincipal();
                    Iterator iterator = map.entrySet().iterator();
                    String subId = null;
                    String issuer = null;
                    if (iterator.hasNext()) {
                        Map.Entry<String, String> entry = (Map.Entry<String, String>) iterator.next();
                        if ("sub".equals(entry.getKey())) {
                            // check if sub id is already present in the database
                            subId = entry.getValue();
                            if (subId == null) {
                                throw new UnsupportedOperationException("No subId found!");
                            }
                        }
                    }
                    if (iterator.hasNext()) {
                        Map.Entry<String, String> entry = (Map.Entry<String, String>) iterator.next();
                        if ("iss".equals(entry.getKey())) {
                            issuer = entry.getValue();
                            if (!env.getProperty("module.core.oidc.identity.provider.url").equals(issuer)) {
                                throw new UnsupportedOperationException("Wrong or no issuer found!");
                            }
                        }
                    }

                    User currentUserInDb = userService.findBySubIdAndIssuer(subId, issuer);
                    UserInfo oidcUserInfo = ((OIDCAuthenticationToken) source).getUserInfo();

                    if (currentUserInDb == null && oidcUserInfo != null) {
                        createNewUserFirstLogin(token, subId, issuer, oidcUserInfo);
                    } else if (oidcUserInfo != null) {
                        updateUserInformationFromOIDC(token, currentUserInDb, oidcUserInfo);
                    } else {
                        // oidc information is null
                        throw new UnsupportedOperationException("No OIDC information found!");
                    }
                }
            }

            private void updateUserInformationFromOIDC(OIDCAuthenticationToken token, User currentUserInDb, UserInfo oidcUserInfo) {
                // get the current authentication details of the user
                Authentication auth = SecurityContextHolder.getContext().getAuthentication();
                enrichAuthoritiesWithStoredAuthorities(currentUserInDb, auth);

                // check for profile updates since the last login
                String oidcUpdatedTime = token.getUserInfo().getUpdatedTime();
                // oidc time: "20150701_090039"
                // oidc format: "yyyyMMdd_HHmmss"
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
                try {
                    Date date = sdf.parse(oidcUpdatedTime);
                    if (currentUserInDb.getLastupdatedAt().getTime() > date.getTime()) {
                        User userWithGivenPreferredUserName = userService.findByUsername(oidcUserInfo.getPreferredUsername());
                        int i = 0;
                        if (userWithGivenPreferredUserName != null) {
                            while (userWithGivenPreferredUserName != null) {
                                String prefferedUsername = oidcUserInfo.getPreferredUsername() + "#" + i;
                                userWithGivenPreferredUserName = userService.findByUsername(prefferedUsername);
                            }
                        } else {
                            currentUserInDb.setUsername(oidcUserInfo.getPreferredUsername());
                        }

                        currentUserInDb.setFullName(oidcUserInfo.getName());
                        currentUserInDb.setEmail(oidcUserInfo.getEmail());
                        currentUserInDb = userService.save(currentUserInDb);
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }

            private void createNewUserFirstLogin(OIDCAuthenticationToken token, String subId, String issuer, UserInfo oidcUserInfo) {
                // create a new user
                User user = new User();
                // check for colliding user names (via preferred user name)
                User userWithGivenPreferredUserName = userService.findByUsername(oidcUserInfo.getPreferredUsername());
                int i = 0;
                if (userWithGivenPreferredUserName != null) {
                    while (userWithGivenPreferredUserName != null) {
                        String prefferedUsername = oidcUserInfo.getPreferredUsername() + "#" + i;
                        userWithGivenPreferredUserName = userService.findByUsername(prefferedUsername);
                    }
                } else {
                    user.setUsername(oidcUserInfo.getPreferredUsername());
                }

                user.setFullName(oidcUserInfo.getName());
                user.setEmail(oidcUserInfo.getEmail());
                user.setEnabled(true);
                // apply roles
                List<Role> roleList = new ArrayList<Role>();
                Role userRole = roleService.findByName("ROLE_USER");
                if (userRole == null) {
                    // create initial roles
                    String newUserRoleName = "ROLE_USER";
                    userRole = createNewUserRole(newUserRoleName);
                    String newAdminRoleName = "ROLE_ADMIN";
                    Role adminRole = createNewUserRole(newAdminRoleName);
                    // For the first user add the admin role
                    roleList.add(adminRole);
                } else {
                    roleList.add(userRole);
                }
                user.setRoleList(roleList);
                // A password is required so we set a uuid generated one
                if ("development".equals(env.getProperty("lds.app.instance"))) {
                    user.setPassword("<PASSWORD>");
                } else {
                    user.setPassword(<PASSWORD>());
                }
                user.setSubId(subId);
                user.setIssuer(issuer);
                String oidcUpdatedTime = token.getUserInfo().getUpdatedTime();
                // oidc time: "20150701_090039"<commit_msg>- removed unnecessary code.<commit_after>/*
 *  Code contributed to the Learning Layers project
 *  http://www.learning-layers.eu
 *  Development is partly funded by the FP7 Programme of the European
 *  Commission under Grant Agreement FP7-ICT-318209.
 *  Copyright (c) 2015, Karlsruhe University of Applied Sciences.
 *  For a list of contributors see the AUTHORS file at the top-level directory
 *  of this distribution.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package de.hska.ld.core.config.security.openidconnect;

import de.hska.ld.core.config.security.AjaxAuthenticationFailureHandler;
import de.hska.ld.core.config.security.AjaxAuthenticationSuccessHandler;
import de.hska.ld.core.config.security.AjaxLogoutSuccessHandler;
import de.hska.ld.core.config.security.FormAuthenticationProvider;
import de.hska.ld.core.persistence.domain.Role;
import de.hska.ld.core.persistence.domain.User;
import de.hska.ld.core.service.RoleService;
import de.hska.ld.core.service.UserService;
import org.mitre.oauth2.service.impl.DefaultClientUserDetailsService;
import org.mitre.openid.connect.client.*;
import org.mitre.openid.connect.client.service.impl.*;
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
import org.mitre.openid.connect.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
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.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.security.web.authentication.preauth.AbstractPreAuthenticatedProcessingFilter;
import org.springframework.security.web.util.matcher.RegexRequestMatcher;
import org.springframework.security.web.util.matcher.RequestMatcher;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true, proxyTargetClass = true)
public class OIDCSecurityConfig extends WebSecurityConfigurerAdapter {

    // client configuration wiki
    // @see https://github.com/mitreid-connect/OpenID-Connect-Java-Spring-Server/wiki/Client-configuration

    // converted from servlet-context.xml
    // @see https://github.com/mitreid-connect/simple-web-app/blob/master/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml#L400

    // executed 'java -jar json-web-key-generator-0.1-SNAPSHOT-jar-with-dependencies.jar -t RSA -s 1024 -S -i rsa1' to generate keystore
    // @see https://github.com/mitreid-connect/OpenID-Connect-Java-Spring-Server/wiki/Key-generation
    private AuthenticationManager authenticationManager;

    @Autowired
    private HybridIssuerService hybridIssuerService;

    @Autowired
    private DynamicServerConfigurationService dynamicServerConfigurationService;

    @Autowired
    private DynamicRegistrationClientConfigurationService dynamicClientConfigurationService;

    @Autowired
    private StaticAuthRequestOptionsService staticAuthRequestOptionsService;

    @Autowired
    private PlainAuthRequestUrlBuilder plainAuthRequestUrlBuilder;

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private Environment env;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userService)
                .passwordEncoder(passwordEncoder());
    }

    @Override
    @SuppressWarnings("unchecked")
    protected void configure(HttpSecurity http) throws Exception {
        OIDCAuthenticationFilter oidcFilter = openIdConnectAuthenticationFilter();
        oidcFilter.setAuthenticationSuccessHandler(new AuthenticationSuccessHandler() {
            @Override
            public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
                response.sendRedirect(env.getProperty("module.core.oidc.redirect.to.client"));
            }
        });
        oidcFilter.setApplicationEventPublisher(new ApplicationEventPublisher() {
            @Override
            public void publishEvent(ApplicationEvent event) {
                Object source = event.getSource();
                OIDCAuthenticationToken token = null;
                if (source != null) {
                    token = (OIDCAuthenticationToken) source;
                }
                if (token != null) {
                    Map map = (Map) token.getPrincipal();
                    Iterator iterator = map.entrySet().iterator();
                    String subId = null;
                    String issuer = null;
                    if (iterator.hasNext()) {
                        Map.Entry<String, String> entry = (Map.Entry<String, String>) iterator.next();
                        if ("sub".equals(entry.getKey())) {
                            // check if sub id is already present in the database
                            subId = entry.getValue();
                            if (subId == null) {
                                throw new UnsupportedOperationException("No subId found!");
                            }
                        }
                    }
                    if (iterator.hasNext()) {
                        Map.Entry<String, String> entry = (Map.Entry<String, String>) iterator.next();
                        if ("iss".equals(entry.getKey())) {
                            issuer = entry.getValue();
                            if (!env.getProperty("module.core.oidc.identity.provider.url").equals(issuer)) {
                                throw new UnsupportedOperationException("Wrong or no issuer found!");
                            }
                        }
                    }

                    User currentUserInDb = userService.findBySubIdAndIssuer(subId, issuer);
                    UserInfo oidcUserInfo = ((OIDCAuthenticationToken) source).getUserInfo();

                    if (currentUserInDb == null && oidcUserInfo != null) {
                        createNewUserFirstLogin(token, subId, issuer, oidcUserInfo);
                    } else if (oidcUserInfo != null) {
                        updateUserInformationFromOIDC(token, currentUserInDb, oidcUserInfo);
                    } else {
                        // oidc information is null
                        throw new UnsupportedOperationException("No OIDC information found!");
                    }
                }
            }

            private void updateUserInformationFromOIDC(OIDCAuthenticationToken token, User currentUserInDb, UserInfo oidcUserInfo) {
                // get the current authentication details of the user
                Authentication auth = SecurityContextHolder.getContext().getAuthentication();
                enrichAuthoritiesWithStoredAuthorities(currentUserInDb, auth);

                // check for profile updates since the last login
                String oidcUpdatedTime = token.getUserInfo().getUpdatedTime();
                // oidc time: "20150701_090039"
                // oidc format: "yyyyMMdd_HHmmss"
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
                try {
                    Date date = sdf.parse(oidcUpdatedTime);
                    if (currentUserInDb.getEmail() == null || currentUserInDb.getLastupdatedAt().getTime() > date.getTime()) {
                        currentUserInDb.setFullName(oidcUserInfo.getName());
                        currentUserInDb.setEmail(oidcUserInfo.getEmail());
                        currentUserInDb = userService.save(currentUserInDb);
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }

            private void createNewUserFirstLogin(OIDCAuthenticationToken token, String subId, String issuer, UserInfo oidcUserInfo) {
                // create a new user
                User user = new User();
                // check for colliding user names (via preferred user name)
                User userWithGivenPreferredUserName = userService.findByUsername(oidcUserInfo.getPreferredUsername());
                int i = 0;
                if (userWithGivenPreferredUserName != null) {
                    while (userWithGivenPreferredUserName != null) {
                        String prefferedUsername = oidcUserInfo.getPreferredUsername() + "#" + i;
                        userWithGivenPreferredUserName = userService.findByUsername(prefferedUsername);
                    }
                } else {
                    user.setUsername(oidcUserInfo.getPreferredUsername() + "#" + i);
                }

                user.setFullName(oidcUserInfo.getName());
                user.setEmail(oidcUserInfo.getEmail());
                user.setEnabled(true);
                // apply roles
                List<Role> roleList = new ArrayList<Role>();
                Role userRole = roleService.findByName("ROLE_USER");
                if (userRole == null) {
                    // create initial roles
                    String newUserRoleName = "ROLE_USER";
                    userRole = createNewUserRole(newUserRoleName);
                    String newAdminRoleName = "ROLE_ADMIN";
                    Role adminRole = createNewUserRole(newAdminRoleName);
                    // For the first user add the admin role
                    roleList.add(adminRole);
                } else {
                    roleList.add(userRole);
                }
                user.setRoleList(roleList);
                // A password is required so we set a uuid generated one
                if ("development".equals(env.getProperty("lds.app.instance"))) {
                    user.setPassword("<PASSWORD>");
                } else {
                    user.setPassword(UUID.randomUUID().<PASSWORD>());
                }
                user.setSubId(subId);
                user.setIssuer(issuer);
                String oidcUpdatedTime = token.getUserInfo().getUpdatedTime();
                // oidc time: "20150701_090039"
{'fraction_non_alphanumeric': 0.08381410256410256, 'fraction_numerical': 0.003966346153846154, 'mean_word_length': 2.4813110181311018, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 21, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4268781', 'n_tokens_mistral': 6169, 'n_tokens_neox': 5872, 'n_words': 1158}