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.cas;
020
021import org.apache.shiro.authc.AuthenticationToken;
022import org.apache.shiro.subject.Subject;
023import org.apache.shiro.subject.SubjectContext;
024import org.apache.shiro.web.mgt.DefaultWebSubjectFactory;
025
026/**
027 * {@link org.apache.shiro.mgt.SubjectFactory Subject} implementation to be used in CAS-enabled applications.
028 *
029 * @since 1.2
030 */
031public class CasSubjectFactory extends DefaultWebSubjectFactory {
032
033    @Override
034    public Subject createSubject(SubjectContext context) {
035
036        //the authenticated flag is only set by the SecurityManager after a successful authentication attempt.
037        boolean authenticated = context.isAuthenticated();
038
039        //although the SecurityManager 'sees' the submission as a successful authentication, in reality, the
040        //login might have been just a CAS rememberMe login.  If so, set the authenticated flag appropriately:
041        if (authenticated) {
042
043            AuthenticationToken token = context.getAuthenticationToken();
044
045            if (token != null && token instanceof CasToken) {
046                CasToken casToken = (CasToken) token;
047                // set the authenticated flag of the context to true only if the CAS subject is not in a remember me mode
048                if (casToken.isRememberMe()) {
049                    context.setAuthenticated(false);
050                }
051            }
052        }
053
054        return super.createSubject(context);
055    }
056}