<commit_before>/*
 * Copyright 2017 Crown Copyright
 *
 * 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 stroom.security.server;

import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import stroom.entity.shared.EntityServiceException;
import stroom.node.server.StroomPropertyService;
import stroom.security.server.exception.AccountExpiredException;
import stroom.security.server.exception.BadCredentialsException;
import stroom.security.server.exception.DisabledException;
import stroom.security.server.exception.LockedException;
import stroom.security.shared.FindUserCriteria;
import stroom.security.shared.PermissionNames;
import stroom.security.shared.UserRef;
import stroom.security.shared.UserStatus;
import stroom.util.cert.CertificateUtil;
import stroom.util.shared.EqualsUtil;

import javax.inject.Inject;
import java.util.regex.Pattern;

@Component
public class DBRealm extends AuthenticatingRealm {
    private static final Logger LOGGER = LoggerFactory.getLogger(DBRealm.class);

    private static final String ADMINISTRATORS = "Administrators";

    private final UserService userService;
    private final UserAppPermissionService userAppPermissionService;
    private final StroomPropertyService stroomPropertyService;

    private String cachedRegex;
    private Pattern cachedPattern;

    private volatile boolean doneCreateOrRefreshAdminRole = false;

    @Inject
    public DBRealm(final UserService userService, final UserAppPermissionService userAppPermissionService,
                   final StroomPropertyService stroomPropertyService,
                   final CredentialsMatcher matcher) {
        super(matcher);
        this.userService = userService;
        this.userAppPermissionService = userAppPermissionService;
        this.stroomPropertyService = stroomPropertyService;
    }

    @Override
    public boolean supports(final AuthenticationToken token) {
        return token != null
                && (token instanceof UsernamePasswordToken || token instanceof CertificateAuthenticationToken);
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(final AuthenticationToken token)
            throws AuthenticationException {
        if (token instanceof JWTAuthenticationToken) {
            final JWTAuthenticationToken jwtAuthenticationToken = (JWTAuthenticationToken) token;
            return authenticateWithJWT(jwtAuthenticationToken);
        }

        if (token instanceof CertificateAuthenticationToken) {
            final CertificateAuthenticationToken certificateAuthenticationToken = (CertificateAuthenticationToken) token;
            return authenticateWithCertificate(certificateAuthenticationToken);
        }

        if (token instanceof UsernamePasswordToken) {<commit_msg>Add JWTAuthenticationToken to supported tokens<commit_after>/*
 * Copyright 2017 Crown Copyright
 *
 * 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 stroom.security.server;

import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import stroom.entity.shared.EntityServiceException;
import stroom.node.server.StroomPropertyService;
import stroom.security.server.exception.AccountExpiredException;
import stroom.security.server.exception.BadCredentialsException;
import stroom.security.server.exception.DisabledException;
import stroom.security.server.exception.LockedException;
import stroom.security.shared.FindUserCriteria;
import stroom.security.shared.PermissionNames;
import stroom.security.shared.UserRef;
import stroom.security.shared.UserStatus;
import stroom.util.cert.CertificateUtil;
import stroom.util.shared.EqualsUtil;

import javax.inject.Inject;
import java.util.regex.Pattern;

@Component
public class DBRealm extends AuthenticatingRealm {
    private static final Logger LOGGER = LoggerFactory.getLogger(DBRealm.class);

    private static final String ADMINISTRATORS = "Administrators";

    private final UserService userService;
    private final UserAppPermissionService userAppPermissionService;
    private final StroomPropertyService stroomPropertyService;

    private String cachedRegex;
    private Pattern cachedPattern;

    private volatile boolean doneCreateOrRefreshAdminRole = false;

    @Inject
    public DBRealm(final UserService userService, final UserAppPermissionService userAppPermissionService,
                   final StroomPropertyService stroomPropertyService,
                   final CredentialsMatcher matcher) {
        super(matcher);
        this.userService = userService;
        this.userAppPermissionService = userAppPermissionService;
        this.stroomPropertyService = stroomPropertyService;
    }

    @Override
    public boolean supports(final AuthenticationToken token) {
        return token != null && (token instanceof UsernamePasswordToken ||
                token instanceof CertificateAuthenticationToken ||
                token instanceof JWTAuthenticationToken);
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(final AuthenticationToken token)
            throws AuthenticationException {
        if (token instanceof JWTAuthenticationToken) {
            final JWTAuthenticationToken jwtAuthenticationToken = (JWTAuthenticationToken) token;
            return authenticateWithJWT(jwtAuthenticationToken);
        }

        if (token instanceof CertificateAuthenticationToken) {
            final CertificateAuthenticationToken certificateAuthenticationToken = (CertificateAuthenticationToken) token;
            return authenticateWithCertificate(certificateAuthenticationToken);
        }

        if (token instanceof UsernamePasswordToken) {
{'fraction_non_alphanumeric': 0.06368772470467386, 'fraction_numerical': 0.0025680534155110425, 'mean_word_length': 4.575518969219757, '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': '1220881', 'n_tokens_mistral': 1896, 'n_tokens_neox': 1858, 'n_words': 507}