001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019
020package org.apache.isis.core.runtime.authentication.standard;
021
022import org.apache.isis.core.commons.authentication.AuthenticationSession;
023import org.apache.isis.core.commons.config.IsisConfiguration;
024import org.apache.isis.core.runtime.authentication.AuthenticationRequest;
025
026public abstract class AuthenticatorAbstract implements Authenticator {
027
028    private final IsisConfiguration configuration;
029
030    // //////////////////////////////////////////////////////
031    // constructor
032    // //////////////////////////////////////////////////////
033
034    public AuthenticatorAbstract(final IsisConfiguration configuration) {
035        this.configuration = configuration;
036    }
037
038    // //////////////////////////////////////////////////////
039    // init, shutdown
040    // //////////////////////////////////////////////////////
041
042    @Override
043    public void init() {
044        // does nothing.
045    }
046
047    @Override
048    public void shutdown() {
049        // does nothing.
050    }
051
052    // //////////////////////////////////////////////////////
053    // API
054    // //////////////////////////////////////////////////////
055
056    /**
057     * Default implementation returns a {@link SimpleSession}; can be overridden
058     * if required.
059     */
060    @Override
061    public AuthenticationSession authenticate(final AuthenticationRequest request, final String code) {
062        if (!isValid(request)) {
063            return null;
064        }
065        return new SimpleSession(request.getName(), request.getRoles(), code);
066    }
067
068    /**
069     * Whether this {@link Authenticator} is valid in the running context (and
070     * optionally with respect to the provided {@link AuthenticationRequest}).
071     * 
072     * <p>
073     * For example, the <tt>ExplorationAuthenticator</tt> (in the default
074     * runtime) is only available for authentication if running in
075     * <i>exploration mode</i>.
076     * 
077     * <p>
078     * TODO: [ISIS-292] should change visibility to <tt>protected</tt> when remove from the API.
079     */
080    public abstract boolean isValid(AuthenticationRequest request);
081
082    // //////////////////////////////////////////////////////
083    // Injected (via constructor)
084    // //////////////////////////////////////////////////////
085
086    public IsisConfiguration getConfiguration() {
087        return configuration;
088    }
089
090}