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.web.jaxrs;
020
021import org.apache.shiro.SecurityUtils;
022import org.apache.shiro.subject.PrincipalCollection;
023import org.apache.shiro.subject.Subject;
024
025import javax.ws.rs.container.ContainerRequestContext;
026import javax.ws.rs.core.SecurityContext;
027import java.security.Principal;
028
029/**
030 * A Shiro based {@link SecurityContext} that exposes the current Shiro {@link Subject} as a {@link Principal}.
031 * The {@link #isUserInRole(String)} method returns the result of {@link Subject#hasRole(String)}.
032 *
033 * @since 1.4
034 */
035public class ShiroSecurityContext implements SecurityContext {
036
037    private final ContainerRequestContext containerRequestContext;
038    private final SecurityContext originalSecurityContext;
039
040    public ShiroSecurityContext(ContainerRequestContext containerRequestContext) {
041        this.containerRequestContext = containerRequestContext;
042        this.originalSecurityContext = containerRequestContext.getSecurityContext();
043    }
044
045    @Override
046    public Principal getUserPrincipal() {
047
048        Principal result;
049
050        Subject subject = getSubject();
051        PrincipalCollection shiroPrincipals = subject.getPrincipals();
052        if (shiroPrincipals != null) {
053            result = shiroPrincipals.oneByType(Principal.class);
054
055            if (result == null) {
056                result = new ObjectPrincipal(shiroPrincipals.getPrimaryPrincipal());
057            }
058        } else {
059            result = originalSecurityContext.getUserPrincipal();
060        }
061
062        return result;
063    }
064
065    @Override
066    public boolean isUserInRole(String role) {
067        return getSubject().hasRole(role);
068    }
069
070    @Override
071    public boolean isSecure() {
072        return containerRequestContext.getSecurityContext().isSecure();
073    }
074
075    @Override
076    public String getAuthenticationScheme() {
077        return containerRequestContext.getSecurityContext().getAuthenticationScheme();
078    }
079
080    private Subject getSubject() {
081        return SecurityUtils.getSubject();
082    }
083
084
085    /**
086     * Java Principal wrapper around any Shiro Principal object.s
087     */
088    private class ObjectPrincipal implements Principal {
089
090        private Object object;
091
092        ObjectPrincipal(Object object) {
093            this.object = object;
094        }
095
096        public Object getObject() {
097            return object;
098        }
099
100        public String getName() {
101            return getObject().toString();
102        }
103
104        @Override
105        public boolean equals(Object o) {
106            if (this == o) {
107                return true;
108            }
109            if (o == null || getClass() != o.getClass()) {
110                return false;
111            }
112
113            ObjectPrincipal that = (ObjectPrincipal) o;
114
115            return object.equals(that.object);
116
117        }
118
119        public int hashCode() {
120            return object.hashCode();
121        }
122
123        public String toString() {
124            return object.toString();
125        }
126    }
127}