<commit_before>/**
 * @license
 * Copyright 2017 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package foam.box;

import foam.core.X;
import foam.dao.DAO;
import foam.nanos.app.AppConfig;
import foam.nanos.auth.AuthService;
import foam.nanos.auth.*;
import foam.nanos.boot.NSpec;
import foam.nanos.logger.*;
import foam.nanos.session.Session;
import java.util.Date;
import javax.naming.NoPermissionException;
import javax.servlet.http.HttpServletRequest;

public class SessionServerBox
  extends ProxyBox
{
  protected boolean authenticate_;

  public SessionServerBox(X x, Box delegate, boolean authenticate) {
    super(x, delegate);
    authenticate_ = authenticate;
  }

  public void send(Message msg) {
    String sessionID = (String) msg.getAttributes().get("sessionId");
    Logger logger    = (Logger) getX().get("logger");

    try {
      if ( sessionID != null ) {
        NSpec              spec       = getX().get(NSpec.class);
        HttpServletRequest req        = getX().get(HttpServletRequest.class);
        AuthService        auth       = (AuthService) getX().get("auth");
        DAO                sessionDAO = (DAO)         getX().get("localSessionDAO");
        DAO                groupDAO   = (DAO)         getX().get("groupDAO");
        Session            session    = (Session)     sessionDAO.find(sessionID);

        if ( session == null ) {
          session = new Session();
          session.setId(sessionID);
          session.setRemoteHost(req.getRemoteHost());
          session.setContext(getX().put(Session.class, session));
          sessionDAO.put(session);
        } else {
          // Check that the remoteHost hasn't changed?
          if ( ! session.getRemoteHost().equals(req.getRemoteHost()) ) {
            logger.warning("Attempt to use session create for ", session.getRemoteHost(), " from ", req.getRemoteHost());
            throw new RuntimeException("Attempt to use session from invalid host.");
          }
        }

        User user = (User) session.getContext().get("user");
        X    x    = session.getContext()
          .put(
            "logger",
            new PrefixLogger(
                new Object[] { user == null ? "" : user.getId() + " - " + user.label(), "[Service]", spec.getName() },
                (Logger) session.getContext().get("logger")))
          .put(HttpServletRequest.class, req);

        session.touch();

        if ( authenticate_ && session.getUserId() == 0 ) {
          msg.replyWithException(new AuthenticationException());
          return;
        }

        if ( user != null ) {
          Group group = (Group) groupDAO.find(user.getGroup());

          if ( authenticate_ && ! auth.check(session.getContext(), "service." + spec.getName()) ) {
            logger.debug("missing permission", group != null ? group.getId() : "NO GROUP" , "service." + spec.getName());
            // msg.replyWithException(new NoPermissionException("No permission"));
            // return;
          }<commit_msg>Now logout session if used from new host rather than throw exception.<commit_after>/**
 * @license
 * Copyright 2017 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package foam.box;

import foam.core.X;
import foam.dao.DAO;
import foam.nanos.app.AppConfig;
import foam.nanos.auth.AuthService;
import foam.nanos.auth.*;
import foam.nanos.boot.NSpec;
import foam.nanos.logger.*;
import foam.nanos.session.Session;
import java.util.Date;
import javax.naming.NoPermissionException;
import javax.servlet.http.HttpServletRequest;

public class SessionServerBox
  extends ProxyBox
{
  protected boolean authenticate_;

  public SessionServerBox(X x, Box delegate, boolean authenticate) {
    super(x, delegate);
    authenticate_ = authenticate;
  }

  public void send(Message msg) {
    String sessionID = (String) msg.getAttributes().get("sessionId");
    Logger logger    = (Logger) getX().get("logger");

    try {
      if ( sessionID != null ) {
        NSpec              spec       = getX().get(NSpec.class);
        HttpServletRequest req        = getX().get(HttpServletRequest.class);
        AuthService        auth       = (AuthService) getX().get("auth");
        DAO                sessionDAO = (DAO)         getX().get("localSessionDAO");
        DAO                groupDAO   = (DAO)         getX().get("groupDAO");
        Session            session    = (Session)     sessionDAO.find(sessionID);

        if ( session == null ) {
          session = new Session();
          session.setId(sessionID);
          session.setRemoteHost(req.getRemoteHost());
          session.setContext(getX().put(Session.class, session));
          sessionDAO.put(session);
        } else if ( ! session.getRemoteHost().equals(req.getRemoteHost()) ) {
          // If an existing session is reused with a different remote host then
          // logout the session and force a re-login.
          logger.warning("Attempt to use session create for ", session.getRemoteHost(), " from ", req.getRemoteHost());
          session.setContext(getX().put(Session.class, session));
          session.setRemoteHost(req.getRemoteHost());
          sessionDAO.put(session);
        }

        User user = (User) session.getContext().get("user");
        X    x    = session.getContext()
          .put(
            "logger",
            new PrefixLogger(
                new Object[] { user == null ? "" : user.getId() + " - " + user.label(), "[Service]", spec.getName() },
                (Logger) session.getContext().get("logger")))
          .put(HttpServletRequest.class, req);

        session.touch();

        if ( authenticate_ && session.getUserId() == 0 ) {
          msg.replyWithException(new AuthenticationException());
          return;
        }

        if ( user != null ) {
          Group group = (Group) groupDAO.find(user.getGroup());

          if ( authenticate_ && ! auth.check(session.getContext(), "service." + spec.getName()) ) {
            logger.debug("missing permission", group != null ? group.getId() : "NO GROUP" , "service." + spec.getName());
            // msg.replyWithException(new NoPermissionException("No permission"));
            // return;
          }
{'fraction_non_alphanumeric': 0.13491936771515248, 'fraction_numerical': 0.002235350471020278, 'mean_word_length': 2.3659322944653414, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5881112', 'n_tokens_mistral': 1746, 'n_tokens_neox': 1712, 'n_words': 391}