Files
graphql-api-js/src/rest/cognitoService.ts
T
Arwid ThornströmandGitHub c68c182b64 3010: Apollo 4 major update and complete security overhaul to prepare for public api support
* 2919: rewrite for update to apollo 4 (#131)

* rewrite for update to apollo 4

* removed container tests, edited make to publish any branch to staging

* revert makefile

* cleanup

* update 1

* more fixes to get it working on aws

* enabled csrf prevention protection, testing if build works

* switched to express version

* changed the healthcheck url

* added cache to restdatasources

* 3011: create a login mutation on graphql server that responds with a access token (#133)

* 3011 added login rest endpoint and general scope fixes

* changed login auth to basic auth username and password

* minor changes from CR feedback

* force update

* added tighter timeout for idle knex connection

* Refactor authentication and scopes (#134)

* big refactor of scopes in graphql

* cr fixes

* some security fixes (#135)

* some security fixes

* cleanup

* update staging

* minor readme change
2022-12-12 12:58:24 +01:00

41 lines
1.2 KiB
TypeScript

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,
};
};