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;
17
18 import io.jsonwebtoken.impl.DefaultClaims;
19 import io.jsonwebtoken.impl.DefaultHeader;
20 import io.jsonwebtoken.impl.DefaultJwsHeader;
21 import io.jsonwebtoken.impl.DefaultJwtBuilder;
22 import io.jsonwebtoken.impl.DefaultJwtParser;
23
24 import java.util.Map;
25
26 /**
27 * Factory class useful for creating instances of JWT interfaces. Using this factory class can be a good
28 * alternative to tightly coupling your code to implementation classes.
29 *
30 * @since 0.1
31 */
32 public final class Jwts {
33
34 private Jwts(){}
35
36 /**
37 * Creates a new {@link Header} instance suitable for <em>plaintext</em> (not digitally signed) JWTs. As this
38 * is a less common use of JWTs, consider using the {@link #jwsHeader()} factory method instead if you will later
39 * digitally sign the JWT.
40 *
41 * @return a new {@link Header} instance suitable for <em>plaintext</em> (not digitally signed) JWTs.
42 */
43 public static Header header() {
44 return new DefaultHeader();
45 }
46
47 /**
48 * Creates a new {@link Header} instance suitable for <em>plaintext</em> (not digitally signed) JWTs, populated
49 * with the specified name/value pairs. As this is a less common use of JWTs, consider using the
50 * {@link #jwsHeader(java.util.Map)} factory method instead if you will later digitally sign the JWT.
51 *
52 * @return a new {@link Header} instance suitable for <em>plaintext</em> (not digitally signed) JWTs.
53 */
54 public static Header header(Map<String, Object> header) {
55 return new DefaultHeader(header);
56 }
57
58 /**
59 * Returns a new {@link JwsHeader} instance suitable for digitally signed JWTs (aka 'JWS's).
60 *
61 * @return a new {@link JwsHeader} instance suitable for digitally signed JWTs (aka 'JWS's).
62 * @see JwtBuilder#setHeader(Header)
63 */
64 public static JwsHeader jwsHeader() {
65 return new DefaultJwsHeader();
66 }
67
68 /**
69 * Returns a new {@link JwsHeader} instance suitable for digitally signed JWTs (aka 'JWS's), populated with the
70 * specified name/value pairs.
71 *
72 * @return a new {@link JwsHeader} instance suitable for digitally signed JWTs (aka 'JWS's), populated with the
73 * specified name/value pairs.
74 * @see JwtBuilder#setHeader(Header)
75 */
76 public static JwsHeader jwsHeader(Map<String, Object> header) {
77 return new DefaultJwsHeader(header);
78 }
79
80 /**
81 * Returns a new {@link Claims} instance to be used as a JWT body.
82 *
83 * @return a new {@link Claims} instance to be used as a JWT body.
84 */
85 public static Claims claims() {
86 return new DefaultClaims();
87 }
88
89 /**
90 * Returns a new {@link Claims} instance populated with the specified name/value pairs.
91 *
92 * @param claims the name/value pairs to populate the new Claims instance.
93 * @return a new {@link Claims} instance populated with the specified name/value pairs.
94 */
95 public static Claims claims(Map<String, Object> claims) {
96 return new DefaultClaims(claims);
97 }
98
99 /**
100 * Returns a new {@link JwtParser} instance that can be configured and then used to parse JWT strings.
101 *
102 * @return a new {@link JwtParser} instance that can be configured and then used to parse JWT strings.
103 */
104 public static JwtParser parser() {
105 return new DefaultJwtParser();
106 }
107
108 /**
109 * Returns a new {@link JwtBuilder} instance that can be configured and then used to create JWT compact serialized
110 * strings.
111 *
112 * @return a new {@link JwtBuilder} instance that can be configured and then used to create JWT compact serialized
113 * strings.
114 */
115 public static JwtBuilder builder() {
116 return new DefaultJwtBuilder();
117 }
118 }
119