Skip to content

Commit ded8a84

Browse files
committed
refactor: add docblocks
1 parent 770b8f6 commit ded8a84

File tree

4 files changed

+68
-6
lines changed

4 files changed

+68
-6
lines changed

src/encryption.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ import { MessageVerifier } from './message_verifier.ts'
2323
* security (read more https://en.wikipedia.org/wiki/Semantic_security).
2424
*/
2525
export class Encryption {
26+
/**
27+
* Configuration options for the encryption instance
28+
*/
2629
#options: Required<EncryptionOptions>
2730

2831
/**
@@ -50,11 +53,18 @@ export class Encryption {
5053

5154
/**
5255
* The algorithm in use
56+
*
57+
* @returns The encryption algorithm being used
5358
*/
5459
get algorithm(): 'aes-256-cbc' {
5560
return this.#options.algorithm
5661
}
5762

63+
/**
64+
* Creates a new Encryption instance with the provided options
65+
*
66+
* @param options - Configuration options for encryption
67+
*/
5868
constructor(options: EncryptionOptions) {
5969
const secretValue =
6070
options.secret && typeof options.secret === 'object' && 'release' in options.secret
@@ -69,8 +79,10 @@ export class Encryption {
6979

7080
/**
7181
* Validates the app secret
82+
*
83+
* @param secret - The secret to validate
7284
*/
73-
#validateSecret(secret?: string) {
85+
#validateSecret(secret?: string): void {
7486
if (typeof secret !== 'string') {
7587
throw new errors.E_MISSING_APP_KEY()
7688
}
@@ -93,8 +105,13 @@ export class Encryption {
93105
*
94106
* You can optionally define a purpose for which the value was encrypted and
95107
* mentioning a different purpose/no purpose during decrypt will fail.
108+
*
109+
* @param payload - The data to be encrypted
110+
* @param expiresIn - Optional expiration time
111+
* @param purpose - Optional purpose for which the value is encrypted
112+
* @returns The encrypted payload as a string
96113
*/
97-
encrypt(payload: any, expiresIn?: string | number, purpose?: string) {
114+
encrypt(payload: any, expiresIn?: string | number, purpose?: string): string {
98115
/**
99116
* Using a random string as the iv for generating unpredictable values
100117
*/
@@ -132,6 +149,10 @@ export class Encryption {
132149

133150
/**
134151
* Decrypt value and verify it against a purpose
152+
*
153+
* @param value - The encrypted value to decrypt
154+
* @param purpose - Optional purpose that the value was encrypted for
155+
* @returns The decrypted data if valid, null if decryption fails
135156
*/
136157
decrypt<T extends any>(value: unknown, purpose?: string): T | null {
137158
if (typeof value !== 'string') {
@@ -191,8 +212,11 @@ export class Encryption {
191212

192213
/**
193214
* Create a children instance with different secret key
215+
*
216+
* @param options - Optional configuration options to override
217+
* @returns A new Encryption instance with the merged options
194218
*/
195-
child(options?: EncryptionOptions) {
219+
child(options?: EncryptionOptions): Encryption {
196220
return new Encryption({ ...this.#options, ...options })
197221
}
198222
}

src/errors.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,20 @@
99

1010
import { createError } from '@poppinss/utils/exception'
1111

12+
/**
13+
* Error thrown when the application key is too short to be secure.
14+
* The application key must be at least 16 characters long to ensure
15+
* adequate security for encryption operations.
16+
*/
1217
export const E_INSECURE_APP_KEY = createError(
1318
'The value of "app.appKey" should be atleast 16 characters long',
1419
'E_INSECURE_APP_KEY'
1520
)
1621

22+
/**
23+
* Error thrown when the application key is missing from the configuration.
24+
* The application key is required for all encryption and decryption operations.
25+
*/
1726
export const E_MISSING_APP_KEY = createError(
1827
'Missing "app.appKey". The key is required to encrypt values',
1928
'E_MISSING_APP_KEY'

src/hmac.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,38 @@ import base64 from '@poppinss/utils/base64'
1616
* integrity.
1717
*/
1818
export class Hmac {
19+
/**
20+
* The cryptographic key used for HMAC generation
21+
*/
1922
#key: Buffer
2023

24+
/**
25+
* Creates a new HMAC instance with the provided cryptographic key
26+
*
27+
* @param key - The buffer containing the cryptographic key
28+
*/
2129
constructor(key: Buffer) {
2230
this.#key = key
2331
}
2432

2533
/**
2634
* Generate the hmac
35+
*
36+
* @param value - The string value to generate HMAC for
37+
* @returns The base64 URL encoded HMAC hash
2738
*/
28-
generate(value: string) {
39+
generate(value: string): string {
2940
return base64.urlEncode(createHmac('sha256', this.#key).update(value).digest('hex'))
3041
}
3142

3243
/**
3344
* Compare raw value against an existing hmac
45+
*
46+
* @param value - The original string value
47+
* @param existingHmac - The existing HMAC to compare against
48+
* @returns True if the HMACs match, false otherwise
3449
*/
35-
compare(value: string, existingHmac: string) {
50+
compare(value: string, existingHmac: string): boolean {
3651
return safeEqual(this.generate(value), existingHmac)
3752
}
3853
}

src/message_verifier.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ export class MessageVerifier {
3434
*/
3535
#separator = '.'
3636

37+
/**
38+
* Creates a new MessageVerifier instance with the provided secret
39+
*
40+
* @param secret - The secret key used for signing operations
41+
*/
3742
constructor(secret: string) {
3843
this.#cryptoKey = createHash('sha256').update(secret).digest()
3944
}
@@ -51,8 +56,13 @@ export class MessageVerifier {
5156
*
5257
* You can optionally define a purpose for which the value was signed and
5358
* mentioning a different purpose/no purpose during unsign will fail.
59+
*
60+
* @param payload - The data to be signed
61+
* @param expiresIn - Optional expiration time
62+
* @param purpose - Optional purpose for which the value is signed
63+
* @returns The signed payload as a string
5464
*/
55-
sign(payload: any, expiresIn?: string | number, purpose?: string) {
65+
sign(payload: any, expiresIn?: string | number, purpose?: string): string {
5666
if (payload === null || payload === undefined) {
5767
throw new RuntimeException(`Cannot sign "${payload}" value`)
5868
}
@@ -63,6 +73,10 @@ export class MessageVerifier {
6373

6474
/**
6575
* Unsign a previously signed value with an optional purpose
76+
*
77+
* @param payload - The signed payload string to verify
78+
* @param purpose - Optional purpose that the value was signed for
79+
* @returns The original data if valid, null if invalid or verification fails
6680
*/
6781
unsign<T extends any>(payload: string, purpose?: string): T | null {
6882
if (typeof payload !== 'string') {

0 commit comments

Comments
 (0)