001/*
002 * Copyright 2025 The Apache Software Foundation.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.apache.wiki.security;
017
018import jakarta.servlet.http.HttpServletRequest;
019import jakarta.servlet.jsp.PageContext;
020import java.util.Enumeration;
021import org.apache.wiki.api.core.Context;
022import org.apache.wiki.api.core.Session;
023import org.apache.wiki.event.WikiEvent;
024import org.apache.wiki.event.WikiSecurityEvent;
025
026/**
027 * A utility class to append audit log attributes to a WikiEvent
028 *
029 * @since 3.0.0
030 */
031public final class EventUtil {
032
033    private EventUtil() {
034    }
035
036    public static WikiEvent applyFrom(WikiSecurityEvent event) {
037        if (event.getTarget() != null && event.getTarget() instanceof Session session) {
038            applyFrom(event, session);
039        }
040        if (event.getSrc() != null && event.getSrc() instanceof Session session) {
041            applyFrom(event, session);
042        }
043
044        return event;
045    }
046
047    public static WikiEvent applyFrom(WikiEvent event, PageContext request) {
048        if (request.getRequest() != null) {
049            applyFrom(event, (HttpServletRequest) request.getRequest());
050        }
051
052        return event;
053    }
054
055    public static WikiEvent applyFrom(WikiEvent event, Session request) {
056        if (request == null) {
057            return event;
058        }
059        if (request.getUserPrincipal() != null) {
060            event.getAttributes().put("getUserPrincipal", request.getUserPrincipal().getName());
061        }
062        event.getAttributes().put("getLoginPrincipal", request.getLoginPrincipal().getName());
063        event.getAttributes().put("getStatus", request.getStatus());
064        if (request.getSubject() != null) {
065            event.getAttributes().put("getSubject", request.getSubject().toString());
066        }
067
068        return event;
069    }
070
071    public static WikiEvent applyFrom(WikiEvent event, Context request) {
072        if (request == null) {
073            return event;
074        }
075        if (request.getHttpRequest() != null) {
076            applyFrom(event, request.getHttpRequest());
077        }
078        return event;
079    }
080
081    public static WikiEvent applyFrom(WikiEvent event, HttpServletRequest request) {
082        if (request == null) {
083            return event;
084        }
085        if (event instanceof WikiSecurityEvent e) {
086            if (e.getTarget() != null && e.getTarget() instanceof Session session) {
087                applyFrom(event, session);
088            }
089        }
090        if (event.getSrc() != null && event.getSrc() instanceof Session session) {
091            applyFrom(event, session);
092        }
093
094        if (request.getUserPrincipal() != null) {
095            event.getAttributes().put("username", request.getUserPrincipal().getName());
096        }
097        event.getAttributes().put("RemoteAddr", request.getRemoteAddr());
098        event.getAttributes().put("RemoteHost", request.getRemoteHost());
099        event.getAttributes().put("RemoteUser", request.getRemoteUser());
100        event.getAttributes().put("RequestURL", request.getRequestURL());
101        event.getAttributes().put("AuthType", request.getAuthType());
102        event.getAttributes().put("Method", request.getMethod());
103        event.getAttributes().put("SessionId", request.getSession().getId());
104        event.getAttributes().put("sessionCreatedAt", request.getSession().getCreationTime());
105        event.getAttributes().put("sesionLastAccessAt", request.getSession().getLastAccessedTime());
106        Enumeration<String> it = request.getHeaderNames();
107        if (it != null) {
108            while (it.hasMoreElements()) {
109                String h = it.nextElement();
110                event.getAttributes().put(h, request.getHeader(h));
111            }
112        }
113        return event;
114    }
115}