docusaurus icon indicating copy to clipboard operation
docusaurus copied to clipboard

Access restriction by password protection

Open azcadev opened this issue 8 years ago • 21 comments

🚀 Feature

I want to make a wiki for my enterprise, but it would contain some sensitive information as well. I need some kind of restriction that only allows authenticated users to read the wiki contents. Could you please consider this request to allow us protecting our wiki ? Thanks in advance.

azcadev avatar Sep 13 '18 13:09 azcadev

@Azcatchi As Docusaurus generates static content, there's no way for Docusaurus to add auth restrictions. This has to be done on the web server layer which is beyond the scope of Docusaurus. I recommend that you look up the docs for adding auth to the web server you are using or put your contents behind a VPN.

Closing this issue as it's out of scope.

yangshun avatar Oct 14 '18 22:10 yangshun

@Azcatchi As Docusaurus generates static content, there's no way for Docusaurus to add auth restrictions. This has to be done on the web server layer which is beyond the scope of Docusaurus. I recommend that you look up the docs for adding auth to the web server you are using or put your contents behind a VPN.

Closing this issue as it's out of scope.

I think this answer is outdated. Docosaurus essentially is SPA, so password addition is not impossible.

TautvydasDerzinskas avatar Mar 02 '22 14:03 TautvydasDerzinskas

I think this answer is outdated. Docosaurus essentially is SPA, so password addition is not impossible.

Yes, it's indeed possible, but it's absolutely out of scope. You are welcome to swizzle Root and add your authentication layer.

Josh-Cena avatar Mar 02 '22 14:03 Josh-Cena

I'm referring to not using any external auth services for server-side password protection. That's still not possible.

yangshun avatar Mar 02 '22 15:03 yangshun

Some possible solutions to protect a Docusaurus site:

    1. password protection can be provided by some static hosts / CDN: https://docs.netlify.com/visitor-access/password-protection/
    1. CDNs with edge serverless functions (Cloudflare workers etc) can allow more, like cookie or OAuth workflow on top of a static website: https://github.com/gr2m/cloudflare-worker-github-oauth-login
    1. Add Client-Side-Rendering routes and fetch the content from a protected API, similar to Gatsby: https://www.gatsbyjs.com/docs/how-to/routing/client-only-routes-and-user-authentication/

I think 3) is a bit in the scope of Docusaurus. We probably have all the technical primitives to make this possible already, and just need to make a POC + document this.

  • addRoute({path: "/admin/*",component: "@site/src/Admin"})
  • Admin should statically render a spinner or login page on the server => /admin/index.html
  • add Netlify redirect from /admin/* to /admin/index.html (for all i18n languages)
  • Use client-side route components in Admin after mount
  • Use API with authentication to fetch data at runtime in Admin routes

If someone wants to work on a POC + doc that could be nice, otherwise I'll do that later, not a high priority


Note with Cloudflare workers and other edge functions, you can do some quite advanced things. For example, you could build 2 versions of your static website (one for anonymous, one for logged-in users). The edge function can check the auth cookie and serve one static site or the other accordingly

slorber avatar Mar 02 '22 15:03 slorber

Hi there, not sure if this is what is being referred to, but you can easily create an Express Server with Basic Auth for your Docusaurus site and use the Express.static method to serve the build folder.

Run npm run build and you will see a build folder is dynamically created in your project and then

You will need to add

"type": "module", 

and the script

"start": "node --experimental-modules src/app.js",

to your package.json

and create an app.js file within the src directory and add the code

import express from 'express';
import dotenv from 'dotenv';

dotenv.config();

const app = express();

app.use(express.json());

const authorize = ((req, res, next) => {

    const auth = {login: process.env.USERNAME, password: process.env.PASSWORD} // change this

    // parse login and password from headers
    const b64auth = (req.headers.authorization || '').split(' ')[1] || ''
    const [login, password] = Buffer.from(b64auth, 'base64').toString().split(':')
  
    // Verify login and password are set and correct
    if (login && password && login === auth.login && password === auth.password) {
      // Access granted...
      return next()
    }
  
    // Access denied...
    res.set('WWW-Authenticate', 'Basic realm="401"') // change this if desired
    res.status(401).send('Authentication required.') // custom message
});

app.use('/', authorize);
app.use('/', express.static('build'));

app.listen(3000);

Hope this helps!

leanndemetro avatar Apr 14 '22 20:04 leanndemetro

I tried @leanndemetro's solution, which does indeed create a login dialogue, but using the default username & password ("USERNAME" & "PASSWORD"), I couldn't log in. Am I missing something?

EDIT: apparently I needed to edit/create .env in my root with something like this:

USERNAME=some-username PASSWORD=mynewstrongpassword

In my case, I needed to update it because I was already using it for other stuff.

Unfortunately, this breaks the build step, which chokes on the "type": "module" requirement.

techbridgedev avatar Apr 19 '23 16:04 techbridgedev

You may check my articles that show how to add an authentication layer to your Docusuaurs website.

https://iammassoud.net/blog/docusaurus-authentication-roadmap

massoudmaboudi avatar May 23 '23 02:05 massoudmaboudi

You may check my articles that show how to add an authentication layer to your Docusuaurs website.

https://iammassoud.net/blog/docusaurus-authentication-roadmap

Thanks, Massoud. I followed the tutorial for authenticating with AWS Cognito & got it to work on my local setup, but the build step chokes with:

[ERROR] Unable to build website for locale en-us.

[ERROR] Error: The `docusaurus_tag` meta tag could not be found. Please make sure that your page is wrapped in the `<Layout>` component (from `@theme/Layout`). If it is, then this is a bug, please report it.

Any idea what may be going on? If I rename the theme directory temporarily I can complete a build, but of course, that stops the authentication components from being compiled. It does point to something in one of those swizzled components as being the culprit, though.

techbridgedev avatar May 25 '23 17:05 techbridgedev

You may check my articles that show how to add an authentication layer to your Docusuaurs website. https://iammassoud.net/blog/docusaurus-authentication-roadmap

Thanks, Massoud. I followed the tutorial for authenticating with AWS Cognito & got it to work on my local setup, but the build step chokes with:

[ERROR] Unable to build website for locale en-us.

[ERROR] Error: The `docusaurus_tag` meta tag could not be found. Please make sure that your page is wrapped in the `<Layout>` component (from `@theme/Layout`). If it is, then this is a bug, please report it.

Any idea what may be going on? If I rename the theme directory temporarily I can complete a build, but of course, that stops the authentication components from being compiled. It does point to something in one of those swizzled components as being the culprit, though.

Maybe you can share your repo if it is public. or at least you can share your file hierarchy and the content of Root.js

massoudmaboudi avatar May 26 '23 02:05 massoudmaboudi

It a private repo, but here's the hierarchy:

.
└── App/
    ├── .docusaurus/
    ├── .vscode/
    ├── build/
    ├── dist/
    ├── docs/
    ├── node_modules/
    ├── src/
    │   ├── components/
    │   │   ├── Auth/
    │   │   │   └── index.js
    │   │   ├── Login/
    │   │   │   └── index.js
    │   │   └── * OTHER UNRELATED COMPONENTS
    │   ├── config/
    │   │   ├── amplify-config.js
    │   │   └── aws-exports.js
    │   ├── css/
    │   ├── pages/
    │   ├── partials/
    │   ├── theme/
    │   │   ├── Navbar/
    │   │   │   ├── Content/
    │   │   │   │   ├── index.js
    │   │   │   │   └── styles.module.css
    │   │   │   └── MobileSidebar/
    │   │   │       └── index.js
    │   │   └── Root.js
    │   └── utils/
    │       ├── constants.js
    │       └── utils.js
    ├── static/
    ├── .env.local
    ├── .gitignore
    ├── .markdownlint.json
    ├── babel.config.js
    ├── docusaurus.config.js
    ├── jsonconfig.json
    ├── package.json
    ├── README.md
    └── sidebars.js
And here's Root.js:
   // src/theme/Root.js
   
   import React from 'react';
   
   import { Amplify } from 'aws-amplify';
   import { Authenticator } from '@aws-amplify/ui-react';
   
   import { configure } from '../config/amplify-config';
   import { AuthCheck } from '../components/Auth';
   
   const awsConfig = configure();
   Amplify.configure(awsConfig);
   
   export default function Root({ children }) {
     return (
       <Authenticator.Provider>
         <AuthCheck children={children} />
       </Authenticator.Provider>
     );
   }

techbridgedev avatar May 26 '23 16:05 techbridgedev

@techbridgedev The issue doesn't seem to be associated with the swizzled components since it is working as expected. Though, you have a few options:

  • Just clone my example repo, fill up the env file, and try to build it. I tested it, it worked as expected. Then step by step add your new changes.
  • If you want, you can add me to your repo so I can check as well.
  • You may check the docusaurus.config.js and the code in pages folder too.

Overall, this issue can be from your search plugin as well.

massoudmaboudi avatar May 26 '23 16:05 massoudmaboudi

@massoudmaboudi I cloned the feat-specific-route-protection branch of the repo, added my .env.local file, and ran the build script, but it failed with a bunch of broken links all pointing to /login. I am able to serve the site via npm run start and can confirm that authentication works.

I haven't pushed my own changes to my experimental branch where I am testing authentication, but I could if you still want to be added to the repo to check it out.

I still need to look into the search plugin to see if that's responsible.

EDIT: I temporarily removed the search plugin from docusaurus.config.js from my own implementation & the error did go away, only to be replaced by the error I describe above (broken links pointing to /login). So I suspect my original error has to do with swizzling the navbar while also using the local search plugin. Still a mystery why I'm getting the other error, though.

techbridgedev avatar Jun 01 '23 16:06 techbridgedev

@techbridgedev You are correct since I missed pushing a line to /src/components/Auth/index.js line 28. Please check the commit below for the code change: link to the commit

massoudmaboudi avatar Jun 02 '23 01:06 massoudmaboudi

@techbridgedev You are correct since I missed pushing a line to /src/components/Auth/index.js line 28. Please check the commit below for the code change:

I've downloaded both branches after the above fix but still have the same build error. Do you want me to file an issue?

techbridgedev avatar Jun 13 '23 21:06 techbridgedev

@techbridgedev You can resolve the issue by setting onBrokenLinks: 'warn' instead of onBrokenLinks: 'throw' in docusaurus.config.js. but I suggest the below code-change.

There should be an explanation why Docusaurus is behaving like this, though, to improve the code and not to change the default setting of Docusaurus, I created a new commit and added the /login route under src/pages.

link to the commit

By this change, you don't need to change docusaurus.config.js.

massoudmaboudi avatar Jun 14 '23 02:06 massoudmaboudi

You may check my articles that show how to add an authentication layer to your Docusuaurs website.

https://iammassoud.net/blog/docusaurus-authentication-roadmap

I just followed your tutorial, the auth works great!

Thank you so much for such a detailed walkthrough, truly appreciate it!

norahsakal avatar Jul 13 '23 08:07 norahsakal

Hi there, not sure if this is what is being referred to, but you can easily create an Express Server with Basic Auth for your Docusaurus site and use the Express.static method to serve the build folder.

I would like to ask if everything is good when deployed to Vercel and used that Auth?

OmerFarukOruc avatar Feb 15 '24 11:02 OmerFarukOruc

I created a function protection system and it works great when going to: /someprotectedpage However if someone goes to /notprotected and clicks on a button that leads to /someprotectepage it doesn't work anymore because of the one page aspect that will just ask for hashed .js files. I'm currently trying to track which hash files belongs to which page but that seems to be a whole rabbit hole on it's own. How did you all solve this problem?

I do have some other ideas like:

  • Make people reload the page when going to protected pages (but that isn't backend secure)
  • building a limited unprotected build and basically host 2 different version and give hashed bashed on login (but seems a bit of a hassle if you have different types of protected areas)

Edit: Fixed it, I just read the .js files and create a mapping file based on the attributes in those files.

IRiViI avatar Jun 27 '25 08:06 IRiViI

@IRiViI my recommendation would be to build 2 static sites that differ based on env variables/config:

  • one for authenticated users
  • one for anonymous users

Then you can use your CDN edge features to route the user to one deployment or the other, depending on cookies and other things like that.

slorber avatar Jun 27 '25 14:06 slorber