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