| 
									
										
										
										
											2019-10-15 04:12:46 -06:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var Keypairs = require('../'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* global Promise*/ | 
					
						
							|  |  |  | Keypairs.parseOrGenerate({ key: null }) | 
					
						
							| 
									
										
										
										
											2020-07-28 15:42:32 -06:00
										 |  |  | 	.then(function (pair) { | 
					
						
							| 
									
										
										
										
											2019-10-15 04:12:46 -06:00
										 |  |  | 		// should NOT have any warning output
 | 
					
						
							|  |  |  | 		if (!pair.private || !pair.public) { | 
					
						
							|  |  |  | 			throw new Error('missing key pairs'); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return Promise.all([ | 
					
						
							|  |  |  | 			// Testing Public Part of key
 | 
					
						
							| 
									
										
										
										
											2020-07-28 15:42:32 -06:00
										 |  |  | 			Keypairs.export({ jwk: pair.public }).then(function (pem) { | 
					
						
							| 
									
										
										
										
											2019-10-15 04:12:46 -06:00
										 |  |  | 				if (!/--BEGIN PUBLIC/.test(pem)) { | 
					
						
							|  |  |  | 					throw new Error('did not export public pem'); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				return Promise.all([ | 
					
						
							| 
									
										
										
										
											2020-07-28 15:42:32 -06:00
										 |  |  | 					Keypairs.parse({ key: pem }).then(function (pair) { | 
					
						
							| 
									
										
										
										
											2019-10-15 04:12:46 -06:00
										 |  |  | 						if (pair.private) { | 
					
						
							|  |  |  | 							throw new Error("shouldn't have private part"); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						return true; | 
					
						
							|  |  |  | 					}), | 
					
						
							|  |  |  | 					Keypairs.parse({ key: pem, private: true }) | 
					
						
							| 
									
										
										
										
											2020-07-28 15:42:32 -06:00
										 |  |  | 						.then(function () { | 
					
						
							| 
									
										
										
										
											2019-10-15 04:12:46 -06:00
										 |  |  | 							var err = new Error( | 
					
						
							|  |  |  | 								'should have thrown an error when private key was required and public pem was given' | 
					
						
							|  |  |  | 							); | 
					
						
							|  |  |  | 							err.code = 'NOERR'; | 
					
						
							|  |  |  | 							throw err; | 
					
						
							|  |  |  | 						}) | 
					
						
							| 
									
										
										
										
											2020-07-28 15:42:32 -06:00
										 |  |  | 						.catch(function (e) { | 
					
						
							| 
									
										
										
										
											2019-10-15 04:12:46 -06:00
										 |  |  | 							if ('NOERR' === e.code) { | 
					
						
							|  |  |  | 								throw e; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 							return true; | 
					
						
							|  |  |  | 						}) | 
					
						
							| 
									
										
										
										
											2020-07-28 15:42:32 -06:00
										 |  |  | 				]).then(function () { | 
					
						
							| 
									
										
										
										
											2019-10-15 04:12:46 -06:00
										 |  |  | 					return true; | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}), | 
					
						
							|  |  |  | 			// Testing Private Part of Key
 | 
					
						
							| 
									
										
										
										
											2020-07-28 15:42:32 -06:00
										 |  |  | 			Keypairs.export({ jwk: pair.private }).then(function (pem) { | 
					
						
							| 
									
										
										
										
											2019-10-15 04:12:46 -06:00
										 |  |  | 				if (!/--BEGIN .*PRIVATE KEY--/.test(pem)) { | 
					
						
							|  |  |  | 					throw new Error('did not export private pem: ' + pem); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				return Promise.all([ | 
					
						
							| 
									
										
										
										
											2020-07-28 15:42:32 -06:00
										 |  |  | 					Keypairs.parse({ key: pem }).then(function (pair) { | 
					
						
							| 
									
										
										
										
											2019-10-15 04:12:46 -06:00
										 |  |  | 						if (!pair.private) { | 
					
						
							|  |  |  | 							throw new Error('should have private part'); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						if (!pair.public) { | 
					
						
							|  |  |  | 							throw new Error('should have public part also'); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						return true; | 
					
						
							|  |  |  | 					}), | 
					
						
							| 
									
										
										
										
											2020-07-28 15:42:32 -06:00
										 |  |  | 					Keypairs.parse({ key: pem, public: true }).then(function ( | 
					
						
							| 
									
										
										
										
											2019-10-15 04:12:46 -06:00
										 |  |  | 						pair | 
					
						
							|  |  |  | 					) { | 
					
						
							|  |  |  | 						if (pair.private) { | 
					
						
							|  |  |  | 							throw new Error('should NOT have private part'); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						if (!pair.public) { | 
					
						
							|  |  |  | 							throw new Error( | 
					
						
							|  |  |  | 								'should have the public part though' | 
					
						
							|  |  |  | 							); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						return true; | 
					
						
							|  |  |  | 					}) | 
					
						
							| 
									
										
										
										
											2020-07-28 15:42:32 -06:00
										 |  |  | 				]).then(function () { | 
					
						
							| 
									
										
										
										
											2019-10-15 04:12:46 -06:00
										 |  |  | 					return true; | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}), | 
					
						
							|  |  |  | 			Keypairs.parseOrGenerate({ key: 'not a key', public: true }).then( | 
					
						
							| 
									
										
										
										
											2020-07-28 15:42:32 -06:00
										 |  |  | 				function (pair) { | 
					
						
							| 
									
										
										
										
											2019-10-15 04:12:46 -06:00
										 |  |  | 					// SHOULD have warning output
 | 
					
						
							|  |  |  | 					if (!pair.private || !pair.public) { | 
					
						
							|  |  |  | 						throw new Error( | 
					
						
							|  |  |  | 							"missing key pairs (should ignore 'public')" | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					if (!pair.parseError) { | 
					
						
							|  |  |  | 						throw new Error( | 
					
						
							|  |  |  | 							'should pass parseError for malformed string' | 
					
						
							|  |  |  | 						); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					return true; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			), | 
					
						
							| 
									
										
										
										
											2020-07-28 15:42:32 -06:00
										 |  |  | 			Keypairs.parse({ key: JSON.stringify(pair.private) }).then( | 
					
						
							|  |  |  | 				function (pair) { | 
					
						
							|  |  |  | 					if (!pair.private || !pair.public) { | 
					
						
							|  |  |  | 						throw new Error('missing key pairs (stringified jwt)'); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					return true; | 
					
						
							| 
									
										
										
										
											2019-10-15 04:12:46 -06:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2020-07-28 15:42:32 -06:00
										 |  |  | 			), | 
					
						
							| 
									
										
										
										
											2019-10-15 04:12:46 -06:00
										 |  |  | 			Keypairs.parse({ | 
					
						
							|  |  |  | 				key: JSON.stringify(pair.private), | 
					
						
							|  |  |  | 				public: true | 
					
						
							| 
									
										
										
										
											2020-07-28 15:42:32 -06:00
										 |  |  | 			}).then(function (pair) { | 
					
						
							| 
									
										
										
										
											2019-10-15 04:12:46 -06:00
										 |  |  | 				if (pair.private) { | 
					
						
							|  |  |  | 					throw new Error("has private key when it shouldn't"); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				if (!pair.public) { | 
					
						
							|  |  |  | 					throw new Error("doesn't have public key when it should"); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				return true; | 
					
						
							|  |  |  | 			}), | 
					
						
							|  |  |  | 			Keypairs.parse({ key: JSON.stringify(pair.public), private: true }) | 
					
						
							| 
									
										
										
										
											2020-07-28 15:42:32 -06:00
										 |  |  | 				.then(function () { | 
					
						
							| 
									
										
										
										
											2019-10-15 04:12:46 -06:00
										 |  |  | 					var err = new Error( | 
					
						
							|  |  |  | 						'should have thrown an error when private key was required and public jwk was given' | 
					
						
							|  |  |  | 					); | 
					
						
							|  |  |  | 					err.code = 'NOERR'; | 
					
						
							|  |  |  | 					throw err; | 
					
						
							|  |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2020-07-28 15:42:32 -06:00
										 |  |  | 				.catch(function (e) { | 
					
						
							| 
									
										
										
										
											2019-10-15 04:12:46 -06:00
										 |  |  | 					if ('NOERR' === e.code) { | 
					
						
							|  |  |  | 						throw e; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					return true; | 
					
						
							|  |  |  | 				}), | 
					
						
							|  |  |  | 			Keypairs.signJwt({ | 
					
						
							|  |  |  | 				jwk: pair.private, | 
					
						
							|  |  |  | 				// Note: using ES512 won't actually increase the length
 | 
					
						
							|  |  |  | 				// (it would be truncated to fit into the key size)
 | 
					
						
							|  |  |  | 				alg: 'ES256', | 
					
						
							|  |  |  | 				iss: 'https://example.com/', | 
					
						
							|  |  |  | 				exp: '1h' | 
					
						
							| 
									
										
										
										
											2020-07-28 15:42:32 -06:00
										 |  |  | 			}).then(function (jwt) { | 
					
						
							| 
									
										
										
										
											2019-10-15 04:12:46 -06:00
										 |  |  | 				var parts = jwt.split('.'); | 
					
						
							|  |  |  | 				var now = Math.round(Date.now() / 1000); | 
					
						
							|  |  |  | 				var token = { | 
					
						
							|  |  |  | 					header: JSON.parse(Buffer.from(parts[0], 'base64')), | 
					
						
							|  |  |  | 					payload: JSON.parse(Buffer.from(parts[1], 'base64')), | 
					
						
							|  |  |  | 					signature: parts[2] //Buffer.from(parts[2], 'base64')
 | 
					
						
							|  |  |  | 				}; | 
					
						
							|  |  |  | 				// allow some leeway just in case we happen to hit a 1ms boundary
 | 
					
						
							|  |  |  | 				if (token.payload.exp - now > 60 * 59.99) { | 
					
						
							|  |  |  | 					return true; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				throw new Error('token was not properly generated'); | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2020-07-28 15:42:32 -06:00
										 |  |  | 		]).then(function (results) { | 
					
						
							| 
									
										
										
										
											2019-10-15 04:12:46 -06:00
										 |  |  | 			if ( | 
					
						
							|  |  |  | 				results.length && | 
					
						
							| 
									
										
										
										
											2020-07-28 15:42:32 -06:00
										 |  |  | 				results.every(function (v) { | 
					
						
							| 
									
										
										
										
											2019-10-15 04:12:46 -06:00
										 |  |  | 					return true === v; | 
					
						
							|  |  |  | 				}) | 
					
						
							|  |  |  | 			) { | 
					
						
							|  |  |  | 				console.log('PASS'); | 
					
						
							|  |  |  | 				process.exit(0); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				throw new Error("didn't get all passes (but no errors either)"); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2020-07-28 15:42:32 -06:00
										 |  |  | 	.catch(function (e) { | 
					
						
							| 
									
										
										
										
											2019-10-15 04:12:46 -06:00
										 |  |  | 		console.error('Caught an unexpected (failing) error:'); | 
					
						
							|  |  |  | 		console.error(e); | 
					
						
							|  |  |  | 		process.exit(1); | 
					
						
							|  |  |  | 	}); |