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 Criipto Verify 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 Criipto Verify to enable OpenID Connect on top of the national identities
- Modify you Node application to defer authentication to Criipto Verify.
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 criipto.com and sign up. Once signed up you can join our Slack channel from the Criipto Verify dashboard.
1. Setting up your Criipto Verify account
As the various national and bank identity services do not support OpenID Connect or any other standard protocol, we use Criipto Verify as the identity service in the middle.
So, if you haven’t done so already, sign up for Criipto Verify: criipto.com
Name your first domain which will be a subdomain of criipto.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 Criipto Verify, you have one more step to get it set up for OpenID Connect. Open the Criipto Verify 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 Criipto Verify
For this example we use a simple, automatically scaffolded, Express application. The steps will be the same if you want to add Criipto Verify 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 Criipto Verify 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 the Criipto Verify 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 Criipto Verify. 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 Criipto Verify 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
Criipto Verify, 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 Criipto Verify tenant, we’ve set up test tenant in Criipto Verify 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 criipto.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!
RECENT POSTSJune 15, 2017
OpenID Connect from ASP.NET Core with Visual Studio 2017March 24, 2017
Sign text with BankID, NemID, right in your front-endFebruary 25, 2017
OpenID Connect from ASP.NET Core - on Mac OSXFebruary 14, 2017
Use BankID and NemID from a Node.js applicationJanuary 10, 2017
Security considerations when building an identity serviceDecember 15, 2016
Real world identities online: Why and how?December 07, 2016
Criipto Verify connects Auth0 to Scandinavian national and bank identitiesDecember 04, 2015
Grean extends Auth0 with B2B onboarding and authorizationAugust 31, 2015
Are security concerns holding back your B2B integration?