faustjs icon indicating copy to clipboard operation
faustjs copied to clipboard

Documentation around Basic Authentication

Open helmutgranda opened this issue 2 years ago • 4 comments

Is there any documentation on how to extract data on a WPE WP hosted solution behind Basic Authentication?

I can't even extract the data from an instance behind BA, here is the output in the command line:

error - Unable to find a GraphQL endpoint at https://[projectname].wpengine.com/index.php?graphql error - WPGraphQL may not be active, or your WordPress site is unavailable.

If anyone could point me to the location where I can find the instructions or if anyone has started on them and needs help finish the work I will be more than happy to help. It is imperative that we keep the WP Instance the way it is and only extract the data to create the Front End.

helmutgranda avatar Aug 04 '23 17:08 helmutgranda

Hey @helmutgranda.

For custom authentication you will have to configure the Apollo client to pass the Authorization: Basic credentials header. You can do this by creating Faust Plugin for the apolloClientOptions filter:

import { setConfig, getGraphqlEndpoint } from '@faustwp/core';
import templates from './wp-templates';
import possibleTypes from './possibleTypes.json';
import { setContext } from '@apollo/client/link/context';
import { createHttpLink } from '@apollo/client';

const setAuthorizationLink = setContext((_, { headers }) => {
  return {
    headers: {
      ...headers,
      authorization: `Basic ""`
    }
  }
});

class BasicAuthPlugin {
  apply({ addFilter }) {
    addFilter('apolloClientOptions', 'faust', (apolloClientOptions) => {
      const existingLink = apolloClientOptions?.link;
      return {
        ...apolloClientOptions,
        link: setAuthorizationLink.concat(existingLink)
      }
    });
  }
}

/**
 * @type {import('@faustwp/core').FaustConfig}
 **/
export default setConfig({
  templates,
  experimentalPlugins: [new BasicAuthPlugin()],
  experimentalToolbar: false,
  possibleTypes,
});

Bear in mind though that this might break the existing Faust Auth system since you can't be having the same authorization headers.

See: https://github.com/wpengine/faustjs/issues/851#issuecomment-1126247893

theodesp avatar Aug 08 '23 10:08 theodesp

Hey @helmutgranda. Were you able to resolve your issues with auth?

theodesp avatar Aug 14 '23 09:08 theodesp

Hey @theodesp, I was able to add the plugin, thank you so much for providing it. However, I have to figure out where a barrier of entry is earlier. This is the error in the terminal when I run npm run dev

error - Unable to find a GraphQL endpoint at https://mysubdomain.wpengine.com/index.php?graphql
error - WPGraphQL may not be active, or your WordPress site is unavailable.

If I try to ping to an endpoint that does not contain a password then I am able to get the data without any issues, once I switch to a domain behind basic authentication I get the error above.

Any ideas on where it may be causing the site to not even be able to ping to the site?

helmutgranda avatar Aug 19 '23 04:08 helmutgranda

Faust Tries to do a health check when you run dev or prod builds. The fetch code is located here;

https://github.com/wpengine/faustjs/blob/canary/packages/faustwp-cli/src/healthCheck/verifyGraphQLEndpoint.ts#L9-L43

Is there is something needed to perform successfully since if you require a password there then this request will fail?

I suspect that we need to add a way to configure this request or skip this health check with an env variable.

theodesp avatar Aug 21 '23 09:08 theodesp