Skip to content
This repository was archived by the owner on Apr 17, 2024. It is now read-only.

Commit 1798b7c

Browse files
willinoiscopybara-github
authored andcommitted
Add JWT Signature parameters and key types.
PiperOrigin-RevId: 625468585
1 parent 9325851 commit 1798b7c

5 files changed

+209
-0
lines changed

cc/jwt/BUILD.bazel

+31
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,37 @@ cc_library(
275275
],
276276
)
277277

278+
cc_library(
279+
name = "jwt_signature_parameters",
280+
hdrs = ["jwt_signature_parameters.h"],
281+
include_prefix = "tink/jwt",
282+
deps = ["//:parameters"],
283+
)
284+
285+
cc_library(
286+
name = "jwt_signature_public_key",
287+
hdrs = ["jwt_signature_public_key.h"],
288+
include_prefix = "tink/jwt",
289+
deps = [
290+
":jwt_signature_parameters",
291+
"//:key",
292+
"@com_google_absl//absl/types:optional",
293+
],
294+
)
295+
296+
cc_library(
297+
name = "jwt_signature_private_key",
298+
hdrs = ["jwt_signature_private_key.h"],
299+
include_prefix = "tink/jwt",
300+
deps = [
301+
":jwt_signature_parameters",
302+
":jwt_signature_public_key",
303+
"//:key",
304+
"//:private_key",
305+
"@com_google_absl//absl/types:optional",
306+
],
307+
)
308+
278309
# tests
279310

280311
cc_test(

cc/jwt/CMakeLists.txt

+30
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,36 @@ tink_cc_library(
252252
tink::proto::tink_cc_proto
253253
)
254254

255+
tink_cc_library(
256+
NAME jwt_signature_parameters
257+
SRCS
258+
jwt_signature_parameters.h
259+
DEPS
260+
tink::core::parameters
261+
)
262+
263+
tink_cc_library(
264+
NAME jwt_signature_public_key
265+
SRCS
266+
jwt_signature_public_key.h
267+
DEPS
268+
tink::jwt::jwt_signature_parameters
269+
absl::optional
270+
tink::core::key
271+
)
272+
273+
tink_cc_library(
274+
NAME jwt_signature_private_key
275+
SRCS
276+
jwt_signature_private_key.h
277+
DEPS
278+
tink::jwt::jwt_signature_parameters
279+
tink::jwt::jwt_signature_public_key
280+
absl::optional
281+
tink::core::key
282+
tink::core::private_key
283+
)
284+
255285
# tests
256286

257287
tink_cc_test(

cc/jwt/jwt_signature_parameters.h

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
////////////////////////////////////////////////////////////////////////////////
16+
17+
#ifndef TINK_JWT_JWT_SIGNATURE_PARAMETERS_H_
18+
#define TINK_JWT_JWT_SIGNATURE_PARAMETERS_H_
19+
20+
#include "tink/parameters.h"
21+
22+
namespace crypto {
23+
namespace tink {
24+
25+
// Describes a JWT signature key pair without the randomly chosen key material.
26+
class JwtSignatureParameters : public Parameters {
27+
// Returns true if verification is allowed for tokens without a `kid` header.
28+
virtual bool AllowKidAbsent() const = 0;
29+
};
30+
31+
} // namespace tink
32+
} // namespace crypto
33+
34+
35+
#endif // TINK_JWT_JWT_SIGNATURE_PARAMETERS_H_

cc/jwt/jwt_signature_private_key.h

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
////////////////////////////////////////////////////////////////////////////////
16+
17+
#ifndef TINK_JWT_JWT_SIGNATURE_PRIVATE_KEY_H_
18+
#define TINK_JWT_JWT_SIGNATURE_PRIVATE_KEY_H_
19+
20+
#include <string>
21+
22+
#include "absl/types/optional.h"
23+
#include "tink/jwt/jwt_signature_parameters.h"
24+
#include "tink/jwt/jwt_signature_public_key.h"
25+
#include "tink/key.h"
26+
#include "tink/private_key.h"
27+
28+
namespace crypto {
29+
namespace tink {
30+
31+
// Represents the signing function for a JWT Signature primitive.
32+
class JwtSignaturePrivateKey : public PrivateKey {
33+
public:
34+
const JwtSignaturePublicKey& GetPublicKey() const override = 0;
35+
36+
absl::optional<std::string> GetKid() const {
37+
return GetPublicKey().GetKid();
38+
}
39+
40+
absl::optional<int> GetIdRequirement() const override {
41+
return GetPublicKey().GetIdRequirement();
42+
}
43+
44+
const JwtSignatureParameters& GetParameters() const override {
45+
return GetPublicKey().GetParameters();
46+
}
47+
48+
bool operator==(const Key& other) const override = 0;
49+
};
50+
51+
} // namespace tink
52+
} // namespace crypto
53+
54+
#endif // TINK_JWT_JWT_SIGNATURE_PRIVATE_KEY_H_

cc/jwt/jwt_signature_public_key.h

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
////////////////////////////////////////////////////////////////////////////////
16+
17+
#ifndef TINK_JWT_JWT_SIGNATURE_PUBLIC_KEY_H_
18+
#define TINK_JWT_JWT_SIGNATURE_PUBLIC_KEY_H_
19+
20+
#include <string>
21+
22+
#include "absl/types/optional.h"
23+
#include "tink/jwt/jwt_signature_parameters.h"
24+
#include "tink/key.h"
25+
26+
namespace crypto {
27+
namespace tink {
28+
29+
// Represents the verification function for a JWT Signature primitive.
30+
class JwtSignaturePublicKey : public Key {
31+
public:
32+
// Returns the `kid` to be used for this key
33+
// (https://www.rfc-editor.org/rfc/rfc7517#section-4.5).
34+
//
35+
// Note that the `kid` is not necessarily related to Tink's key ID in the
36+
// keyset.
37+
//
38+
// If present, this `kid` will be written into the `kid` header during
39+
// `ComputeMacAndEncode()`. If absent, no `kid` will be written.
40+
//
41+
// If present, and the `kid` header is present, the contents of the
42+
// `kid` header need to match the return value of this function for
43+
// validation to succeed in `VerifyMacAndDecode()`.
44+
//
45+
// Note that `GetParameters().AllowKidAbsent()` specifies whether or not
46+
// omitting the `kid` header is allowed. Of course, if
47+
// `GetParameters().AllowKidAbsent()` returns false, then `GetKid()` must
48+
// return a non-empty value.
49+
virtual absl::optional<std::string> GetKid() const = 0;
50+
51+
const JwtSignatureParameters& GetParameters() const override = 0;
52+
53+
bool operator==(const Key& other) const override = 0;
54+
};
55+
56+
} // namespace tink
57+
} // namespace crypto
58+
59+
#endif // TINK_JWT_JWT_SIGNATURE_PUBLIC_KEY_H_

0 commit comments

Comments
 (0)