@auth0-kits/server
Advanced tools
Comparing version 1.0.1 to 1.1.0
{ | ||
"name": "@auth0-kits/server", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "", | ||
@@ -13,4 +13,5 @@ "main": "index.js", | ||
"node-fetch": "^2.6.0", | ||
"querystring": "^0.2.0" | ||
"querystring": "^0.2.0", | ||
"untracer": "^1.0.0" | ||
} | ||
} |
const querystring = require('querystring'); | ||
const fetch = require('node-fetch'); | ||
const Tracer = require('untracer'); | ||
@@ -9,3 +10,25 @@ const TOKEN_URL = '/oauth/token'; | ||
class Auth0Service { | ||
constructor({ auth0TenantUrl, oauthRedirectUri, clientId, clientSecret }) { | ||
/** | ||
*Creates an instance of Auth0Service. | ||
* @param {*} { | ||
* auth0TenantUrl, | ||
* oauthRedirectUri, | ||
* clientId, | ||
* clientSecret, | ||
* debug = false, | ||
* tracer, | ||
* log, | ||
* } options | ||
* @param {Tracer?} options.tracer | ||
* @memberof Auth0Service | ||
*/ | ||
constructor({ | ||
auth0TenantUrl, | ||
oauthRedirectUri, | ||
clientId, | ||
clientSecret, | ||
debug = false, | ||
tracer, | ||
log, | ||
}) { | ||
this.auth0TenantUrl = auth0TenantUrl; | ||
@@ -15,2 +38,4 @@ this.clientId = clientId; | ||
this.oauthRedirectUri = oauthRedirectUri; | ||
this.tracer = tracer || new Tracer({ log, silent: !debug }); | ||
} | ||
@@ -27,4 +52,7 @@ | ||
async getManagementApiToken({ clientId, clientSecret } = {}) { | ||
this.tracer.trace('getManagementApiToken'); | ||
const client_id = clientId || this.clientId; | ||
const client_secret = clientSecret || this.clientSecret; | ||
this.tracer.crumb({ client_id, client_secret }); | ||
@@ -37,11 +65,31 @@ const body = querystring.stringify({ | ||
}); | ||
this.tracer.crumb({ body }); | ||
const response = await fetch(`${this.auth0TenantUrl}${TOKEN_URL}`, { | ||
method: 'POST', | ||
body, | ||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, | ||
}); | ||
const resToken = await response.json(); | ||
let response; | ||
try { | ||
const managementTokenUrl = `${this.auth0TenantUrl}${TOKEN_URL}`; | ||
this.tracer.crumb({ managementTokenUrl }); | ||
return (!resToken || !resToken.access_token) ? null : resToken.access_token; | ||
response = await fetch(managementTokenUrl, { | ||
method: 'POST', | ||
body, | ||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, | ||
}); | ||
const { headers, status, statusText } = response; | ||
this.tracer.crumb({ headers, status, statusText }); | ||
} catch (error) { | ||
throw this.tracer.break(error); | ||
} | ||
let tokenResponse; | ||
try { | ||
tokenResponse = await response.json(); | ||
this.tracer.crumb({ tokenResponse }); | ||
} catch (error) { | ||
throw this.tracer.break(error); | ||
} | ||
const result = (!resToken || !resToken.access_token) ? null : resToken.access_token; | ||
return this.tracer.dump(result); | ||
} | ||
@@ -57,10 +105,32 @@ | ||
async getUserInfo(userId) { | ||
const managementToken = await this.getManagementApiToken(); | ||
this.tracer.trace('getUserInfo', { userId }); | ||
let managementToken; | ||
try { | ||
managementToken = await this.getManagementApiToken({ logBreadcrumbs }); | ||
this.tracer.crumb({ managementToken }); | ||
if (!managementToken) { | ||
throw new Error('Cannot get user info because managementToken is null.'); | ||
} | ||
} catch (error) { | ||
throw this.tracer.break(error); | ||
} | ||
const url = `${this.auth0TenantUrl}${USER_URL}${userId}`; | ||
const response = await fetch(url, { | ||
headers: { | ||
'Authorization': `Bearer ${managementToken}`, | ||
}, | ||
}); | ||
return response.json(); | ||
this.tracer.crumb({ url }); | ||
let response; | ||
try { | ||
response = await fetch(url, { | ||
headers: { 'Authorization': `Bearer ${managementToken}` }, | ||
}); | ||
const { headers, status, statusText } = response; | ||
this.tracer.crumb({ headers, status, statusText }); | ||
} catch (error) { | ||
throw this.tracer.break(error); | ||
} | ||
const responseJson = await response.json(); | ||
return this.tracer.dump(responseJson); | ||
} | ||
@@ -76,2 +146,4 @@ | ||
async getUserAccessTokenByCode(authorizationCode) { | ||
this.tracer.trace('getUserAccessTokenByCode', { authorizationCode }); | ||
const body = querystring.stringify({ | ||
@@ -84,8 +156,23 @@ grant_type: 'authorization_code', | ||
}); | ||
const response = await fetch(`${this.auth0TenantUrl}${TOKEN_URL}`, { | ||
method: 'POST', | ||
body, | ||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, | ||
}); | ||
return response.json(); | ||
this.tracer.crumb({ body }); | ||
let response; | ||
try { | ||
const url = `${this.auth0TenantUrl}${TOKEN_URL}`; | ||
this.tracer.crumb({ url }); | ||
response = await fetch(url, { | ||
method: 'POST', | ||
body, | ||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, | ||
}); | ||
const { headers, status, statusText } = response; | ||
this.tracer.crumb({ headers, status, statusText }); | ||
} catch (error) { | ||
throw this.tracer.break(error); | ||
} | ||
const responseJson = await response.json(); | ||
return this.tracer.dump(responseJson); | ||
} | ||
@@ -101,2 +188,4 @@ | ||
async renewToken(refreshToken) { | ||
this.tracer.trace('renewToken', { refreshToken }); | ||
const body = querystring.stringify({ | ||
@@ -109,8 +198,23 @@ grant_type: 'refresh_token', | ||
}); | ||
const response = await fetch(`${this.auth0TenantUrl}${TOKEN_URL}`, { | ||
method: 'POST', | ||
body, | ||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, | ||
}); | ||
return response.json(); | ||
this.tracer.crumb({ body }); | ||
let response; | ||
try { | ||
const url = `${this.auth0TenantUrl}${TOKEN_URL}`; | ||
this.tracer.crumb({ url }); | ||
response = await fetch(url, { | ||
method: 'POST', | ||
body, | ||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, | ||
}); | ||
const { headers, status, statusText } = response; | ||
this.tracer.crumb({ headers, status, statusText }); | ||
} catch (error) { | ||
throw this.tracer.break(error); | ||
} | ||
const responseJson = await response.json(); | ||
return this.tracer.dump(responseJson); | ||
} | ||
@@ -117,0 +221,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
7700
213
5
3
+ Addeduntracer@^1.0.0
+ Addeduntracer@1.0.0(transitive)