As a cloud developer, securing a single-page application (SPA) can be an intricate process with many different security protocols to consider.
With just a few lines of code, you can easily add a level of security to your SPA by requiring that users sign in to gain access to protected resources. The App ID SDK gives you the ability to do the following:
- Enforce social or enterprise sign-in (such as Facebook, Google, and SAML 2.0 Federation) as identity provider options to your apps by using APIs, SDKs, prebuilt UIs, or your own branded UIs through verification of authentication tokens.
- Personalize your app experiences by storing user data, such as preferences and information that is returned by public social profiles.
- Manage a user registry that you can easily scale as your user base grows.
- Enforce policy-driven security for your apps by integrating with IBM Cloud Kubernetes Service at the cluster level.
Securing Angular SPAs
Working with another frontend framework? No problem, follow along for the concepts but use your code. However, if your app has a backend that you manage or needs to be refreshed to load content, try working with the web app flow instead.
Understanding the flow
Before we get started, it is important to get the context of what we are about to do. The figure below shows how to securely authenticate and authorize single-page application users with the Authorization Code with Proof Key for Code Exchange (PKCE):
- A user attempts to log in to your single-page application.
- The App ID SDK creates a code verifier for the authorization request, which is the plain text version of the code challenge.
- Along with the authorization request, the client sends the code challenge and the challenge method that is used to encode the challenge.
- The authentication flow is started by App ID in a new window.
- The user chooses an identity provider to authenticate with and completes the sign-in process.
- The App ID SDK on the application receives the grant code.
- The SDK then makes an XHR request to the App ID token endpoint along with the grant code and the code verifier.
- App ID returns access and identity tokens.
Before you get started with your Angular SPA, be sure that you have the following prerequisites:
- Node.js version 8.10+
- NPM package manager version 5.6+
- IBM Cloud App ID instance with SPA credentials
- A Redirect URI set in the App ID service dashboard
Note: You can skip the steps below by checking out the repository of our Angular sample SPA which you can clone and have running quickly to see how App ID can easily secure your application.
The steps below will take about 12 minutes to complete.
Prepare your workspace
- In your terminal, install the Angular CLI:
- Create a workspace and initial application:
There will be follow-up questions after running the command above, go ahead and select your preferences as shown below:
- Would you like to add Angular routing? y
- Which stylesheet format would you like to use? CSS
- Navigate to the workspace folder:
- Install the IBM Cloud AppID SDK dependency using npm:
Create your application
- In your terminal, navigate to the app folder of your application and create two Angular components—welcome and home:
- First, we will update the welcome component. In your code editor, go to the welcome folder, open
welcome.component.html, and replace its contents with the code below:
- Open the
welcome.component.tsfile and replace its contents with the code below. This file will contain the code necessary to add authentication and authorization to the application using the App ID SDK. Be sure to add your own SPA's client ID and discovery endpoint:
- Now, we will update the home component. In the home folder, open the
home.component.htmlfile and replace its contents with the code below:
- Go to the
home.component.tsfile in the home component, clear all the code present, and add the code below:
- Navigate to the app folder and replace the contents of
app.component.htmlwith the following code:
- Go to the
app.component.ts file, clear all the code present, and add the code below:
- Go to the src folder and add the following global styles to the
- Save all the files and in your terminal, run the following command to view your app in the browser at http://localhost:4200:
Make sure you register your redirect_uri (in this case http://localhost:4200/*) with App ID to ensure that only authorized clients are allowed to participate in the authorization workflow. This can be done on the App ID dashboard under the Manage Authentication tab in the Authentication Settings. See "Adding redirect URIs" for more details.
Well done! You successfully integrated IBM Cloud App ID's SDK for SPA into an Angular application.
Tips and things to note
- You can use the App ID client SDK to automatically obtain a new pair of tokens without requiring that the user explicitly sign in. For more information, see "Configuring silent login."
- To learn more about security vulnerabilities with the implicit grant type and authorization code flow, visit OAuth 2.0 Security Best Current Practice.
- For more information about how PKCE and the authorization code flow work together, see the spec.
Questions and feedback
- If you have technical questions about App ID, post your question on Stack Overflow and tag your question with ibm-appid.
- For questions about the service and getting started instructions, use the IBM Developer Answers forum. Include the appid tag.
- Check out the App ID tutorials on our YouTube channel.
- Open a support ticket in the IBM Cloud menu.
- Reach out directly to the development team on Slack!
To get started with App ID, check it out in the IBM Cloud Catalog.