001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.oauth;
003
004import java.util.Objects;
005
006import org.openstreetmap.josm.tools.CheckParameterUtil;
007
008import oauth.signpost.OAuthConsumer;
009
010/**
011 * An oauth token that has been obtained by JOSM and can be used to authenticate the user on the server.
012 */
013public class OAuthToken {
014
015    /**
016     * Creates an OAuthToken from the token currently managed by the {@link OAuthConsumer}.
017     *
018     * @param consumer the consumer
019     * @return the token
020     */
021    public static OAuthToken createToken(OAuthConsumer consumer) {
022        return new OAuthToken(consumer.getToken(), consumer.getTokenSecret());
023    }
024
025    private final String key;
026    private final String secret;
027
028    /**
029     * Creates a new token
030     *
031     * @param key the token key
032     * @param secret the token secret
033     */
034    public OAuthToken(String key, String secret) {
035        this.key = key;
036        this.secret = secret;
037    }
038
039    /**
040     * Creates a clone of another token
041     *
042     * @param other the other token. Must not be null.
043     * @throws IllegalArgumentException if other is null
044     */
045    public OAuthToken(OAuthToken other) {
046        CheckParameterUtil.ensureParameterNotNull(other, "other");
047        this.key = other.key;
048        this.secret = other.secret;
049    }
050
051    /**
052     * Replies the token key
053     *
054     * @return the token key
055     */
056    public String getKey() {
057        return key;
058    }
059
060    /**
061     * Replies the token secret
062     *
063     * @return the token secret
064     */
065    public String getSecret() {
066        return secret;
067    }
068
069    @Override
070    public int hashCode() {
071        return Objects.hash(key, secret);
072    }
073
074    @Override
075    public boolean equals(Object obj) {
076        if (this == obj) return true;
077        if (obj == null || getClass() != obj.getClass()) return false;
078        OAuthToken that = (OAuthToken) obj;
079        return Objects.equals(key, that.key) &&
080                Objects.equals(secret, that.secret);
081    }
082}