<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/shiny-server-series-part-4-adding-user-authentication-using-auth0/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.jasperginn.nl/shiny-server-series-pt4/"> Jasper Ginn's blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><em>This guide is part of a series on setting up your own private server running shiny apps. There are many guides with great advice on how to set up an R shiny server and related software. I try to make a comprehensive guide based in part on these resources as well as my own experiences. I always aim to properly attribute information to their respective sources. If you notice an issue, please contact me.</em></p>
<p>Part 1 of this series is available <a target="_blank" rel="nofollow" href="https://www.jasperginn.nl/shiny-server-series-pt1/">here</a></p>
<p>Part 2 of this series is available <a target="_blank" rel="nofollow" href="https://www.jasperginn.nl/shiny-server-series-pt2/">here</a></p>
<p>Part 3 of this series is available <a target="_blank" rel="nofollow" href="https://www.jasperginn.nl/shiny-server-series-pt3/">here</a></p>
<p>We’re nearly at the end of the shiny server series, and that means that we’ll be getting into the really useful parts. In this tutorial, we’ll use <a target="_blank" rel="nofollow" href="http://auth0.com/">Auth0</a> to set up user authentication to regulate access to our private shiny server.</p>
<h2>Resources used for this part</h2>
<p>This guide draws on <a target="_blank" rel="nofollow" href="https://auth0.com/blog/adding-authentication-to-shiny-server/">this</a> excellent tutorial produced by Auth0.</p>
<h2>Where were we?</h2>
<p>In the <a target="_blank" rel="nofollow" href="https://www.jasperginn.nl/shiny-server-series-pt3/">previous part</a>, we set up SSL encryption on our server. This makes user authentication safer by encrypting traffic to and from the server. Remember, however, that we didn’t yet set up nginx to use this SSL certificate. So although we set up all the prerequisites, SSL encryption is not yet operational.</p>
<p>As always, the first thing we’ll do is log into the server</p>
<figure>
<pre><code data-lang="shell"><span># Log into the server</span>
ssh root@&lt;your-VPS-ip></code></pre>
</figure>
<p>Before you do anything else, take a note of the location where Let’s encrypt stored your certificate. It’s easiest to just navigate to the following folder</p>
<figure>
<pre><code data-lang="shell"><span>cd</span> /etc/letsencrypt/live</code></pre>
</figure>
<p>And execute</p>
<figure>
<pre><code data-lang="shell">ls</code></pre>
</figure>
<p>Write down the name of this folder, you’ll need it to complete the next step.</p>
<p>Now, we can switch to the shiny user:</p>
<figure>
<pre><code data-lang="shell">su shiny
<span># Navigate to shiny home</span>
<span>cd</span></code></pre>
</figure>
<p>Back up the nginx configuration in case something goes wrong. Then, delete the config and open a new default config file:</p>
<figure>
<pre><code data-lang="shell">sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/default2
<span># Delete default</span>
sudo rm /etc/nginx/sites-available/default
<span># Make a new default config</span>
sudo nano /etc/nginx/sites-available/default</code></pre>
</figure>
<p>The next part can be a bit tricky. Take a look at the config file below. You need to replace <code>&lt;your-domain-name></code> with the following:</p>
<ol>
<li>In lines <strong>19-20</strong>, replace the text by the name of the folder you just wrote down.</li>
<li>In lines <strong>39</strong> and <strong>91</strong>, replace the text by your custom domain name, <strong>with and without</strong> ‘www’</li>
</ol>
<p>I’ve added some helpful screenshots below for clarification. It’s easiest to just copy-paste this into a notepad file and replace the values before pasting it into the nano editor.</p>
<p><div class="feedlyNoScript">
<pre>400: Invalid request
</pre>
<p><br>
</p>
<p>The image below shows what this looks like in the nano editor.</p>
<p>(here’s the top part of the config file)<br>
<img alt="" src="https://i1.wp.com/www.jasperginn.nl/assets/images/blog/shiny_series_pt4/replacmeents.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>(and here’s the bottom part)<br>
<img alt="" src="https://i2.wp.com/www.jasperginn.nl/assets/images/blog/shiny_series_pt4/replacements2.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>After pasting the new configuration, save the file by hitting <code>control+x</code> and then <code>Y</code> and <code>enter</code>. Then, restart the nginx service:</p>
<figure>
<pre><code data-lang="shell">sudo service nginx restart</code></pre>
</figure>
<p>In your browser, navigate to your domain name. If all went well, you should now see the lock symbol indicating that the setup is a success!</p>
<p><img alt="" src="https://i1.wp.com/www.jasperginn.nl/assets/images/blog/shiny_series_pt4/authenticated.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h2>What does this new config file do?</h2>
<p>Glad you asked. This config file makes sure that we use the Let’s Encrypt SSL certificate we created earlier. We also keep the locations we’ve been using so far, but we changed the config files for the private-apps shiny server. This configuration file re-routs all traffic through the Auth0 authentication service we’ll set up in a second. If the user successfully authenticates, they will be forwarded to the private shiny environment.</p>
<h2>Setting up your Auth0 account</h2>
<p>Navigate to the <a target="_blank" rel="nofollow" href="https://manage.auth0.com/login">Auth0 website</a> and sign up for an account. A free account works with up to 7.000 users, so this should be more than enough.</p>
<p>Navigate to connections and remove all social logins. We won’t be needing them for this application as we’ll be setting up an email whitelist:</p>
<p><img alt="" src="https://i0.wp.com/www.jasperginn.nl/assets/images/blog/shiny_series_pt4/auth0removesociallogins.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Go to ‘clients’ and click on ‘create client’</p>
<p><img alt="" src="https://i2.wp.com/www.jasperginn.nl/assets/images/blog/shiny_series_pt4/auth0_step1.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Name your application and choose ‘regular web app’:</p>
<p><img alt="" src="https://i2.wp.com/www.jasperginn.nl/assets/images/blog/shiny_series_pt4/auth0_step2.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Now go to ‘settings’, note down your domain, client ID and secret ID, and add a callback url in the following format:</p>
<figure>
<pre><code data-lang="text">https://www.&lt;your-domain-name>.&lt;extension>/private-apps/callback,
https://&lt;your-domain-name>.&lt;extension>/private-apps/callback</code></pre>
</figure>
<p><img alt="" src="https://i2.wp.com/www.jasperginn.nl/assets/images/blog/shiny_series_pt4/auth0_step3.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Scroll down and save your changes. Then, navigate to ‘rules’ and click on ‘create first rule’:</p>
<p><img alt="" src="https://i0.wp.com/www.jasperginn.nl/assets/images/blog/shiny_series_pt4/auth0_step4.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>From this list, select ‘whitelist for a specific app’:</p>
<p><img alt="" src="https://i0.wp.com/www.jasperginn.nl/assets/images/blog/shiny_series_pt4/auth0_step5.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Change the name of the app (red box) to your application name, then add the email addresses that are allowed to make an account (gold box):</p>
<p><img alt="" src="https://i2.wp.com/www.jasperginn.nl/assets/images/blog/shiny_series_pt4/auth0_step6.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h2>Setting up the Auth0 service</h2>
<p>Go back to the terminal and clone my fork of the auth0 shiny-auth0 repository:</p>
<figure>
<pre><code data-lang="shell"><span>cd</span> ~ <span>&amp;&amp;</span> git clone https://github.com/JasperHG90/shiny-auth0.git</code></pre>
</figure>
<p>Install nodejs and npm</p>
<figure>
<pre><code data-lang="shell">sudo apt-get install -y nodejs npm</code></pre>
</figure>
<p>Enter the shiny-auth0 repository and let npm install the dependencies</p>
<figure>
<pre><code data-lang="shell"><span>cd </span>shiny-auth0 <span>&amp;&amp;</span> npm install</code></pre>
</figure>
<p>Go to <a target="_blank" rel="nofollow" href="http://www.unit-conversion.info/texttools/random-string-generator/">this website</a> and generate a random string of 40 characters. Copy-paste this string in a temporary document. You will need it a little later.</p>
<p>Go back to the terminal and create a file called ‘.env’ in the shiny-auth0 directory:</p>
<figure>
<pre><code data-lang="shell"> nano .env</code></pre>
</figure>
<p>Copy the lines below into a temporary document and add the required information</p>
<figure>
<pre><code data-lang="text">AUTH0_CLIENT_SECRET=yoursecretid
AUTH0_CLIENT_ID=yourclientid
AUTH0_DOMAIN=yourdomain
AUTH0_CALLBACK_URL=https://&lt;your-domain-name>.&lt;extension>/private-apps/callback
COOKIE_SECRET=random string generated in the previous step
SHINY_HOST=localhost
SHINY_PORT=4949
PORT=3000</code></pre>
</figure>
<p>Save the file by hitting <code>control+x</code> and then <code>Y</code> and <code>enter</code>. Make a symlink for nodejs so it can also be called with ‘node’:</p>
<figure>
<pre><code data-lang="shell">sudo ln -s <span>`</span>which nodejs<span>`</span> /usr/bin/node</code></pre>
</figure>
<p>Run the app using:</p>
<figure>
<pre><code data-lang="shell">npm start</code></pre>
</figure>
<p>Now, go to: <code>www.&lt;your-domain-name>.&lt;extension>/private-apps/</code> to see if it works.</p>
<p>Now that we know that user authentication works, we need to make sure that the Auth0 service can quietly run in the background like a regular service. Hit <code>control + C</code> in the terminal to stop the nodejs server. Then, execute</p>
<figure>
<pre><code data-lang="shell">sudo nano /etc/systemd/system/shiny-auth0.service</code></pre>
</figure>
<p>and copy-paste the following:</p>
<figure>
<pre><code data-lang="text">[Service]
ExecStart=/usr/bin/node /home/shiny/shiny-auth0/bin/www
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=shiny
User=shiny
Group=shiny
WorkingDirectory=/home/shiny/shiny-auth0/
Environment=NODE_ENV=production

[Install]
WantedBy=multi-user.target</code></pre>
</figure>
<p>Hit <code>Control + X</code>, <code>Y</code> and then <code>enter</code>. Then, execute the next two lines:</p>
<figure>
<pre><code data-lang="shell">sudo systemctl <span>enable </span>shiny-auth0
sudo systemctl start shiny-auth0</code></pre>
</figure>
<p>This automatically starts up the auth0 server every time the server restarts.</p>
<h2>Wrapping up</h2>
<p>Congratulations, user authentication is now set up! This wraps up part 4 of the shiny server series. In the last instalment, we’ll be adding a simple static website created using Jekyll.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.jasperginn.nl/shiny-server-series-pt4/"> Jasper Ginn's blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/anW3f9zBcvM"></a></div></div>