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.openid.connect.sdk;
019
020
021import com.nimbusds.oauth2.sdk.id.Identifier;
022import com.nimbusds.oauth2.sdk.util.StringUtils;
023import net.jcip.annotations.Immutable;
024
025
026/**
027 * Nonce. This is a random, unique string value to associate a user-session 
028 * with an ID Token and to mitigate replay attacks.
029 *
030 * <p>Example generation of a nonce with eight random mixed-case alphanumeric
031 * characters:
032 *
033 * <pre>
034 * Nonce nonce = new Nonce(8);
035 * </pre>
036 *
037 * <p>Related specifications:
038 *
039 * <ul>
040 *     <li>OpenID Connect Core 1.0, section 3.1.2.1. and 15.5.2.
041 * </ul>
042 */
043@Immutable
044public final class Nonce extends Identifier {
045
046
047        /**
048         * Creates a new nonce with the specified value.
049         *
050         * @param value The nonce value. Must not be {@code null} or empty 
051         *              string.
052         */
053        public Nonce(final String value) {
054        
055                super(value);
056        }
057
058
059        /**
060         * Creates a new nonce with a randomly generated value of the specified
061         * byte length, Base64URL-encoded.
062         *
063         * @param byteLength The byte length of the value to generate. Must be
064         *                   greater than one.
065         */
066        public Nonce(final int byteLength) {
067        
068                super(byteLength);
069        }
070        
071        
072        /**
073         * Creates a new nonce with a randomly generated 256-bit (32-byte) 
074         * value, Base64URL-encoded.
075         */
076        public Nonce() {
077
078                super();
079        }
080        
081        
082        @Override
083        public boolean equals(final Object object) {
084        
085                return object instanceof Nonce &&
086                       this.toString().equals(object.toString());
087        }
088        
089        
090        /**
091         * Parses a nonce from the specified string.
092         *
093         * @param s The string to parse, {@code null} or empty if no nonce is
094         *          specified.
095         *
096         * @return The nonce, {@code null} if the parsed string was 
097         *         {@code null} or empty.
098         */
099        public static Nonce parse(final String s) {
100        
101                if (StringUtils.isBlank(s))
102                        return null;
103                
104                return new Nonce(s);
105        }
106}