001package com.nimbusds.jose.jwk;
002
003
004import java.text.ParseException;
005
006
007/**
008 * Enumeration of public key uses. Represents the {@code use} parameter in a
009 * JSON Web Key (JWK).
010 *
011 * <p>Public JWK use values:
012 *
013 * <ul>
014 *     <li>{@link #SIGNATURE sig}
015 *     <li>{@link #ENCRYPTION enc}
016 * </ul>
017 *
018 * @author Vladimir Dzhuvinov
019 * @version 2014-04-02
020 */
021public enum KeyUse {
022
023
024        /**
025         * Signature.
026         */
027        SIGNATURE("sig"),
028
029
030        /**
031         * Encryption.
032         */
033        ENCRYPTION("enc");
034
035
036        /**
037         * The public key use identifier.
038         */
039        private final String identifier;
040
041
042        /**
043         * Creates a new public key use with the specified identifier.
044         *
045         * @param identifier The public key use identifier. Must not be
046         *                   {@code null}.
047         */
048        KeyUse(final String identifier) {
049
050                if (identifier == null)
051                        throw new IllegalArgumentException("The key use identifier must not be null");
052
053                this.identifier = identifier;
054        }
055
056
057        /**
058         * Returns the identifier of this public key use.
059         *
060         * @return The identifier.
061         */
062        public String identifier() {
063
064                return identifier;
065        }
066
067
068        /**
069         * @see #identifier()
070         */
071        @Override
072        public String toString() {
073
074                return identifier();
075        }
076
077
078        /**
079         * Parses a public key use from the specified JWK {@code use} parameter
080         * value.
081         *
082         * @param s The string to parse. May be {@code null}.
083         *
084         * @return The public key use, {@code null} if none.
085         *
086         * @throws ParseException If the string couldn't be parsed to a valid
087         *                        public key use.
088         */
089        public static KeyUse parse(final String s)
090                throws ParseException {
091
092                if (s == null) {
093                        return null;
094                }
095
096                for (KeyUse use: KeyUse.values()) {
097
098                        if (s.equals(use.identifier)) {
099                                return use;
100                        }
101                }
102
103                throw new ParseException("Invalid JWK use: " + s, 0);
104        }
105}