20
20
*/
21
21
'use strict' ;
22
22
23
- require ( 'date-utils' ) ;
24
23
var jwtConstants = require ( './constants' ) . Jwt ;
25
- var jwt = require ( 'jwt-simple' ) ;
26
24
var Logger = require ( './log' ) . Logger ;
25
+ var util = require ( './util' ) ;
26
+
27
+ var crypto = require ( 'crypto' ) ;
28
+ require ( 'date-utils' ) ;
29
+ var uuid = require ( 'node-uuid' ) ;
27
30
28
- function SelfSignedJwt ( callContext , authority , clientId , certificate ) {
31
+ function SelfSignedJwt ( callContext , authority , clientId ) {
29
32
this . _log = new Logger ( 'SelfSignedJwt' , callContext . _logContext ) ;
30
33
this . _callContext = callContext ;
31
34
32
35
this . _tokenEndpoint = authority . tokenEndpoint ;
33
- this . _certificate = certificate ;
34
36
this . _clientId = clientId ;
35
37
}
36
38
37
- SelfSignedJwt . prototype . create = function ( ) {
38
- var now = new Date ( ) ;
39
- // var expires = Date.now().add({ minutes: jwtConstants.SELF_SIGNED_JWT_LIFETIME });
39
+ SelfSignedJwt . prototype . _createHeader = function ( thumbprint ) {
40
+ var header = { typ : 'JWT' , alg : 'RS256' , x5t : thumbprint } ;
41
+
42
+ this . _log . verbose ( 'Creating self signed JWT header. Thumbprint: ' + thumbprint ) ;
43
+
44
+ return header ;
45
+ } ;
46
+
47
+ SelfSignedJwt . prototype . _createPayload = function ( ) {
48
+ var now = new Date ( ) ;
40
49
var expires = ( new Date ( ) ) . addMinutes ( jwtConstants . SELF_SIGNED_JWT_LIFETIME ) ;
41
50
42
- var jwtPayload = { } ;
43
- jwtPayload [ jwtConstants . AUDIENCE ] = this . _authority ;
44
- jwtPayload [ jwtConstants . ISSUER ] = this . _clientId ;
45
- jwtPayload [ jwtConstants . SUBJECT ] = this . _clientId ;
46
- jwtPayload [ jwtConstants . NOT_BEFORE ] = now . getTime ( ) ;
47
- jwtPayload [ jwtConstants . EXPIRES_ON ] = expires . getTime ( ) ;
51
+ this . _log . verbose ( 'Creating self signed JWT payload. Expires: ' + expires + ' NotBefore: ' + now ) ;
52
+
53
+ var jwtPayload = { } ;
54
+ jwtPayload [ jwtConstants . AUDIENCE ] = this . _authority ;
55
+ jwtPayload [ jwtConstants . ISSUER ] = this . _clientId ;
56
+ jwtPayload [ jwtConstants . SUBJECT ] = this . _clientId ;
57
+ jwtPayload [ jwtConstants . NOT_BEFORE ] = now . getTime ( ) ;
58
+ jwtPayload [ jwtConstants . EXPIRES_ON ] = expires . getTime ( ) ;
59
+ jwtPayload [ jwtConstants . JWT_ID ] = uuid . v4 ( ) ;
60
+
61
+ return jwtPayload ;
62
+ } ;
63
+
64
+ SelfSignedJwt . prototype . create = function ( certificate , thumbprint ) {
65
+ var header = this . _createHeader ( thumbprint ) ;
66
+ var payload = this . _createPayload ( ) ;
67
+
68
+ var headerString = util . base64EncodeStringUrlSafe ( JSON . stringify ( header ) ) ;
69
+ var payloadString = util . base64EncodeStringUrlSafe ( JSON . stringify ( payload ) ) ;
70
+ var stringToSign = headerString + '.' + payloadString ;
71
+
72
+ var signature = util . base64EncodeStringUrlSafe ( crypto . createSign ( 'RSA-SHA256' ) . update ( stringToSign ) . sign ( certificate , 'base64' ) ) ;
48
73
49
- return jwt . encode ( jwtPayload , this . _certificate , 'RS256' ) ;
74
+ return stringToSign + '.' + signature ;
50
75
} ;
51
76
52
77
module . exports . SelfSignedJwt = SelfSignedJwt ;
0 commit comments