[CXF-7344] Fix for jms rest problem. Convert dots to underscores
[cxf.git] / rt / rs / security / jose-parent / jose / src / main / java / org / apache / cxf / rs / security / jose / jwt / JwtClaims.java
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 package org.apache.cxf.rs.security.jose.jwt;
21
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Map;
25
26 import org.apache.cxf.common.util.StringUtils;
27 import org.apache.cxf.helpers.CastUtils;
28 import org.apache.cxf.jaxrs.json.basic.JsonMapObject;
29
30
31
32
33 public class JwtClaims extends JsonMapObject {
34
35 public JwtClaims() {
36 }
37
38 public JwtClaims(Map<String, Object> values) {
39 super(values);
40 }
41
42 public void setIssuer(String issuer) {
43 setClaim(JwtConstants.CLAIM_ISSUER, issuer);
44 }
45
46 public String getIssuer() {
47 return (String)getClaim(JwtConstants.CLAIM_ISSUER);
48 }
49
50 public void setSubject(String subject) {
51 setClaim(JwtConstants.CLAIM_SUBJECT, subject);
52 }
53
54 public String getSubject() {
55 return (String)getClaim(JwtConstants.CLAIM_SUBJECT);
56 }
57
58 /**
59 * Set a single audience value which will be serialized as a String
60 * @param audience the audience
61 */
62 public void setAudience(String audience) {
63 setClaim(JwtConstants.CLAIM_AUDIENCE, audience);
64 }
65
66 /**
67 * Get a single audience value. If the audience claim value is an array then the
68 * first value will be returned.
69 * @return the audience
70 */
71 public String getAudience() {
72 List<String> audiences = getAudiences();
73 if (!StringUtils.isEmpty(audiences)) {
74 return audiences.get(0);
75 } else {
76 return null;
77 }
78 }
79
80 /**
81 * Set an array of audiences
82 * @param audiences the audiences array
83 */
84 public void setAudiences(List<String> audiences) {
85 setClaim(JwtConstants.CLAIM_AUDIENCE, audiences);
86 }
87
88 /**
89 * Get an array of audiences
90 * @return the audiences array
91 */
92 public List<String> getAudiences() {
93 Object audiences = getClaim(JwtConstants.CLAIM_AUDIENCE);
94 if (audiences instanceof List<?>) {
95 return CastUtils.cast((List<?>)audiences);
96 } else if (audiences instanceof String) {
97 return Collections.singletonList((String)audiences);
98 }
99
100 return null;
101 }
102
103 public void setExpiryTime(Long expiresIn) {
104 setClaim(JwtConstants.CLAIM_EXPIRY, expiresIn);
105 }
106
107 public Long getExpiryTime() {
108 return getLongProperty(JwtConstants.CLAIM_EXPIRY);
109 }
110
111 public void setNotBefore(Long notBefore) {
112 setClaim(JwtConstants.CLAIM_NOT_BEFORE, notBefore);
113 }
114
115 public Long getNotBefore() {
116 return getLongProperty(JwtConstants.CLAIM_NOT_BEFORE);
117 }
118
119 public void setIssuedAt(Long issuedAt) {
120 setClaim(JwtConstants.CLAIM_ISSUED_AT, issuedAt);
121 }
122
123 public Long getIssuedAt() {
124 return getLongProperty(JwtConstants.CLAIM_ISSUED_AT);
125 }
126
127 public void setTokenId(String id) {
128 setClaim(JwtConstants.CLAIM_JWT_ID, id);
129 }
130
131 public String getTokenId() {
132 return (String)getClaim(JwtConstants.CLAIM_JWT_ID);
133 }
134
135 public JwtClaims setClaim(String name, Object value) {
136 setProperty(name, value);
137 return this;
138 }
139
140 public Object getClaim(String name) {
141 return getProperty(name);
142 }
143 }