AuthToken.java 3.13 KB
Newer Older
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision: 691 $
 * $Date: 2004-12-13 15:06:54 -0300 (Mon, 13 Dec 2004) $
 *
6
 * Copyright (C) 2004-2008 Jive Software. All rights reserved.
7
 *
8 9 10 11 12 13 14 15 16 17 18
 * 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.
19 20
 */

21
package org.jivesoftware.openfire.auth;
22

23
import org.jivesoftware.openfire.user.UserManager;
Gaston Dombiak's avatar
Gaston Dombiak committed
24
import org.jivesoftware.util.JiveGlobals;
25

26 27 28 29 30 31 32 33 34 35
/**
 * A token that proves that a user has successfully authenticated.
 *
 * @author Matt Tucker
 * @see AuthFactory
 */
public class AuthToken {

    private static final long serialVersionUID = 01L;
    private String username;
36
    private String domain;
37
    private Boolean anonymous;
38 39 40

    /**
     * Constucts a new AuthToken with the specified username.
41
     * The username can be either a simple username or a full JID.
42
     *
Gaston Dombiak's avatar
Gaston Dombiak committed
43
     * @param jid the username or bare JID to create an authToken token with.
44
     */
45
    public AuthToken(String jid) {
Gaston Dombiak's avatar
Gaston Dombiak committed
46 47 48 49 50 51
        if (jid == null) {
            this.domain = JiveGlobals.getProperty("xmpp.domain");
            return;
        }
        int index = jid.indexOf("@");
        if (index > -1) {
52 53 54 55 56 57
            this.username = jid.substring(0,index);
            this.domain = jid.substring(index+1);
        } else {
            this.username = jid;
            this.domain = JiveGlobals.getProperty("xmpp.domain");
        }
58 59
    }

60
    public AuthToken(String jid, Boolean anonymous) {
Gaston Dombiak's avatar
Gaston Dombiak committed
61 62
        int index = jid.indexOf("@");
        if (index > -1) {
63 64 65 66 67 68
            this.username = jid.substring(0,index);
            this.domain = jid.substring(index+1);
        } else {
            this.username = jid;
            this.domain = JiveGlobals.getProperty("xmpp.domain");
        }
69 70 71
        this.anonymous = anonymous;
    }

72
    /**
Gaston Dombiak's avatar
Gaston Dombiak committed
73 74
     * Returns the username associated with this AuthToken. A <tt>null</tt> value
     * means that the authenticated user is anonymous.
75
     *
Gaston Dombiak's avatar
Gaston Dombiak committed
76
     * @return the username associated with this AuthToken or null when using an anonymous user.
77 78 79 80 81
     */
    public String getUsername() {
        return username;
    }

82 83 84 85 86 87 88 89 90
    /**
     * Returns the domain associated with this AuthToken.
     *
     * @return the domain associated with this AuthToken.
     */
    public String getDomain() {
        return domain;
    }

91 92 93 94 95 96
    /**
     * Returns true if this AuthToken is the Anonymous auth token.
     *
     * @return true if this token is the anonymous AuthToken.
     */
    public boolean isAnonymous() {
97 98 99 100
        if (anonymous == null) {
            anonymous = username == null || !UserManager.getInstance().isRegisteredUser(username);
        }
        return anonymous;
101 102
    }
}