001package com.nimbusds.oauth2.sdk.pkce;
002
003
004import com.nimbusds.oauth2.sdk.id.Identifier;
005import net.jcip.annotations.Immutable;
006
007
008/**
009 * Method that was used to derive an authorisation code challenge.
010 *
011 * <p>Related specifications:
012 *
013 * <ul>
014 *     <li>Proof Key for Code Exchange by OAuth Public Clients (RFC 7636).
015 * </ul>
016 */
017@Immutable
018public final class CodeChallengeMethod extends Identifier {
019
020
021        /**
022         * Plain code challenge method.
023         */
024        public static final CodeChallengeMethod PLAIN = new CodeChallengeMethod("plain");
025
026
027        /**
028         * SHA-256 code challenge method.
029         */
030        public static final CodeChallengeMethod S256 = new CodeChallengeMethod("S256");
031
032
033        /**
034         * Gets the default code challenge method.
035         *
036         * @return {@link #PLAIN}
037         */
038        public static CodeChallengeMethod getDefault() {
039
040                return PLAIN;
041        }
042        
043
044        /**
045         *
046         * @param value The code challenge method value. Must not be
047         *              {@code null} or empty string.
048         */
049        public CodeChallengeMethod(final String value) {
050
051                super(value);
052        }
053
054
055        /**
056         * Parses a code challenge method from the specified value.
057         *
058         * @param value The code challenge method value. Must not be
059         *              {@code null} or empty string.
060         *
061         * @return The code challenge method.
062         */
063        public static CodeChallengeMethod parse(final String value) {
064
065                if (value.equals(PLAIN.getValue())) {
066                        return PLAIN;
067                } else if (value.equals(S256.getValue())) {
068                        return S256;
069                } else {
070                        return new CodeChallengeMethod(value);
071                }
072        }
073
074
075        @Override
076        public boolean equals(final Object object) {
077
078                return object instanceof CodeChallengeMethod &&
079                        this.toString().equals(object.toString());
080        }
081}