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