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        /**
037         * The minimum character length of a code verifier.
038         */
039        public static final int MIN_LENGTH = 43;
040
041
042        /**
043         * The maximum character length of a code verifier.
044         */
045        public static final int MAX_LENGTH = 128;
046        
047
048        /**
049         * Creates a new code verifier with the specified value.
050         *
051         * @param value The code verifier value. Must not contain characters
052         *              other than [A-Z] / [a-z] / [0-9] / "-" / "." / "_" /
053         *              "~". The verifier length must be at least 43
054         *              characters but not more than 128 characters. Must not
055         *              be {@code null} or empty string.
056         */
057        public CodeVerifier(final String value) {
058                super(value);
059
060                if (value.length() < MIN_LENGTH) {
061                        throw new IllegalArgumentException("The code verifier must be at least " + MIN_LENGTH + " characters");
062                }
063
064                if (value.length() > MAX_LENGTH) {
065                        throw new IllegalArgumentException("The code verifier must not be longer than " + MAX_LENGTH + " characters");
066                }
067        }
068
069
070        /**
071         * Generates a new code verifier represented by a secure random 256-bit
072         * number that is Base64URL-encoded (as a 43 character string, which is
073         * the {@link #MIN_LENGTH minimum character length} of a code
074         * verifier).
075         */
076        public CodeVerifier() {
077                super(32);
078        }
079
080
081        @Override
082        public boolean equals(final Object object) {
083                return object instanceof CodeVerifier && super.equals(object);
084        }
085}