Skip to content
This repository was archived by the owner on Feb 24, 2021. It is now read-only.

Commit 4f084c1

Browse files
authored
Merge pull request #76 from arsdragonfly/wip
Add an option to include profile in JWT
2 parents af97313 + 74f4e0d commit 4f084c1

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

packages/ooth-jwt/src/index.ts

+36-24
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,43 @@ type Options = {
1919
privateKey?: string;
2020
publicKey?: string;
2121
algorithm?: string;
22+
includeProfile?: boolean;
2223
};
2324

2425
type TokenOptions = {
2526
sharedSecret?: string;
2627
privateKey?: string;
2728
publicKey?: string;
2829
algorithm?: string;
29-
}
30+
includeProfile?: boolean;
31+
};
3032

3133
function randomToken(): string {
3234
return randomBytes(43).toString('hex');
3335
}
3436

35-
export function getToken(userId: string, iat: number, tokenExpiry: number, options: TokenOptions): string {
36-
const data = {
37-
iat,
38-
exp: iat + tokenExpiry,
39-
_id: userId,
40-
};
37+
export async function getToken(userId: string, iat: number, tokenExpiry: number, options: TokenOptions, ooth: Ooth): Promise<string> {
38+
const data: {[key: string]: any} = {
39+
iat,
40+
exp: iat + tokenExpiry,
41+
_id: userId,
42+
};
4143

42-
if(options.sharedSecret) {
43-
return sign(data, options.sharedSecret);
44-
}
45-
46-
if (options.privateKey) {
47-
return sign(data, options.privateKey, {
48-
algorithm: options.algorithm
49-
});
44+
if (options.includeProfile) {
45+
data.user = ooth.getProfile(await ooth.getUserById(userId));
46+
}
47+
48+
if (options.sharedSecret) {
49+
return sign(data, options.sharedSecret);
50+
}
51+
52+
if (options.privateKey) {
53+
return sign(data, options.privateKey, {
54+
algorithm: options.algorithm,
55+
});
56+
}
57+
throw new Error('No secret nor key provided');
5058
}
51-
throw new Error('No secret nor key provided');
52-
}
5359

5460
export default function({
5561
name = 'jwt',
@@ -59,23 +65,29 @@ export default function({
5965
sharedSecret,
6066
privateKey,
6167
publicKey,
62-
algorithm = 'RS256'
68+
algorithm = 'RS256',
69+
includeProfile = false,
6370
}: Options): void {
64-
if(sharedSecret === undefined && privateKey === undefined) {
71+
72+
if (sharedSecret === undefined && privateKey === undefined) {
6573
throw new Error('Either sharedSecret or privateKey/publicKey pair is required');
6674
}
67-
if(sharedSecret !== undefined && privateKey !== undefined) {
75+
if (sharedSecret !== undefined && privateKey !== undefined) {
6876
throw new Error('Either sharedSecret or privateKey should be provided, not both');
6977
}
70-
if(privateKey !== undefined && publicKey === undefined) {
78+
if (privateKey !== undefined && publicKey === undefined) {
7179
throw new Error('publicKey is required with privateKey');
7280
}
7381
// Return jwt after successful (primary) auth
7482
ooth.registerAuthAfterware(async (result: { [key: string]: any }, userId: string | undefined) => {
7583
if (userId) {
76-
result.token = getToken(userId, new Date().getTime() / 1000, tokenExpiry, {
77-
sharedSecret, privateKey, publicKey, algorithm
78-
});
84+
result.token = await getToken(userId, new Date().getTime() / 1000, tokenExpiry, {
85+
sharedSecret,
86+
privateKey,
87+
publicKey,
88+
algorithm,
89+
includeProfile,
90+
}, ooth);
7991

8092
const refreshToken = randomToken();
8193
const now = new Date();

0 commit comments

Comments
 (0)