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 net.jcip.annotations.Immutable; 022 023import org.apache.commons.lang3.StringUtils; 024 025 026/** 027 * Opaque value used to maintain state between a request and a callback. Also 028 * serves as a protection against XSRF attacks, among other uses. 029 */ 030@Immutable 031public final class State extends Identifier { 032 033 034 /** 035 * Creates a new state with the specified value. 036 * 037 * @param value The state value. Must not be {@code null} or empty 038 * string. 039 */ 040 public State(final String value) { 041 042 super(value); 043 } 044 045 046 /** 047 * Creates a new state with a randomly generated value of the specified 048 * byte length, Base64URL-encoded. 049 * 050 * @param byteLength The byte length of the value to generate. Must be 051 * greater than one. 052 */ 053 public State(final int byteLength) { 054 055 super(byteLength); 056 } 057 058 059 /** 060 * Creates a new state with a randomly generated 256-bit (32-byte) 061 * value, Base64URL-encoded. 062 */ 063 public State() { 064 065 super(); 066 } 067 068 069 @Override 070 public boolean equals(final Object object) { 071 072 return object instanceof State && 073 this.toString().equals(object.toString()); 074 } 075 076 077 /** 078 * Parses a state from the specified string. 079 * 080 * @param s The string to parse, {@code null} or empty if no state is 081 * specified. 082 * 083 * @return The state, {@code null} if the parsed string was 084 * {@code null} or empty. 085 */ 086 public static State parse(final String s) { 087 088 if (StringUtils.isBlank(s)) 089 return null; 090 091 return new State(s); 092 } 093}