001package com.nimbusds.oauth2.sdk.id; 002 003import net.jcip.annotations.Immutable; 004 005import org.apache.commons.lang3.StringUtils; 006 007 008/** 009 * Opaque value used to maintain state between a request and a callback. Also 010 * serves as a protection against XSRF attacks, among other uses. This class is 011 * immutable. 012 * 013 * @author Vladimir Dzhuvinov 014 */ 015@Immutable 016public final class State extends Identifier { 017 018 019 /** 020 * Creates a new state with the specified value. 021 * 022 * @param value The state value. Must not be {@code null} or empty 023 * string. 024 */ 025 public State(final String value) { 026 027 super(value); 028 } 029 030 031 /** 032 * Creates a new state with a randomly generated value of the specified 033 * byte length, Base64URL-encoded. 034 * 035 * @param byteLength The byte length of the value to generate. Must be 036 * greater than one. 037 */ 038 public State(final int byteLength) { 039 040 super(byteLength); 041 } 042 043 044 /** 045 * Creates a new state with a randomly generated 256-bit (32-byte) 046 * value, Base64URL-encoded. 047 */ 048 public State() { 049 050 super(); 051 } 052 053 054 @Override 055 public boolean equals(final Object object) { 056 057 return object instanceof State && 058 this.toString().equals(object.toString()); 059 } 060 061 062 /** 063 * Parses a state from the specified string. 064 * 065 * @param s The string to parse, {@code null} or empty if no state is 066 * specified. 067 * 068 * @return The state, {@code null} if the parsed string was 069 * {@code null} or empty. 070 */ 071 public static State parse(final String s) { 072 073 if (StringUtils.isBlank(s)) 074 return null; 075 076 return new State(s); 077 } 078}