Use BankID and NemID from a Node.js application
Learn how to authenticate your users with Norwegian or Swedish BankID or Danish NemID with Node.js and Express. By leveraging OpenID Connect, connecting Node applications to national identity services via easyID has become a trivial job, as shown in this post.
To immediately get a running version of the code from this post get the sample from GitHub
This example creates a site from scratch, but the instructions may as well be applied to an existing Node application. Two steps are involved in getting going from zero:
- Set up an account in easyID to enable OpenID Connect on top of the national identities
- Modify you Node application to defer authentication to easyID.
A note on test users
In order to test the use of Norwegian BankID, Swedish BankID, and Danish NemID you need test identities.
For the sample shown here, we’ve set it up to use Norwegian BankID with a hardware token. You may try it out with a test user with Fødselsnummer, Engangskode, and Personlig passord set to 15076400000, otp, qwer1234
Please let us know if you need your own test identities, and we will help you set them up. Just go to grean.com/easyid and sign up. Once signed up you can join our Slack channel from the easyID dashboard.
1. Setting up your easyID account
As the various national and bank identity services do not support OpenID Connect or any other standard protocol, we use Grean easyID as the identity service in the middle.
So, if you haven’t done so already, sign up for easyID: grean.com/easyid
Name your first domain which will be a subdomain of grean.id. This will be the domain name we will use in the following.
Once signed up, go to the Applications tab to register your application.
Note that if you use Auth0 as your identity broker your life just got a little bit easier and you should take a look at our screencast showing how that scenario works.
But this post is not about that, so for this exercise just go for the manual option.
Remember to copy the Client ID value - you will need it when configuring the Express middleware.
For the Callback URLs just enter the URL where your application may be reached for notification of the
authenticated user. In our example that would be
Once the application has been registered in easyID, you have one more step to get it set up for OpenID Connect. Open the easyID application registration again and switch on the OAuth2 Code flow. This will generate a client secret that you will need in your application. Be sure to copy the value or you will have to generate a new one. (As with any other password we will, of course, only store the hashed value)
With this you will be able to set up your application to authenticate using any of the enabled identity services: Danish NemID, Swedish BankID and/or Norwegian BankID.
2. Integrate your application with easyID
For this example we use a simple, automatically scaffolded, Express application. The steps will be the same if you want to add easyID authentication to your existing application.
So starting from scratch, simply generate an Express app skeleton:
This generates a simple site structure for an Express application using the EJS view engine. Navigate your browser to http://localhost:3000 to view the site.
Next add the necessary middleware to enable authentication using an OpenID Connect module. Note that we pull the OpenID Connect passport strategy directly from GitHub. As of this writing the NPM version has not been updated for a while and did not have the needed features. We will soon release a specialized easyID strategy, but till then:
Add the authentication middleware
Once the application has been created and you have installed the necessary Node modules, you need to update your application to perform OpenID Connect authentication with Grean’s easyID identity provider.
In your application file, here
app.js, include the new modules:
Note that the
jsonwebtoken and the
jwksRsa modules are needed only if
you plan to verify the token issued by easyID. In the scenario outlined here,
where you simply authenticate the user and extract the user information,
this is not a hard requirement as the token is being retrieved over a
confidential backchannel using the client ID and client secret.
Next is the instantiation of the passport strategy. As you can see, several settings revolve around the domain name of your easyID domain.
acr_values parameter for the strategy setup. This identitfies
the specific kind of authentication you choose. Your options include:
- Norwegian BankID:
- Hardware token (kodebrikke):
- Swedish BankID:
- Same device:
- Another device (aka mobile):
- Same device:
- Danish NemID:
- Personal with code card:
- Employee with code card:
- Employee with code file:
- Personal with code card:
With this we’ve set up most of the infrastructure and now just need to make our endpoints do their thing.
A note on signature validation
If you want to verify the signature of the
id_token you get back from
easyID, add this function, and call it as shown in the comment in the
above code block.
The views: Start the login and show the user info
First add a login link to the front page. Put this, or something to the same effect, in the page:
That will get the login process started. Next we need to display a protected page upon successful autthentication.
Add a new file,
users.ejs to the
Routing to login and back
With the views set up, we route requests to the right endpoints.
First the login request is routed.
We route authenticated requests to the users view. Authentication is
checked by the simple helper module,
routes/users.js to something like this:
Running the application
As you would have noticed in the above code for
app.js we rely on a set of
environment variables to run the application:
If you want to run the sample without your own easyID tenant, we’ve set up test tenant in easyID for you to use. Just use this to run you local version.
That’s it! View, download, and run this sample from GitHub
Go to grean.com to sign up today to quickly enable your applications to accept real people’s real identities. We run a tight ship to ensure that you get the same level of security and safety that you get from the various proprietary bank and government solutions!