001/*
002 * oauth2-oidc-sdk
003 *
004 * Copyright 2012-2016, Connect2id Ltd and contributors.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
007 * this file except in compliance with the License. You may obtain a copy of the
008 * License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software distributed
013 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
014 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
015 * specific language governing permissions and limitations under the License.
016 */
017
018package com.nimbusds.oauth2.sdk.id;
019
020
021import java.io.Serializable;
022import java.security.SecureRandom;
023
024import org.apache.commons.lang3.StringUtils;
025
026import com.nimbusds.jose.util.Base64URL;
027
028import net.minidev.json.JSONAware;
029import net.minidev.json.JSONValue;
030
031
032/**
033 * The base class for representing identifiers and identities. Provides
034 * constructors that generate Base64URL-encoded secure random identifier
035 * values.
036 *
037 * <p>Extending classes must override the {@link #equals} method.
038 */
039public class Identifier implements Serializable, Comparable<Identifier>, JSONAware {
040        
041        
042        /**
043         * The default byte length of generated identifiers.
044         */
045        public static final int DEFAULT_BYTE_LENGTH = 32;
046        
047        
048        /**
049         * The secure random generator.
050         */
051        private static final SecureRandom secureRandom = new SecureRandom();
052
053
054        /**
055         * The identifier value.
056         */
057        private final String value;
058
059
060        /**
061         * Creates a new identifier with the specified value.
062         *
063         * @param value The identifier value. Must not be {@code null} or empty
064         *              string.
065         */
066        public Identifier(final String value) {
067
068                if (StringUtils.isBlank(value))
069                        throw new IllegalArgumentException("The value must not be null or empty string");
070
071                this.value = value;
072        }
073
074
075        /**
076         * Creates a new identifier with a randomly generated value of the 
077         * specified byte length, Base64URL-encoded.
078         *
079         * @param byteLength The byte length of the value to generate. Must be
080         *                   greater than one.
081         */
082        public Identifier(final int byteLength) {
083                
084                if (byteLength < 1)
085                        throw new IllegalArgumentException("The byte length must be a positive integer");
086                
087                byte[] n = new byte[byteLength];
088                
089                secureRandom.nextBytes(n);
090
091                value = Base64URL.encode(n).toString();
092        }
093        
094        
095        /**
096         * Creates a new identifier with a randomly generated 256-bit 
097         * (32-byte) value, Base64URL-encoded.
098         */
099        public Identifier() {
100
101                this(DEFAULT_BYTE_LENGTH);
102        }
103
104
105        /**
106         * Returns the value of this identifier.
107         *
108         * @return The value.
109         */
110        public String getValue() {
111
112                return value;
113        }
114
115
116        /**
117         * Returns the JSON string representation of this identifier.
118         *
119         * @return The JSON string.
120         */
121        @Override
122        public String toJSONString() {
123
124                StringBuilder sb = new StringBuilder("\"");
125                sb.append(JSONValue.escape(value));
126                sb.append('"');
127                return sb.toString();
128        }
129        
130        
131        /**
132         * @see #getValue
133         */
134        @Override
135        public String toString() {
136        
137                return getValue();
138        }
139
140
141        @Override
142        public int compareTo(final Identifier other) {
143
144                return getValue().compareTo(other.getValue());
145        }
146
147
148        @Override
149        public boolean equals(final Object o) {
150                if (this == o) return true;
151                if (o == null || getClass() != o.getClass()) return false;
152
153                Identifier that = (Identifier) o;
154
155                return getValue() != null ? getValue().equals(that.getValue()) : that.getValue() == null;
156
157        }
158
159
160        @Override
161        public int hashCode() {
162                return getValue() != null ? getValue().hashCode() : 0;
163        }
164}