import axios from 'axios'; import { Scopes } from '../cognito/access-control'; export const getClientAccessToken = async (req) => { const token = req.headers.authorization || ''; const tokenKey = Buffer.from( `${process.env.WEB_CLIENT_USERNAME}:${process.env.WEB_CLIENT_PASSWORD}`, ).toString('base64'); if (token !== `Basic ${tokenKey}`) { return Promise.reject(new Error('password or username did not match')); } const clientId = `${process.env.COGNITO_LOGIN_CLIENT_ID}`; const cognitoAppUrl = `${process.env.COGNITO_LOGIN_CLIENT_URL}`; const authString = 'Basic ' + Buffer.from( clientId + ':' + process.env.COGNITO_LOGIN_CLIENT_SECRET, ).toString('base64'); const res = await axios({ url: cognitoAppUrl + '/oauth2/token', method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', Authorization: authString, }, data: { grant_type: 'client_credentials', client_id: clientId, scope: `https://graphql.photowall.com/${Scopes.PRODUCTS_PUBLIC_READ} https://graphql.photowall.com/${Scopes.DESIGNERS_PUBLIC_READ}`, }, }); return { access_token: res.data.access_token, expires_in: res.data.expires_in, }; };