001/* 002 * oauth2-oidc-sdk 003 * 004 * Copyright 2012-2020, 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.ciba; 019 020 021import java.util.regex.Pattern; 022 023import net.jcip.annotations.Immutable; 024 025import com.nimbusds.oauth2.sdk.ParseException; 026import com.nimbusds.oauth2.sdk.id.Identifier; 027 028 029/** 030 * CIBA request ID ({@code auth_req_id}). 031 * 032 * <p>Related specifications: 033 * 034 * <ul> 035 * <li>OpenID Connect CIBA Flow - Core 1.0, section 7.3. 036 * </ul> 037 */ 038@Immutable 039public class AuthRequestID extends Identifier { 040 041 042 /** 043 * The minimal required entropy (128 bits or 16 bytes). 044 */ 045 public static final int MIN_BYTE_LENGTH = 128 / 8; 046 047 048 /** 049 * The recommended entropy (160 bits or 20 bytes). 050 */ 051 public static final int RECOMMENDED_BYTE_LENGTH = 160 / 8; 052 053 054 /** 055 * Pattern that matches allowed characters only. 056 */ 057 public static final Pattern ALLOWED_CHARS_PATTERN = Pattern.compile("^[a-zA-Z0-9\\.\\-_]+$"); 058 059 060 private static final long serialVersionUID = -484823633025535607L; 061 062 063 /** 064 * Creates a new CIBA request ID with a randomly generated 160-bit 065 * (20-byte) value (the {@link #RECOMMENDED_BYTE_LENGTH recommended 066 * length}), Base64URL-encoded. 067 */ 068 public AuthRequestID() { 069 super(RECOMMENDED_BYTE_LENGTH); 070 } 071 072 073 /** 074 * Creates a new CIBA request ID with a randomly generated value of the 075 * specified byte length, Base64URL-encoded. 076 * 077 * @param byteLength The byte length of the value to generate. Must be 078 * at least {@link #MIN_BYTE_LENGTH 128 bits (16 079 * bytes) long}. 080 */ 081 public AuthRequestID(final int byteLength) { 082 super(byteLength); 083 if (byteLength < MIN_BYTE_LENGTH) { 084 throw new IllegalArgumentException("The CIBA request ID must be at least " + MIN_BYTE_LENGTH + " bits long"); 085 } 086 } 087 088 089 /** 090 * Creates a new CIBA request ID with the specified value. 091 * 092 * @param value The CIBA request ID value. Must contain only 093 * {@link #ALLOWED_CHARS_PATTERN legal characters} only 094 * and not be {@code null} or empty string. 095 */ 096 public AuthRequestID(final String value) { 097 super(value); 098 099 if (! ALLOWED_CHARS_PATTERN.matcher(value).matches()) { 100 throw new IllegalArgumentException("Illegal character(s) in the auth_req_id value"); 101 } 102 } 103 104 105 @Override 106 public boolean equals(final Object object) { 107 108 return object instanceof AuthRequestID && this.toString().equals(object.toString()); 109 } 110 111 112 /** 113 * Parses new CIBA request ID from the specified value. 114 * 115 * @param value The CIBA request ID value. 116 * 117 * @throws ParseException On a illegal value. 118 */ 119 public static AuthRequestID parse(final String value) 120 throws ParseException { 121 122 try { 123 return new AuthRequestID(value); 124 } catch (IllegalArgumentException e) { 125 throw new ParseException(e.getMessage()); 126 } 127 } 128}