@@ -19,37 +19,43 @@ type Options = {
19
19
privateKey ?: string ;
20
20
publicKey ?: string ;
21
21
algorithm ?: string ;
22
+ includeProfile ?: boolean ;
22
23
} ;
23
24
24
25
type TokenOptions = {
25
26
sharedSecret ?: string ;
26
27
privateKey ?: string ;
27
28
publicKey ?: string ;
28
29
algorithm ?: string ;
29
- }
30
+ includeProfile ?: boolean ;
31
+ } ;
30
32
31
33
function randomToken ( ) : string {
32
34
return randomBytes ( 43 ) . toString ( 'hex' ) ;
33
35
}
34
36
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
+ } ;
41
43
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' ) ;
50
58
}
51
- throw new Error ( 'No secret nor key provided' ) ;
52
- }
53
59
54
60
export default function ( {
55
61
name = 'jwt' ,
@@ -59,23 +65,29 @@ export default function({
59
65
sharedSecret,
60
66
privateKey,
61
67
publicKey,
62
- algorithm = 'RS256'
68
+ algorithm = 'RS256' ,
69
+ includeProfile = false ,
63
70
} : Options ) : void {
64
- if ( sharedSecret === undefined && privateKey === undefined ) {
71
+
72
+ if ( sharedSecret === undefined && privateKey === undefined ) {
65
73
throw new Error ( 'Either sharedSecret or privateKey/publicKey pair is required' ) ;
66
74
}
67
- if ( sharedSecret !== undefined && privateKey !== undefined ) {
75
+ if ( sharedSecret !== undefined && privateKey !== undefined ) {
68
76
throw new Error ( 'Either sharedSecret or privateKey should be provided, not both' ) ;
69
77
}
70
- if ( privateKey !== undefined && publicKey === undefined ) {
78
+ if ( privateKey !== undefined && publicKey === undefined ) {
71
79
throw new Error ( 'publicKey is required with privateKey' ) ;
72
80
}
73
81
// Return jwt after successful (primary) auth
74
82
ooth . registerAuthAfterware ( async ( result : { [ key : string ] : any } , userId : string | undefined ) => {
75
83
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 ) ;
79
91
80
92
const refreshToken = randomToken ( ) ;
81
93
const now = new Date ( ) ;
0 commit comments