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 */
019package org.apache.shiro.realm;
020
021import org.apache.shiro.authc.AuthenticationException;
022import org.apache.shiro.authc.AuthenticationInfo;
023import org.apache.shiro.authc.AuthenticationToken;
024
025/**
026 * A <tt>Realm</tt> is a security component that can access application-specific security entities
027 * such as users, roles, and permissions to determine authentication and authorization operations.
028 *
029 * <p><tt>Realm</tt>s usually have a 1-to-1 correspondence with a datasource such as a relational database,
030 * file system, or other similar resource.  As such, implementations of this interface use datasource-specific APIs to
031 * determine authorization data (roles, permissions, etc), such as JDBC, File IO, Hibernate or JPA, or any other
032 * Data Access API.  They are essentially security-specific
033 * <a href="http://en.wikipedia.org/wiki/Data_Access_Object" target="_blank">DAO</a>s.
034 *
035 * <p>Because most of these datasources usually contain Subject (a.k.a. User) information such as usernames and
036 * passwords, a Realm can act as a pluggable authentication module in a
037 * <a href="http://en.wikipedia.org/wiki/Pluggable_Authentication_Modules">PAM</a> configuration.  This allows a Realm to
038 * perform <i>both</i> authentication and authorization duties for a single datasource, which caters to the large
039 * majority of applications.  If for some reason you don't want your Realm implementation to perform authentication
040 * duties, you should override the {@link #supports(org.apache.shiro.authc.AuthenticationToken)} method to always
041 * return <tt>false</tt>.
042 *
043 * <p>Because every application is different, security data such as users and roles can be
044 * represented in any number of ways.  Shiro tries to maintain a non-intrusive development philosophy whenever
045 * possible - it does not require you to implement or extend any <tt>User</tt>, <tt>Group</tt> or <tt>Role</tt>
046 * interfaces or classes.
047 *
048 * <p>Instead, Shiro allows applications to implement this interface to access environment-specific datasources
049 * and data model objects.  The implementation can then be plugged in to the application's Shiro configuration.
050 * This modular technique abstracts away any environment/modeling details and allows Shiro to be deployed in
051 * practically any application environment.
052 *
053 * <p>Most users will not implement the <tt>Realm</tt> interface directly, but will extend one of the subclasses,
054 * {@link org.apache.shiro.realm.AuthenticatingRealm AuthenticatingRealm} or {@link org.apache.shiro.realm.AuthorizingRealm}, greatly reducing the effort requird
055 * to implement a <tt>Realm</tt> from scratch.</p>
056 *
057 * @see org.apache.shiro.realm.CachingRealm CachingRealm
058 * @see org.apache.shiro.realm.AuthenticatingRealm AuthenticatingRealm
059 * @see org.apache.shiro.realm.AuthorizingRealm AuthorizingRealm
060 * @see org.apache.shiro.authc.pam.ModularRealmAuthenticator ModularRealmAuthenticator
061 * @since 0.1
062 */
063public interface Realm {
064
065    /**
066     * Returns the (application-unique) name assigned to this <code>Realm</code>. All realms configured for a single
067     * application must have a unique name.
068     *
069     * @return the (application-unique) name assigned to this <code>Realm</code>.
070     */
071    String getName();
072
073    /**
074     * Returns <tt>true</tt> if this realm wishes to authenticate the Subject represented by the given
075     * {@link org.apache.shiro.authc.AuthenticationToken AuthenticationToken} instance, <tt>false</tt> otherwise.
076     *
077     * <p>If this method returns <tt>false</tt>, it will not be called to authenticate the Subject represented by
078     * the token - more specifically, a <tt>false</tt> return value means this Realm instance's
079     * {@link #getAuthenticationInfo} method will not be invoked for that token.
080     *
081     * @param token the AuthenticationToken submitted for the authentication attempt
082     * @return <tt>true</tt> if this realm can/will authenticate Subjects represented by specified token,
083     *         <tt>false</tt> otherwise.
084     */
085    boolean supports(AuthenticationToken token);
086
087    /**
088     * Returns an account's authentication-specific information for the specified <tt>token</tt>,
089     * or <tt>null</tt> if no account could be found based on the <tt>token</tt>.
090     *
091     * <p>This method effectively represents a login attempt for the corresponding user with the underlying EIS datasource.
092     * Most implementations merely just need to lookup and return the account data only (as the method name implies)
093     * and let Shiro do the rest, but implementations may of course perform eis specific login operations if so
094     * desired.
095     *
096     * @param token the application-specific representation of an account principal and credentials.
097     * @return the authentication information for the account associated with the specified <tt>token</tt>,
098     *         or <tt>null</tt> if no account could be found.
099     * @throws org.apache.shiro.authc.AuthenticationException
100     *          if there is an error obtaining or constructing an AuthenticationInfo object based on the
101     *          specified <tt>token</tt> or implementation-specific login behavior fails.
102     */
103    AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException;
104
105}