/*-
 * ========================LICENSE_START=================================
 * restheart-core
 * %%
 * Copyright (C) 2014 - 2020 SoftInstigate
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * =========================LICENSE_END==================================
 */
package org.restheart.security.handlers;

import io.undertow.security.api.AuthenticationMode;
import io.undertow.security.api.SecurityContext;
import io.undertow.security.idm.IdentityManager;
import io.undertow.security.impl.SecurityContextFactoryImpl;
import io.undertow.server.HttpServerExchange;
import java.security.AccessController;
import java.security.PrivilegedAction;
import org.restheart.handlers.PipelinedHandler;

/**
 * This is the PipelinedHandler version of
 * io.undertow.security.handlers.SecurityInitialHandler the security handler
 * responsible for attaching the SecurityContext to the current
 * {@link HttpServerExchange}.
 *
 * This handler is called early in the processing of the incoming request,
 * subsequently supported authentication mechanisms will be added to the
 * context, a decision will then be made if authentication is required or
 * optional and the associated mechanisms will be called.
 *
 * @author Andrea Di Cesare {@literal <andrea@softinstigate.com>}
 */
@SuppressWarnings("deprecation")
public class SecurityInitialHandler extends PipelinedHandler {

    static void setSecurityContext(final HttpServerExchange exchange, final SecurityContext securityContext) {
        if (System.getSecurityManager() == null) {
            exchange.setSecurityContext(securityContext);
        } else {
            AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
                exchange.setSecurityContext(securityContext);
                return null;
            });
        }
    }

    private final AuthenticationMode authenticationMode;
    private final String programaticMechName;
    private final SecurityContextFactoryImpl contextFactory;

    public SecurityInitialHandler(final AuthenticationMode authenticationMode,
            final String programaticMechName,
            final SecurityContextFactoryImpl contextFactory,
            final PipelinedHandler next) {
        super(next);
        this.authenticationMode = authenticationMode;
        this.programaticMechName = programaticMechName;
        this.contextFactory = contextFactory;
    }

    public SecurityInitialHandler(final AuthenticationMode authenticationMode,
            final IdentityManager identityManager,
            final String programaticMechName,
            final PipelinedHandler next) {
        this(authenticationMode,
                programaticMechName,
                (SecurityContextFactoryImpl) SecurityContextFactoryImpl.INSTANCE,
                next);
    }

    public SecurityInitialHandler(final AuthenticationMode authenticationMode, final PipelinedHandler next) {
        this(authenticationMode, null, (SecurityContextFactoryImpl) SecurityContextFactoryImpl.INSTANCE, next);
    }

    @Override
    public void handleRequest(HttpServerExchange exchange) throws Exception {
        var newContext = this.contextFactory.createSecurityContext(exchange, authenticationMode, null, programaticMechName);

        setSecurityContext(exchange, newContext);
        next(exchange);
    }
}

@@ -1185,86 +1185,8 @@
 ge;%0A
-import java.security.AccessController;%0Aimport java.security.PrivilegedAction;%0A
 impo
@@ -1229,16 +1229,16 @@
 andler;%0A
+
 %0A/**%0A * 
@@ -1809,41 +1809,8 @@
  */%0A
-@SuppressWarnings(%22deprecation%22)%0A
 publ
@@ -1872,447 +1872,8 @@
 r %7B%0A
-%0A    static void setSecurityContext(final HttpServerExchange exchange, final SecurityContext securityContext) %7B%0A        if (System.getSecurityManager() == null) %7B%0A            exchange.setSecurityContext(securityContext);%0A        %7D else %7B%0A            AccessController.doPrivileged((PrivilegedAction%3CObject%3E) () -%3E %7B%0A                exchange.setSecurityContext(securityContext);%0A                return null;%0A            %7D);%0A        %7D%0A    %7D%0A%0A
     
@@ -3180,25 +3180,36 @@
     
-var new
+exchange.setSecurity
 Context
- = 
+(
 this
@@ -3306,58 +3306,8 @@
 ame)
-;%0A%0A        setSecurityContext(exchange, newContext
 );%0A 

Remove use of SecurityManager because deprecated in Java 17