mostly stylistic changes
This commit is contained in:
		
							parent
							
								
									393d25621c
								
							
						
					
					
						commit
						2bdf6ffc71
					
				
							
								
								
									
										124
									
								
								lib/index.js
									
									
									
									
									
								
							
							
						
						
									
										124
									
								
								lib/index.js
									
									
									
									
									
								
							@ -11,22 +11,12 @@ var defaults = {
 | 
				
			|||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports.create = function(config) {
 | 
					module.exports.create = function(config) {
 | 
				
			||||||
	var baseUrl = config.baseUrl || defaults.baseUrl;
 | 
						var baseUrl = (config.baseUrl || defaults.baseUrl).replace(/\/$/, '');
 | 
				
			||||||
	var apiKey = config.key || config.apiKey;
 | 
						var apiKey = config.key || config.apiKey;
 | 
				
			||||||
	var apiSecret = config.secret || config.apiSecret;
 | 
						var apiSecret = config.secret || config.apiSecret;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var auth = 'sso-key ' + apiKey + ':' + apiSecret;
 | 
						var auth = 'sso-key ' + apiKey + ':' + apiSecret;
 | 
				
			||||||
 | 
						var redacted = 'sso-key ' + apiKey + ':[redacted]';
 | 
				
			||||||
	function api(method, path, form) {
 | 
					 | 
				
			||||||
		return request({
 | 
					 | 
				
			||||||
			method: method,
 | 
					 | 
				
			||||||
			url: baseUrl + path,
 | 
					 | 
				
			||||||
			headers: {
 | 
					 | 
				
			||||||
				Authorization: auth
 | 
					 | 
				
			||||||
			},
 | 
					 | 
				
			||||||
			json: form || true
 | 
					 | 
				
			||||||
		});
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return {
 | 
						return {
 | 
				
			||||||
		init: function(deps) {
 | 
							init: function(deps) {
 | 
				
			||||||
@ -35,13 +25,7 @@ module.exports.create = function(config) {
 | 
				
			|||||||
		},
 | 
							},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		zones: function(data) {
 | 
							zones: function(data) {
 | 
				
			||||||
			return api('GET', 'domains?statuses=ACTIVE').then(function(resp) {
 | 
								return api('GET', '/domains?statuses=ACTIVE').then(function(resp) {
 | 
				
			||||||
				if (200 !== resp.statusCode) {
 | 
					 | 
				
			||||||
					console.error(resp.statusCode);
 | 
					 | 
				
			||||||
					console.error(resp.body);
 | 
					 | 
				
			||||||
					throw new Error('Could not get list of zones. Check api key, etc');
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
				return resp.body.map(function(x) {
 | 
									return resp.body.map(function(x) {
 | 
				
			||||||
					return x.domain;
 | 
										return x.domain;
 | 
				
			||||||
				});
 | 
									});
 | 
				
			||||||
@ -67,17 +51,9 @@ module.exports.create = function(config) {
 | 
				
			|||||||
				}
 | 
									}
 | 
				
			||||||
			];
 | 
								];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			return api('PATCH', 'domains/' + ch.dnsZone + '/records', records).then(
 | 
								return api('PATCH', '/domains/' + ch.dnsZone + '/records', records).then(
 | 
				
			||||||
				function(resp) {
 | 
									function(resp) {
 | 
				
			||||||
					if (200 !== resp.statusCode) {
 | 
										return null;
 | 
				
			||||||
						console.error(resp.statusCode);
 | 
					 | 
				
			||||||
						console.error(resp.body);
 | 
					 | 
				
			||||||
						throw new Error(
 | 
					 | 
				
			||||||
							'record did not set. check subdomain, api key, etc'
 | 
					 | 
				
			||||||
						);
 | 
					 | 
				
			||||||
					}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
					return true;
 | 
					 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
			);
 | 
								);
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
@ -87,25 +63,15 @@ module.exports.create = function(config) {
 | 
				
			|||||||
			// get all domain records of type or name
 | 
								// get all domain records of type or name
 | 
				
			||||||
			return api(
 | 
								return api(
 | 
				
			||||||
				'GET',
 | 
									'GET',
 | 
				
			||||||
				'domains/' + ch.dnsZone + '/records/TXT/' + ch.dnsPrefix
 | 
									'/domains/' + ch.dnsZone + '/records/TXT/' + ch.dnsPrefix
 | 
				
			||||||
			)
 | 
								)
 | 
				
			||||||
				.then(function(resp) {
 | 
									.then(function(resp) {
 | 
				
			||||||
					if (200 !== resp.statusCode) {
 | 
										// keep all TXT records that we don't need to remove
 | 
				
			||||||
						console.error(resp.statusCode);
 | 
										return resp.body.filter(function(el) {
 | 
				
			||||||
						console.error(resp.body);
 | 
											return el.data !== ch.dnsAuthorization;
 | 
				
			||||||
						throw new Error('Could not get list of zones. Check api key, etc');
 | 
										});
 | 
				
			||||||
					}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
					var newEntries = [];
 | 
					 | 
				
			||||||
					// filter all TXT records without record to remove
 | 
					 | 
				
			||||||
					for (let i = 0; i < resp.body.length; i++) {
 | 
					 | 
				
			||||||
						if (resp.body[i]['data'] !== ch.dnsAuthorization) {
 | 
					 | 
				
			||||||
							newEntries.push(resp.body[i]);
 | 
					 | 
				
			||||||
						}
 | 
					 | 
				
			||||||
					}
 | 
					 | 
				
			||||||
					return newEntries;
 | 
					 | 
				
			||||||
				})
 | 
									})
 | 
				
			||||||
				.then(function(newRecords) {
 | 
									.then(function(records) {
 | 
				
			||||||
					// godaddy doesn't provide an endpoint for a single record removal
 | 
										// godaddy doesn't provide an endpoint for a single record removal
 | 
				
			||||||
					// but provides this endpoint to replace all records of a given type
 | 
										// but provides this endpoint to replace all records of a given type
 | 
				
			||||||
					// https://developer.godaddy.com/doc/endpoint/domains#/v1/recordReplaceType
 | 
										// https://developer.godaddy.com/doc/endpoint/domains#/v1/recordReplaceType
 | 
				
			||||||
@ -113,8 +79,8 @@ module.exports.create = function(config) {
 | 
				
			|||||||
					// hence when only a single record of type exists and is the one to remove
 | 
										// hence when only a single record of type exists and is the one to remove
 | 
				
			||||||
					// we call the endpoint with this dummy record
 | 
										// we call the endpoint with this dummy record
 | 
				
			||||||
 | 
					
 | 
				
			||||||
					if (!newRecords.length) {
 | 
										if (!records.length) {
 | 
				
			||||||
						newRecords.push({
 | 
											records.push({
 | 
				
			||||||
							data: 'free_to_delete',
 | 
												data: 'free_to_delete',
 | 
				
			||||||
							name: 'remove_placeholder',
 | 
												name: 'remove_placeholder',
 | 
				
			||||||
							ttl: 600
 | 
												ttl: 600
 | 
				
			||||||
@ -124,17 +90,10 @@ module.exports.create = function(config) {
 | 
				
			|||||||
					// update - overwrite all type and name records under domain
 | 
										// update - overwrite all type and name records under domain
 | 
				
			||||||
					return api(
 | 
										return api(
 | 
				
			||||||
						'PUT',
 | 
											'PUT',
 | 
				
			||||||
						'domains/' + ch.dnsZone + '/records/TXT',
 | 
											'/domains/' + ch.dnsZone + '/records/TXT',
 | 
				
			||||||
						newRecords
 | 
											records
 | 
				
			||||||
					).then(function(resp) {
 | 
										).then(function(resp) {
 | 
				
			||||||
						if (200 !== resp.statusCode) {
 | 
											return null;
 | 
				
			||||||
							console.error(resp.statusCode);
 | 
					 | 
				
			||||||
							console.error(resp.body);
 | 
					 | 
				
			||||||
							throw new Error(
 | 
					 | 
				
			||||||
								'record did not remove. check subdomain, api key, etc'
 | 
					 | 
				
			||||||
							);
 | 
					 | 
				
			||||||
						}
 | 
					 | 
				
			||||||
						return true;
 | 
					 | 
				
			||||||
					});
 | 
										});
 | 
				
			||||||
				});
 | 
									});
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
@ -142,20 +101,53 @@ module.exports.create = function(config) {
 | 
				
			|||||||
			var ch = data.challenge;
 | 
								var ch = data.challenge;
 | 
				
			||||||
			return api(
 | 
								return api(
 | 
				
			||||||
				'GET',
 | 
									'GET',
 | 
				
			||||||
				'domains/' + ch.dnsZone + '/records/TXT/' + ch.dnsPrefix
 | 
									'/domains/' + ch.dnsZone + '/records/TXT/' + ch.dnsPrefix
 | 
				
			||||||
			).then(function(resp) {
 | 
								).then(function(resp) {
 | 
				
			||||||
				resp = resp.body;
 | 
									var entry = (resp.body || []).filter(function(x) {
 | 
				
			||||||
 | 
										return x.data === ch.dnsAuthorization;
 | 
				
			||||||
 | 
									})[0];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				var entry = null;
 | 
									if (entry) {
 | 
				
			||||||
 | 
										return { dnsAuthorization: entry.data };
 | 
				
			||||||
				if (resp.length) {
 | 
					 | 
				
			||||||
					entry = resp.filter(function(x) {
 | 
					 | 
				
			||||||
						return x.data === ch.dnsAuthorization;
 | 
					 | 
				
			||||||
					})[0];
 | 
					 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				return entry ? { dnsAuthorization: entry.data } : null;
 | 
									return null;
 | 
				
			||||||
			});
 | 
								});
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	};
 | 
						};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						function api(method, path, data) {
 | 
				
			||||||
 | 
							var req = {
 | 
				
			||||||
 | 
								method: method,
 | 
				
			||||||
 | 
								url: baseUrl + path,
 | 
				
			||||||
 | 
								headers: { Authorization: auth },
 | 
				
			||||||
 | 
								json: data || true
 | 
				
			||||||
 | 
							};
 | 
				
			||||||
 | 
							return request(req).then(function(resp) {
 | 
				
			||||||
 | 
								if (resp.statusCode < 200 || resp.statusCode >= 300) {
 | 
				
			||||||
 | 
									req.headers.Authorization = redacted;
 | 
				
			||||||
 | 
									console.error();
 | 
				
			||||||
 | 
									console.error(req.method + ' ' + req.url);
 | 
				
			||||||
 | 
									console.error(req.headers);
 | 
				
			||||||
 | 
									if (data) {
 | 
				
			||||||
 | 
										console.error(data);
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									console.error();
 | 
				
			||||||
 | 
									console.error(resp.statusCode);
 | 
				
			||||||
 | 
									console.error(resp.body);
 | 
				
			||||||
 | 
									console.error();
 | 
				
			||||||
 | 
									throw new Error(
 | 
				
			||||||
 | 
										'Request "' +
 | 
				
			||||||
 | 
											req.method +
 | 
				
			||||||
 | 
											' ' +
 | 
				
			||||||
 | 
											req.url +
 | 
				
			||||||
 | 
											'" failed: ' +
 | 
				
			||||||
 | 
											resp.statusCode +
 | 
				
			||||||
 | 
											' ' +
 | 
				
			||||||
 | 
											JSON.stringify(resp.body) +
 | 
				
			||||||
 | 
											'. Check subdomain, api key, etc'
 | 
				
			||||||
 | 
									);
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							});
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user