Skip to content

Commit 9ac5c68

Browse files
committed
fixed readme
1 parent 7822e76 commit 9ac5c68

File tree

3 files changed

+73
-17
lines changed

3 files changed

+73
-17
lines changed

README.md

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
## Zig-jwt
22

3-
A JWT library for zig.
3+
A JWT (JSON Web Token) library for zig.
44

55

66
### Env
77

88
- Zig >= 0.14.0-dev.2851+b074fb7dd
99

1010

11+
### What the heck is a JWT?
12+
13+
JWT.io has [a great introduction](https://jwt.io/introduction) to JSON Web Tokens.
14+
15+
In short, it's a signed JSON object that does something useful (for example, authentication). It's commonly used for `Bearer` tokens in Oauth 2. A token is made of three parts, separated by `.`'s. The first two parts are JSON objects, that have been [base64url](https://datatracker.ietf.org/doc/html/rfc4648) encoded. The last part is the signature, encoded the same way.
16+
17+
The first part is called the header. It contains the necessary information for verifying the last part, the signature. For example, which encryption method was used for signing and what key was used.
18+
19+
The part in the middle is the interesting bit. It's called the Claims and contains the actual stuff you care about. Refer to [RFC 7519](https://datatracker.ietf.org/doc/html/rfc7519) for information about reserved keys and the proper way to add your own.
20+
21+
22+
### What's in the box?
23+
24+
This library supports the parsing and verification as well as the generation and signing of JWTs. Current supported signing algorithms are HMAC SHA, RSA, RSA-PSS, and ECDSA, though hooks are present for adding your own.
25+
26+
1127
### Adding zig-jwt as a dependency
1228

1329
Add the dependency to your project:
@@ -102,6 +118,45 @@ The JWT library have signing methods:
102118
- `none`: jwt.SigningMethodNone
103119

104120

121+
### Sign PublicKey
122+
123+
RSA PublicKey:
124+
~~~zig
125+
const secret_key = jwt.crypto_rsa.SecretKey;
126+
const public_key = jwt.crypto_rsa.PublicKey;
127+
128+
// rsa no generate
129+
~~~
130+
131+
ECDSA PublicKey:
132+
~~~zig
133+
const ecdsa = std.crypto.sign.ecdsa;
134+
135+
const p256_secret_key = ecdsa.EcdsaP256Sha256.SecretKey;
136+
const p256_public_key = ecdsa.EcdsaP256Sha256.PublicKey;
137+
138+
const p384_secret_key = ecdsa.EcdsaP384Sha384.SecretKey;
139+
const p384_public_key = ecdsa.EcdsaP384Sha384.PublicKey;
140+
141+
// generate p256 public key
142+
const p256_kp = ecdsa.EcdsaP256Sha256.KeyPair.generate();
143+
144+
// generate p384 public key
145+
const p384_kp = ecdsa.EcdsaP384Sha384.KeyPair.generate();
146+
~~~
147+
148+
EdDSA PublicKey:
149+
~~~zig
150+
const Ed25519 = std.crypto.sign.Ed25519;
151+
152+
const secret_key = Ed25519.SecretKey;
153+
const public_key = Ed25519.PublicKey;
154+
155+
// generate public key
156+
const kp = Ed25519.KeyPair.generate();
157+
~~~
158+
159+
105160
### LICENSE
106161

107162
* The library LICENSE is `Apache2`, using the library need keep the LICENSE.

build.zig.zon

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
.{
22
.name = "zig-jwt",
3-
.description = "A JWT library for zig.",
4-
.version = "1.0.7",
3+
.description = "A JWT (JSON Web Token) library for zig.",
4+
.version = "1.0.8",
55
.paths = .{
66
"build.zig",
77
"build.zig.zon",
88
"LICENSE",
9+
"README.md",
910
"src",
1011
},
1112
}

src/jwt.zig

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,27 @@ pub fn JWT(comptime Signer: type, comptime SecretKeyType: type, comptime PublicK
5656
};
5757
}
5858

59-
pub fn sign(self: Self, claims: anytype, key: SecretKeyType) ![]const u8 {
59+
pub fn sign(self: Self, claims: anytype, secret_key: SecretKeyType) ![]const u8 {
6060
var t = token.Token.init(self.alloc);
6161
try t.setHeader(.{
6262
.typ = "JWT",
6363
.alg = self.signer.alg(),
6464
});
6565
try t.setClaims(claims);
6666

67-
const signed = try t.signingString();
67+
const signing_string = try t.signingString();
6868
defer t.deinit();
6969

70-
const signed_string = try self.signer.sign(signed, key);
71-
t.withSignature(signed_string);
70+
const signature = try self.signer.sign(signing_string, secret_key);
71+
t.withSignature(signature);
7272

73-
defer self.alloc.free(signed_string);
73+
defer self.alloc.free(signature);
7474

75-
const sig = try t.signedString();
76-
return sig;
75+
const signed_token = try t.signedString();
76+
return signed_token;
7777
}
7878

79-
pub fn parse(self: Self, token_string: []const u8, key: PublicKeyType) !token.Token {
79+
pub fn parse(self: Self, token_string: []const u8, public_key: PublicKeyType) !token.Token {
8080
var t = token.Token.init(self.alloc);
8181
try t.parse(token_string);
8282

@@ -88,15 +88,15 @@ pub fn JWT(comptime Signer: type, comptime SecretKeyType: type, comptime PublicK
8888
return Error.JWTAlgoInvalid;
8989
}
9090

91-
const token_sign = t.getSignature();
91+
const signature = t.getSignature();
9292

93-
const signed = try self.alloc.alloc(u8, token_sign.len);
94-
@memcpy(signed[0..], token_sign[0..]);
93+
const token_sign = try self.alloc.alloc(u8, signature.len);
94+
@memcpy(token_sign[0..], signature[0..]);
9595

96-
defer self.alloc.free(signed);
96+
defer self.alloc.free(token_sign);
9797

98-
const msg = try t.signingString();
99-
if (!self.signer.verify(msg, signed, key)) {
98+
const signing_string = try t.signingString();
99+
if (!self.signer.verify(signing_string, token_sign, public_key)) {
100100
return Error.JWTVerifyFail;
101101
}
102102

0 commit comments

Comments
 (0)