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
072
073        /**
074         * Generates a new code verifier represented by a secure random 256-bit
075         * number that is Base64URL-encoded (as a 43 character string, which is
076         * the {@link #MIN_LENGTH minimum character length} of a code
077         * verifier).
078         */
079        public CodeVerifier() {
080                super(32);
081        }
082
083
084        @Override
085        public boolean equals(final Object object) {
086                return object instanceof CodeVerifier && super.equals(object);
087        }
088}