/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.sling.extensions.webconsolesecurityprovider.internal;
import java.util.Iterator;
import javax.jcr.Credentials;
import javax.jcr.LoginException;
import javax.jcr.Repository;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import org.apache.jackrabbit.api.JackrabbitSession;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.Group;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.jackrabbit.api.security.user.UserManager;
/**
* The SlingWebConsoleSecurityProvider is security provider for the
* Apache Felix Web Console which validates the user name and password by loging
* into the repository and the checking whether the user is allowed access.
* Access granted by the {@link #authenticate(String, String)} method applies to
* all of the Web Console since the {@link #authorize(Object, String)} method
* always returns true.
*
* This security provider requires a JCR Repository to operate. Therefore it is * only registered as a security provider service once such a JCR Repository is * available. */ public class SlingWebConsoleSecurityProvider extends AbstractWebConsoleSecurityProvider { private Repository repository; public SlingWebConsoleSecurityProvider(final Object repository) { this.repository = (Repository)repository; } // ---------- SCR integration /** * Authenticates and authorizes the user identified by the user name and * password. The check applied to authorize access consists of the following * steps: *
* If the user name and password cannot be used to login to the default
* workspace of the repository or if the user neither one of the configured
* set of granted users or is not a member of the configured set of groups
* access is denied to the Web Console.
*
* @param userName The name of the user to grant access for
* @param password The password to authenticate the user. This may be
* null to assume an empty password.
* @return The userName is currently returned to indicate
* successfull authentication.
* @throws NullPointerException if userName is
* null.
*/
@Override
public Object authenticate(String userName, String password) {
final Credentials creds = new SimpleCredentials(userName,
(password == null) ? new char[0] : password.toCharArray());
Session session = null;
try {
session = repository.login(creds);
if (session instanceof JackrabbitSession) {
UserManager umgr = ((JackrabbitSession) session).getUserManager();
String userId = session.getUserID();
Authorizable a = umgr.getAuthorizable(userId);
if (a instanceof User) {
// check users
if (users.contains(userId)) {
return true;
}
// check groups
Iterator