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.pkce; 019 020 021import com.nimbusds.oauth2.sdk.auth.Secret; 022 023 024/** 025 * Authorisation code verifier. 026 * 027 * <p>Related specifications: 028 * 029 * <ul> 030 * <li>Proof Key for Code Exchange by OAuth Public Clients (RFC 7636). 031 * </ul> 032 */ 033public class CodeVerifier extends Secret { 034 035 036 private static final long serialVersionUID = 1L; 037 038 039 /** 040 * The minimum character length of a code verifier. 041 */ 042 public static final int MIN_LENGTH = 43; 043 044 045 /** 046 * The maximum character length of a code verifier. 047 */ 048 public static final int MAX_LENGTH = 128; 049 050 051 /** 052 * Creates a new code verifier with the specified value. 053 * 054 * @param value The code verifier value. Must not contain characters 055 * other than [A-Z] / [a-z] / [0-9] / "-" / "." / "_" / 056 * "~". The verifier length must be at least 43 057 * characters but not more than 128 characters. Must not 058 * be {@code null} or empty string. 059 */ 060 public CodeVerifier(final String value) { 061 super(value); 062 063 if (value.length() < MIN_LENGTH) { 064 throw new IllegalArgumentException("The code verifier must be at least " + MIN_LENGTH + " characters"); 065 } 066 067 if (value.length() > MAX_LENGTH) { 068 throw new IllegalArgumentException("The code verifier must not be longer than " + MAX_LENGTH + " characters"); 069 } 070 071 if (! isLegal(value)) { 072 throw new IllegalArgumentException("Illegal char(s) in code verifier, see RFC 7636, section 4.1"); 073 } 074 } 075 076 077 /** 078 * Generates a new code verifier represented by a secure random 256-bit 079 * number that is Base64URL-encoded (as a 43 character string, which is 080 * the {@link #MIN_LENGTH minimum character length} of a code 081 * verifier). 082 */ 083 public CodeVerifier() { 084 super(32); 085 } 086 087 088 @Override 089 public boolean equals(final Object object) { 090 return object instanceof CodeVerifier && super.equals(object); 091 } 092 093 094 /** 095 * Returns {@code true} if the specified string is legal for a code 096 * verifier. 097 * 098 * @param s The string, {@code null} if not specified. 099 * 100 * @return {@code true} if legal or {@code null}, {@code false} if 101 * illegal. 102 */ 103 static boolean isLegal(final String s) { 104 105 if (s == null) { 106 return true; 107 } 108 109 for (char c : s.toCharArray()) { 110 if (!isLegal(c)) { 111 return false; 112 } 113 } 114 115 return true; 116 } 117 118 /** 119 * Returns {@code true} if the specified character is legal for a code 120 * verifier. 121 * 122 * @param c The character. 123 * 124 * @return {@code true} if legal, {@code false} if illegal. 125 */ 126 static boolean isLegal(final char c) { 127 128 // https://tools.ietf.org/html/rfc7636#page-8 129 // 130 // ABNF for "code_verifier" is as follows. 131 // 132 // code-verifier = 43*128unreserved 133 // unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" 134 // ALPHA = %x41-5A / %x61-7A 135 // DIGIT = %x30-39 136 137 if (c > 0x7f) { 138 // Not ASCII 139 return false; 140 } 141 142 return c >= 0x41 && c <= 0x5a || c >= 0x61 && c <= 0x7a || c >= 0x30 && c <= 0x39 || c == '-' || c == '.' || c == '_' || c == '~'; 143 } 144}