001    package com.nimbusds.openid.connect.sdk;
002    
003    
004    import net.jcip.annotations.Immutable;
005    
006    import com.nimbusds.oauth2.sdk.id.Identifier;
007    
008    import com.nimbusds.oauth2.sdk.util.StringUtils;
009    
010    
011    /**
012     * Nonce. This is a random, unique string value to associate a user-session 
013     * with an ID Token and to mitigate replay attacks. This class is immutable.
014     *
015     * <p>Example generation of a nonce with eight random mixed-case alphanumeric
016     * characters:
017     *
018     * <pre>
019     * Nonce nonce = new Nonce(8);
020     * </pre>
021     *
022     * <p>Related specifications:
023     *
024     * <ul>
025     *     <li>OpenID Connect Messages 1.0, section 2.1.1 and 2.1.2.1.
026     * </ul>
027     *
028     * @author Vladimir Dzhuvinov
029     * @version $version$ (2013-01-21)
030     */
031    @Immutable
032    public final class Nonce extends Identifier {
033    
034    
035            /**
036             * Creates a new nonce with the specified value.
037             *
038             * @param value The nonce value. Must not be {@code null} or empty 
039             *              string.
040             */
041            public Nonce(final String value) {
042            
043                    super(value);
044            }
045    
046    
047            /**
048             * Creates a new nonce with a randomly generated value of the specified
049             * length. The value will be made up of mixed-case alphanumeric ASCII 
050             * characters.
051             *
052             * @param length The number of characters. Must be a positive integer.
053             */
054            public Nonce(final int length) {
055            
056                    super(length);
057            }
058            
059            
060            /**
061             * Creates a new nonce with a randomly generated value. The value will
062             * be made up of 32 mixed-case alphanumeric ASCII characters.
063             */
064            public Nonce() {
065    
066                    super();
067            }
068            
069            
070            @Override
071            public boolean equals(final Object object) {
072            
073                    return object != null && 
074                           object instanceof Nonce && 
075                           this.toString().equals(object.toString());
076            }
077            
078            
079            /**
080             * Parses a nonce from the specified string.
081             *
082             * @param s The string to parse, {@code null} or empty if no nonce is
083             *          specified.
084             *
085             * @return The nonce, {@code null} if the parsed string was 
086             *         {@code null} or empty.
087             */
088            public static Nonce parse(final String s) {
089            
090                    if (StringUtils.isUndefined(s))
091                            return null;
092                    
093                    return new Nonce(s);
094            }
095    }