<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Antonio Gioia</title>
    <description>The latest articles on DEV Community by Antonio Gioia (@antoniogioiacom).</description>
    <link>https://dev.to/antoniogioiacom</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F379201%2F4ec1e3a4-c8f0-418e-9705-6680cedbbcbd.jpg</url>
      <title>DEV Community: Antonio Gioia</title>
      <link>https://dev.to/antoniogioiacom</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/antoniogioiacom"/>
    <language>en</language>
    <item>
      <title>SAML Single Sign On setup with Express and Passport</title>
      <dc:creator>Antonio Gioia</dc:creator>
      <pubDate>Mon, 04 May 2020 09:31:15 +0000</pubDate>
      <link>https://dev.to/antoniogioiacom/saml-single-sign-on-setup-with-express-and-passport-4m6g</link>
      <guid>https://dev.to/antoniogioiacom/saml-single-sign-on-setup-with-express-and-passport-4m6g</guid>
      <description>&lt;p&gt;In this short guide about SAML authentication on a &lt;a href="https://expressjs.com/"&gt;Express&lt;/a&gt; based web app I'm going to show how to implement a basic setup using &lt;a href="http://www.passportjs.org/"&gt;Passport&lt;/a&gt;, the authentication middleware for &lt;a href="https://nodejs.org/"&gt;Node.js&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is SAML protocol
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://en.wikipedia.org/wiki/Security_Assertion_Markup_Language"&gt;Wikipedia page about SAML&lt;/a&gt; says: "&lt;em&gt;Security Assertion Markup Language is an open standard for exchanging authentication and authorization data between parties, in particular, between an identity provider and a service provider&lt;/em&gt;". In other words a user can use a single login (username and password) for two different applications, for example to login into his university &lt;strong&gt;and&lt;/strong&gt; in your app with the same credentials.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aMMiSprs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://antoniogioia.com/images/blog/saml-browser-sso.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aMMiSprs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://antoniogioia.com/images/blog/saml-browser-sso.png" alt="SAML exchange"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On your app (Service Provider) you have to create a route that Passport will automatically redirect to the Identity Provider login page (in our example the university login page), a user would then login with his student credentials provided by university (Single Sign On) and then get redirected to your app authenticated and authorized.&lt;/p&gt;

&lt;p&gt;In order to make this work both parties (Service Provider and Identity Provider) must coordinate to configure the parts that are going to exchange the XML based security assertions, a successful exchange returns to your backend a JSON object with a few informations, one of which the e-mail address of the student. You have to use that piece of information to check if you already know the user and finally give access to content or, if it's a new user, to complete registration. You are not going to save the password of a user logged in via SAML because the authentication is managed by the Identity Provider.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example of SSO with Express and Passport
&lt;/h2&gt;

&lt;p&gt;Our setup include Express as web server and Passport as authentication middleware. For a basic Service Provider configuration you need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Your cert keys&lt;/li&gt;
&lt;li&gt;  Identity Provider metadata page&lt;/li&gt;
&lt;li&gt;  Identity Provider entry point URL&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And you, as Service Provider, have to provide to the Identity Provider:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Your metadata page&lt;/li&gt;
&lt;li&gt;  Your callback URL&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Create cert keys
&lt;/h3&gt;

&lt;p&gt;Open a terminal, create a &lt;code&gt;certs&lt;/code&gt; folder and create your keys with OpenSSL:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir certs
openssl req -x509 -newkey rsa:4096 -keyout certs\key.pem -out certs\cert.pem -nodes -days 900
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Identity Provider has to give to you the address of their &lt;strong&gt;metadata&lt;/strong&gt; page, an endpoint for an XML document with various details needed by SAML to verify assertions. You need to know the &lt;strong&gt;entry point URL&lt;/strong&gt; as well, it's the actual page on the Identity Provider website where the user will type his credentials to login. On the Identity Provider metadata page you can find the public key that you need have on your server. Find the tag &lt;code&gt;X509Certificate&lt;/code&gt;, copy the content in a file named &lt;code&gt;idp_key.pem&lt;/code&gt; and save it in &lt;code&gt;certs&lt;/code&gt; folder.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create your Service Provider metadata page
&lt;/h3&gt;

&lt;p&gt;As a Service Provider you need to create a metadata page too, Identity Provider will need it to configure their end point. We will use &lt;code&gt;passport-saml&lt;/code&gt; as SAML authentication provider for Passport, you can install it with &lt;code&gt;npm&lt;/code&gt; and require it together with &lt;code&gt;passport&lt;/code&gt; in your Express app.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const passport = require("passport");
const saml = require("passport-saml");

app.route("/metadata").get(function(req, res) {
    res.type("application/xml");
    res.status(200);
    res.send(
    samlStrategy.generateServiceProviderMetadata(
        fs.readFileSync("./certs/cert.pem", "utf8"),
        fs.readFileSync("./certs/cert.pem", "utf8")
    )
    );
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Now if you open the route &lt;code&gt;/metadata&lt;/code&gt; on your app you should see a XML document with your public key exposed.&lt;/p&gt;

&lt;p&gt;We can now configure two more routes, the first will automatically redirect a user to the Identity Provider entry point, the second, where we receive the response, will be called by the Identity Provider application soon after the user is authenticated on their server.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.route("/login-idp").get(passport.authenticate("samlStrategy"));
app.route("/login-idp/callback").post(samlCallback(passport));
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We have now to define the SAML strategy for both routes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Configure SAML strategy and session
&lt;/h3&gt;

&lt;p&gt;We can define a SAML strategy with a new instance of &lt;code&gt;Strategy&lt;/code&gt; from &lt;code&gt;passport-saml&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const samlStrategy = new saml.Strategy(
    {
        callbackUrl: "/login-idp/callback",
        entryPoint: "https://idp.webapplication.com/idp/profile/SAML2/Redirect/SSO",
        issuer: "mywebapp-saml",
        decryptionPvk: fs.readFileSync("./certs/key.pem", "utf8"),
        privateCert: fs.readFileSync("./certs/key.pem", "utf8")
        // more settings might be needed by the Identity Provider
    },
        function(req, profile, done) {
        return done(null, profile);
    }
);

passport.use("samlStrategy", samlStrategy);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;The two arguments are a configuration object and a function that returns a profile object, more details about the configuration on the &lt;a href="https://github.com/bergie/passport-saml"&gt;passport-saml github page&lt;/a&gt;. The &lt;code&gt;entryPoint&lt;/code&gt; key is a URL provided by the Identity Provider, the &lt;code&gt;issuer&lt;/code&gt; key is a string that identifies your request on the Identity Provider side.&lt;/p&gt;

&lt;p&gt;Now we need to define the callback function for the POST request arriving from the Identity Provider application at &lt;code&gt;"/login-idp/callback"&lt;/code&gt;, assuming in the response there is a key &lt;code&gt;email&lt;/code&gt; that identifies the user:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const samlCallback = function(passport) {
    return function(req, res, next) {
        passport.authenticate("samlStrategy", function(err, user, info) {

            const email = user.email;

            // check in database if user with that email exists
            // ...

            // if exists show content for registered users:
            req.login(user, function(err) {
                return res.redirect("/my-contents-page");
            });

            // application logic code
            // ...

            // if doesn't exist redirect to complete registration page
            req.login(user, function(err) {
                return res.redirect(
                    "/complete-registration"
                );
            });

        })(req, res, next);

    };
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;req.login&lt;/code&gt; (together with &lt;a href="http://www.passportjs.org/docs/login/"&gt;req.logout&lt;/a&gt;) is the Passport function that takes care to establish a login session in our Express app. A popular middleware to handle sessions on Express is &lt;code&gt;express-session&lt;/code&gt;, we can use it in our app together with &lt;code&gt;cookie-parser&lt;/code&gt; to keep track of user authentication with cookies:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const session = require('express-session');
const cookieParser = require('cookie-parser');

app.use(cookieParser());

app.use(session({
    secret: "secret",
    resave: false,
    saveUninitialized: true,
    cookie: { maxAge: 7 * 24 * 60 * 60 * 1000 }, // 7 days
    store: [...] // save on database
}));
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Check &lt;a href="https://github.com/expressjs/session"&gt;express-session documentation&lt;/a&gt; for more info.&lt;/p&gt;

&lt;h2&gt;
  
  
  Links
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;a href="https://en.wikipedia.org/wiki/Security_Assertion_Markup_Language"&gt;Security Assertion Markup Language on Wikipedia&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://medium.com/disney-streaming/setup-a-single-sign-on-saml-test-environment-with-docker-and-nodejs-c53fc1a984c9"&gt;Setup a Single Sign On SAML Test Environment with Docker and NodeJS&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="http://www.passportjs.org/packages/passport-saml/"&gt;Passport-SAML package&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>saml</category>
      <category>express</category>
      <category>passport</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
