Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function token(event, context) {
const EARTHDATA_CLIENT_ID = process.env.EARTHDATA_CLIENT_ID;
const EARTHDATA_CLIENT_PASSWORD = process.env.EARTHDATA_CLIENT_PASSWORD;
const EARTHDATA_BASE_URL = process.env.EARTHDATA_BASE_URL || 'https://uat.urs.earthdata.nasa.gov';
const EARTHDATA_CHECK_CODE_URL = `${EARTHDATA_BASE_URL}/oauth/token`;
const code = get(event, 'queryStringParameters.code');
const state = get(event, 'queryStringParameters.state');
// Code contains the value from the Earthdata Login redirect. We use it to get a token.
if (code) {
const params = `?grant_type=authorization_code&code=${code}&redirect_uri=${redirectUriParam()}`;
// Verify token
return got.post(EARTHDATA_CHECK_CODE_URL + params, {
json: true,
auth: `${EARTHDATA_CLIENT_ID}:${EARTHDATA_CLIENT_PASSWORD}`
}).then((r) => {
const tokenInfo = r.body;
const accessToken = tokenInfo.access_token;
// if no access token is given, then the code is wrong
if (typeof accessToken === 'undefined') {
return resp(context, new Error('Failed to get Earthdata token'));
}
const refresh = tokenInfo.refresh_token;
const userName = tokenInfo.endpoint.split('/').pop();
const expires = (+new Date()) + (tokenInfo.expires_in * 1000);
const u = new User();
if (playlistName === undefined){
playlistName = response.body.name;
}
// holds playlist tracks
let tracks = responseTracks.map(responseTrack => responseTrack.track.uri)
var createPlaylistOptions = {
json: true,
headers: {
'Content-type': 'application/json',
'Authorization' : `Bearer ${config.get('bearer')}`,
'Accept' : 'application/json'
},
body: JSON.stringify({ description: `spotiforked from ${playlistUser}/${forkedFrom}`, name: `${playlistName}`, public : true})
};
// create playlist
got.post(`https://api.spotify.com/v1/users/${config.get('username')}/playlists`, createPlaylistOptions)
.then(response => {
// get playlist ID
const newPlaylistID = response.body.id;
// function to add tracks to playlist
function populatePlaylist (id, uris, name) {
var url = `https://api.spotify.com/v1/users/${config.get('username')}/playlists/${id}/tracks?uris=${uris}`
var options = {
json: true,
headers: {
'Content-type': 'application/json',
'Authorization' : `Bearer ${config.get('bearer')}`,
}
};
got.post(url, options)
.then(response => {
async upload (form) {
const { body } = await got.post('https://api.imgur.com/3/image', {
headers: {
Authorization: `Client-ID ${this.options.key}`
},
body: form,
form: true,
json: true
})
if (body && body.success) {
return body.data.link.replace('http:', 'https:')
} else {
console.log(body)
}
}
requestAccessToken(authorizationCode) {
return got.post(
this.tokenEndpoint(),
{
json: true,
form: true,
body: {
grant_type: 'authorization_code',
code: authorizationCode,
redirect_uri: this.redirectUri.toString()
},
auth: `${this.clientId}:${this.clientPassword}`
}
);
}
_call(dataStr) {
return got
.post(this._hostPort, {
encoding: this._encoding,
headers: {
'Content-Type': 'application/json'
},
body: dataStr
})
.then(res => {
return res.body;
});
}
}
sourceMapPath,
store,
}: {
accessKey: string
bundlePath: string
platform: string
sourceMapPath: string
store: string
}): Promise {
try {
const bundleFileRs = fs.createReadStream(bundlePath)
const sourceMapFileRs = fs.createReadStream(sourceMapPath)
const form = new FormData()
form.append('bundle', bundleFileRs)
form.append('sourcemap', sourceMapFileRs)
const res = await got.post(
`http://${this.host}/bundles/${store}/${platform}`,
{
...this.gotCommonOpts,
body: form,
headers: {
'ERN-BUNDLE-STORE-ACCESS-KEY': accessKey,
},
}
)
return JSON.parse(res.body).id
} catch (err) {
throw new Error(err.response?.text ?? err.message)
}
}
const r = (host, body, headers = {}) =>
got.post(`${host}/json`, {
body,
headers: { ...headers, 'Content-Type': 'application/json' },
json: true,
})
export async function requestGraphQL(
query: string,
variables: any = {},
{ instanceUrl, accessToken }: Options
): Promise<{ data?: any; errors?: { message: string; path: string }[] }> {
const headers: Record = {
Accept: 'application/json',
'Content-Type': 'application/json',
'User-Agent': 'TypeScript language server',
}
if (accessToken) {
headers.Authorization = 'token ' + accessToken
}
const response = await got.post(new URL('/.api/graphql', instanceUrl), {
headers,
body: { query, variables },
json: true,
})
return response.body
}
static async createUser({ email, password }) {
const body = await got.post(`${USERS_SERVICE_URI}/users`, { json: { email, password } }).json();
return body;
}
static async createUserSession({ email, password }) {
const body = await got.post(`${USERS_SERVICE_URI}/sessions`, { json: { email, password } }).json();
return body;
}