-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: decoupling JWT from Pollux and adding KID header to JWTs (#271)
Signed-off-by: Curtish <ch@curtish.me>
- Loading branch information
Showing
14 changed files
with
533 additions
and
339 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
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
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 |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { JWTPayload, Signer, createJWT } from "did-jwt"; | ||
import { base64url } from "multiformats/bases/base64"; | ||
import { DID, PrivateKey } from ".."; | ||
import { asJsonObj, isNil } from "../../utils/guards"; | ||
|
||
export namespace JWT { | ||
export interface Header { | ||
typ: string; | ||
alg: string; | ||
[key: string]: any; | ||
} | ||
|
||
export type Payload = JWTPayload; | ||
|
||
export interface DecodedObj { | ||
header: Header; | ||
payload: Payload; | ||
signature: string; | ||
data: string; | ||
} | ||
|
||
|
||
/** | ||
* Creates a signed JWT | ||
* | ||
* @param issuer | ||
* @param privateKey | ||
* @param payload | ||
* @returns | ||
*/ | ||
export const sign = async ( | ||
issuer: DID, | ||
privateKey: PrivateKey, | ||
payload: Partial<Payload>, | ||
header?: Partial<Header> | ||
): Promise<string> => { | ||
if (!privateKey.isSignable()) { | ||
throw new Error("Key is not signable"); | ||
} | ||
|
||
const signer: Signer = async (data: any) => { | ||
const signature = privateKey.sign(Buffer.from(data)); | ||
const encoded = base64url.baseEncode(signature); | ||
return encoded; | ||
}; | ||
|
||
const jwt = await createJWT( | ||
payload, | ||
{ issuer: issuer.toString(), signer }, | ||
{ alg: privateKey.alg, ...asJsonObj(header) } | ||
); | ||
|
||
return jwt; | ||
}; | ||
|
||
/** | ||
* decode a JWT into its parts | ||
* | ||
* @param jws | ||
* @returns | ||
*/ | ||
export const decode = (jws: string): DecodedObj => { | ||
const parts = jws.split("."); | ||
const headersEnc = parts.at(0); | ||
const payloadEnc = parts.at(1); | ||
|
||
if (parts.length != 3 || isNil(headersEnc) || isNil(payloadEnc)) { | ||
// TODO error | ||
// throw new InvalidJWTString(); | ||
throw new Error(); | ||
} | ||
|
||
const headers = base64url.baseDecode(headersEnc); | ||
const payload = base64url.baseDecode(payloadEnc); | ||
|
||
return { | ||
header: JSON.parse(Buffer.from(headers).toString()), | ||
payload: JSON.parse(Buffer.from(payload).toString()), | ||
signature: parts[2], | ||
data: `${headersEnc}.${payloadEnc}`, | ||
}; | ||
}; | ||
} |
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
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
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
Oops, something went wrong.