1 /*
2  * Copyright (C) 2014 jsonwebtoken.io
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package io.jsonwebtoken.impl;
17
18 import io.jsonwebtoken.Claims;
19 import io.jsonwebtoken.RequiredTypeException;
20
21 import java.util.Date;
22 import java.util.Map;
23
24 public class DefaultClaims extends JwtMap implements Claims {
25
26     public DefaultClaims() {
27         super();
28     }
29
30     public DefaultClaims(Map<String, Object> map) {
31         super(map);
32     }
33
34     @Override
35     public String getIssuer() {
36         return getString(ISSUER);
37     }
38
39     @Override
40     public Claims setIssuer(String iss) {
41         setValue(ISSUER, iss);
42         return this;
43     }
44
45     @Override
46     public String getSubject() {
47         return getString(SUBJECT);
48     }
49
50     @Override
51     public Claims setSubject(String sub) {
52         setValue(SUBJECT, sub);
53         return this;
54     }
55
56     @Override
57     public String getAudience() {
58         return getString(AUDIENCE);
59     }
60
61     @Override
62     public Claims setAudience(String aud) {
63         setValue(AUDIENCE, aud);
64         return this;
65     }
66
67     @Override
68     public Date getExpiration() {
69         return get(Claims.EXPIRATION, Date.class);
70     }
71
72     @Override
73     public Claims setExpiration(Date exp) {
74         setDate(Claims.EXPIRATION, exp);
75         return this;
76     }
77
78     @Override
79     public Date getNotBefore() {
80         return get(Claims.NOT_BEFORE, Date.class);
81     }
82
83     @Override
84     public Claims setNotBefore(Date nbf) {
85         setDate(Claims.NOT_BEFORE, nbf);
86         return this;
87     }
88
89     @Override
90     public Date getIssuedAt() {
91         return get(Claims.ISSUED_AT, Date.class);
92     }
93
94     @Override
95     public Claims setIssuedAt(Date iat) {
96         setDate(Claims.ISSUED_AT, iat);
97         return this;
98     }
99
100     @Override
101     public String getId() {
102         return getString(ID);
103     }
104
105     @Override
106     public Claims setId(String jti) {
107         setValue(Claims.ID, jti);
108         return this;
109     }
110
111     @Override
112     public <T> T get(String claimName, Class<T> requiredType) {
113         Object value = get(claimName);
114         if (value == null) { return null; }
115
116         if (Claims.EXPIRATION.equals(claimName) ||
117             Claims.ISSUED_AT.equals(claimName) ||
118             Claims.NOT_BEFORE.equals(claimName)
119         ) {
120             value = getDate(claimName);
121         }
122
123         return castClaimValue(value, requiredType);
124     }
125
126     private <T> T castClaimValue(Object value, Class<T> requiredType) {
127         if (requiredType == Date.class && value instanceof Long) {
128             value = new Date((Long)value);
129         }
130
131         if (value instanceof Integer) {
132             int intValue = (Integer) value;
133             if (requiredType == Long.class) {
134                 value = (long) intValue;
135             } else if (requiredType == Short.class && Short.MIN_VALUE <= intValue && intValue <= Short.MAX_VALUE) {
136                 value = (short) intValue;
137             } else if (requiredType == Byte.class && Byte.MIN_VALUE <= intValue && intValue <= Byte.MAX_VALUE) {
138                 value = (byte) intValue;
139             }
140         }
141
142         if (!requiredType.isInstance(value)) {
143             throw new RequiredTypeException("Expected value to be of type: " + requiredType + ", but was " + value.getClass());
144         }
145
146         return requiredType.cast(value);
147     }
148 }
149