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