ClearspaceAuthProvider.java 4.92 KB
Newer Older
1 2 3 4
/**
 * $Revision$
 * $Date$
 *
5
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
6
 *
7 8 9 10 11 12 13 14 15 16 17
 * Licensed 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.
18 19 20 21 22
 */
package org.jivesoftware.openfire.clearspace;

import org.jivesoftware.openfire.auth.AuthProvider;
import org.jivesoftware.openfire.auth.UnauthorizedException;
23 24
import org.jivesoftware.openfire.auth.ConnectionException;
import org.jivesoftware.openfire.auth.InternalUnauthenticatedException;
25
import static org.jivesoftware.openfire.clearspace.ClearspaceManager.HttpType.GET;
26
import org.jivesoftware.openfire.net.SASLAuthentication;
27 28
import org.jivesoftware.openfire.user.UserNotFoundException;
import org.xmpp.packet.JID;
29 30

/**
Gabriel Guardincerri's avatar
Gabriel Guardincerri committed
31 32 33 34
 * The ClearspaceAuthProvider uses the PermissionService web service inside of Clearspace
 * to retrieve authenticate users. It current version of Clearspace only supports plain authentication.
 *
 * @author Gabriel Guardincerri
35 36
 */
public class ClearspaceAuthProvider implements AuthProvider {
Gabriel Guardincerri's avatar
Gabriel Guardincerri committed
37 38

    // Service url prefix
39 40 41
    protected static final String URL_PREFIX = "permissionService/";

    public ClearspaceAuthProvider() {
42 43
        // Add SASL mechanism for use with Clearspace's group chat integration
        SASLAuthentication.addSupportedMechanism("CLEARSPACE");
44 45
    }

Gabriel Guardincerri's avatar
Gabriel Guardincerri committed
46 47 48 49 50
    /**
     * Clearspace currently supports only plain authentication.
     *
     * @return true
     */
51 52 53 54
    public boolean isPlainSupported() {
        return true;
    }

Gabriel Guardincerri's avatar
Gabriel Guardincerri committed
55 56 57 58 59
    /**
     * Clearspace currently doesn't support digest authentication.
     *
     * @return false
     */
60
    public boolean isDigestSupported() {
61
        return false;
62 63
    }

Gabriel Guardincerri's avatar
Gabriel Guardincerri committed
64
    /**
65
     * Authenticates the user using permissionService/authenticate service of Clearspace.
Gabriel Guardincerri's avatar
Gabriel Guardincerri committed
66 67 68 69 70 71
     * Throws an UnauthorizedException if the user or password are incorrect.
     *
     * @param username the username.
     * @param password the password.
     * @throws UnauthorizedException if the username of password are incorrect.
     */
72 73
    public void authenticate(String username, String password) throws UnauthorizedException,
            ConnectionException, InternalUnauthenticatedException {
74
        try {
75 76
            // Un-escape username.
            username = JID.unescapeNode(username);
77 78
            // Encode potentially non-ASCII characters
            username = URLUTF8Encoder.encode(username);
79
            String path = URL_PREFIX + "authenticate/" + username + "/" + password;
80
            ClearspaceManager.getInstance().executeRequest(GET, path);
81 82
        } catch (UnauthorizedException ue) {
            throw ue;
83 84 85 86 87 88
        } catch (org.jivesoftware.openfire.clearspace.ConnectionException e) {
            if (e.getErrorType() == org.jivesoftware.openfire.clearspace.ConnectionException.ErrorType.AUTHENTICATION) {
                throw new InternalUnauthenticatedException("Bad credentials to use Clearspace webservices", e);
            } else {
                throw new ConnectionException("Error connection to Clearspace webservices", e);
            }
89 90
        } catch (Exception e) {
            // It is not supported exception, wrap it into an UnsupportedOperationException
91
            throw new UnauthorizedException("Unexpected error", e);
92
        }
93 94
    }

Gabriel Guardincerri's avatar
Gabriel Guardincerri committed
95 96 97 98 99 100 101 102 103
    /**
     * This method is not supported.
     *
     * @param username the username
     * @param token    the token
     * @param digest   the digest
     * @throws UnauthorizedException         never throws it
     * @throws UnsupportedOperationException always throws it
     */
104
    public void authenticate(String username, String token, String digest) throws UnauthorizedException {
105
        throw new UnsupportedOperationException("Digest not supported");
106 107
    }

Gabriel Guardincerri's avatar
Gabriel Guardincerri committed
108 109 110 111 112
    /**
     * This method is not supported.
     *
     * @throws UnsupportedOperationException always throws it
     */
113
    public String getPassword(String username) throws UserNotFoundException, UnsupportedOperationException {
114
        throw new UnsupportedOperationException("Password retrieval not supported");
115 116
    }

Gabriel Guardincerri's avatar
Gabriel Guardincerri committed
117 118 119 120 121
    /**
     * This method is not supported.
     *
     * @throws UnsupportedOperationException always throws it
     */
122
    public void setPassword(String username, String password) throws UserNotFoundException, UnsupportedOperationException {
123
        throw new UnsupportedOperationException("Change Password not supported");
124 125
    }

Gabriel Guardincerri's avatar
Gabriel Guardincerri committed
126 127 128 129 130
    /**
     * This method is not supported.
     *
     * @throws UnsupportedOperationException always throws it
     */
131 132 133 134
    public boolean supportsPasswordRetrieval() {
        return false;
    }
}