-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add token complete check and option to decode the header part o…
…f the token
- Loading branch information
1 parent
0d611db
commit f2303f1
Showing
4 changed files
with
96 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,4 @@ dist-ssr | |
*.njsproj | ||
*.sln | ||
*.sw? | ||
coverage | ||
|
||
api_ | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
generated | ||
pnpm-lock.yaml | ||
pnpm-lock.yaml | ||
CHANGELOG.md | ||
.github |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,71 @@ | ||
// setToken.test.ts | ||
import { jwtDecoder } from "./main.ts"; | ||
import { describe, it, expect } from "vitest"; | ||
import { JWTHeader, TokenPart, jwtDecoder, type JWTDecoded } from "./main.ts"; | ||
import { describe, it, expect, expectTypeOf } from "vitest"; | ||
|
||
describe("jwtDecode", () => { | ||
it("decode John Balázs", () => { | ||
const decodedToken = jwtDecoder( | ||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gQmFsw6F6cyIsImlhdCI6MTUxNjIzOTAyMn0.rpQt1WOpF4h5SP9iBBj2NfYvKQLuCI3lHSvxSS3eexs", | ||
); | ||
|
||
expectTypeOf(decodedToken).toEqualTypeOf<JWTDecoded | null>(); | ||
expect(decodedToken).toEqual({ | ||
sub: "1234567890", | ||
name: "John Balázs", | ||
iat: 1516239022, | ||
}); | ||
}); | ||
|
||
it("incomplete token", () => { | ||
const decodedToken = jwtDecoder( | ||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gQmFsw6F6cyIsImlhdCI6MTUxNjIzOTAyMn0", | ||
); | ||
|
||
expectTypeOf(decodedToken).toEqualTypeOf<JWTDecoded | null>(); | ||
expect(decodedToken).toEqual(null); | ||
}); | ||
|
||
it("decode header", () => { | ||
const decodedToken = jwtDecoder( | ||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gQmFsw6F6cyIsImlhdCI6MTUxNjIzOTAyMn0.rpQt1WOpF4h5SP9iBBj2NfYvKQLuCI3lHSvxSS3eexs", | ||
TokenPart.header, | ||
); | ||
|
||
expectTypeOf(decodedToken).toEqualTypeOf<JWTHeader | null>(); | ||
|
||
expect(decodedToken).toEqual({ | ||
alg: "HS256", | ||
typ: "JWT", | ||
}); | ||
}); | ||
|
||
it("return null when nothing defined", () => { | ||
const decodedToken = jwtDecoder(); | ||
expect(decodedToken).toBeNull(); | ||
}); | ||
|
||
it("extended type", () => { | ||
const decodedToken = jwtDecoder< | ||
JWTDecoded & { | ||
roles: string[]; | ||
} | ||
>(""); | ||
|
||
expectTypeOf(decodedToken).toEqualTypeOf< | ||
| (JWTDecoded & { | ||
roles: string[]; | ||
}) | ||
| null | ||
>(); | ||
expect(decodedToken).toBeNull(); | ||
}); | ||
|
||
it("extended type", () => { | ||
const decodedToken = jwtDecoder< | ||
JWTDecoded & { | ||
// Extra attributes here | ||
} | ||
>(""); | ||
expect(decodedToken).toBeNull(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters