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

package foam.nanos.auth;

import foam.core.Detachable;
import foam.core.FObject;
import foam.core.X;
import foam.dao.DAO;
import foam.dao.ProxyDAO;
import foam.dao.ProxySink;
import foam.dao.Sink;
import foam.mlang.order.Comparator;
import foam.mlang.predicate.Predicate;
import foam.util.Password;
import foam.util.SafetyUtil;

import java.util.Calendar;

public class PasswordHashingDAO
    extends ProxyDAO
{
  public PasswordHashingDAO(X x, DAO delegate) {
    setX(x);
    setDelegate(delegate);
  }

  @Override
  public Sink select_(X x, Sink sink, long skip, long limit, Comparator order, Predicate predicate) {
    if ( sink == null ) sink = new ListSink();
    ProxySink proxy = new ProxySink(x, sink) {
      @Override
      public void put(FObject obj, Detachable sub) {
        super.put(sanitize(obj), sub);
      }
    };
    return super.select_(x, proxy, skip, limit, order, predicate);
  }

  @Override
  public FObject find_(X x, Object id) {
    return sanitize(super.find_(x, id));
  }

  @Override
  public FObject put_(X x, FObject obj) {
    Object id = obj.getProperty("id");
    FObject stored = getDelegate().find(id);
    // hash password if result does not exist or does not have password set
    if ( stored == null || SafetyUtil.isEmpty((String) stored.getProperty("password")) ) {
      obj.setProperty("password", Password.hash((String) obj.getProperty("password")));
      obj.setProperty("previousPassword", null);
      obj.setProperty("passwordLastModified", Calendar.getInstance().getTime());
    } else {
      obj.setProperty("password", stored.getProperty("password"));
      obj.setProperty("previousPassword", stored.getProperty("previousPassword"));
      obj.setProperty("passwordLastModified", stored.getProperty("passwordLastModified"));
    }

    // clone result and return copy with no password parameters
    FObject result = super.put_(x, obj);
    return sanitize(result);
  }

  private FObject sanitize(FObject obj) {
    if ( obj == null ) {
      return null;<commit_msg>Fixed sink decoration<commit_after>/**
 * @license
 * Copyright 2017 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package foam.nanos.auth;

import foam.core.Detachable;
import foam.core.FObject;
import foam.core.X;
import foam.dao.*;
import foam.mlang.order.Comparator;
import foam.mlang.predicate.Predicate;
import foam.util.Password;
import foam.util.SafetyUtil;

import java.util.Calendar;

public class PasswordHashingDAO
    extends ProxyDAO
{
  public PasswordHashingDAO(X x, DAO delegate) {
    setX(x);
    setDelegate(delegate);
  }

  @Override
  public Sink select_(X x, Sink sink, long skip, long limit, Comparator order, Predicate predicate) {
    if ( sink == null ) sink = new ListSink();
    ProxySink proxy = new ProxySink(x, sink) {
      @Override
      public void put(FObject obj, Detachable sub) {
        super.put(sanitize(obj), sub);
      }
    };

    super.select_(x, proxy, skip, limit, order, predicate);
    return sink;
  }

  @Override
  public FObject find_(X x, Object id) {
    return sanitize(super.find_(x, id));
  }

  @Override
  public FObject put_(X x, FObject obj) {
    Object id = obj.getProperty("id");
    FObject stored = getDelegate().find(id);
    // hash password if result does not exist or does not have password set
    if ( stored == null || SafetyUtil.isEmpty((String) stored.getProperty("password")) ) {
      obj.setProperty("password", Password.hash((String) obj.getProperty("password")));
      obj.setProperty("previousPassword", null);
      obj.setProperty("passwordLastModified", Calendar.getInstance().getTime());
    } else {
      obj.setProperty("password", <PASSWORD>("password"));
      obj.setProperty("previousPassword", <PASSWORD>("<PASSWORD>"));
      obj.setProperty("passwordLastModified", stored.getProperty("passwordLastModified"));
    }

    // clone result and return copy with no password parameters
    FObject result = super.put_(x, obj);
    return sanitize(result);
  }

  private FObject sanitize(FObject obj) {
    if ( obj == null ) {
      return null;
{'fraction_non_alphanumeric': 0.12670427832628114, 'fraction_numerical': 0.0028208744710860366, 'mean_word_length': 3.7435897435897436, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '3215213', 'n_tokens_mistral': 1289, 'n_tokens_neox': 1283, 'n_words': 336}