API Security: Ways to Authenticate and Authorize
The world of APIs is growing very fast. You use many of them and (probably) you provide some of them. How do we keep them from becoming as lawless as a spaghetti western?
Join the DZone community and get the full member experience.Join For Free
this post will be part one of three for my ultimate guide to api security best practices series. in this post, i will be discussing the current concerns it decision makers have in regards to their current digital assets.
part two will address the need for keeping data private and protecting it from being compromised while making it accessible at all times.
part three will present a fictitious scenario that shows you how anypoint platform can form part of the fabric of a secure api- led architecture.
current state of apis
apis have become a strategic necessity for your business. they facilitate agility and innovation. however, the financial incentive associated with this agility is often tempered with the fear of undue exposure of the valuable information that these apis expose. with data breaches now costing $400m or more, senior it decision makers are right to be concerned about api security .
a secure api is one that can guarantee the confidentiality of the information it processes by making it visible only to the users, apps, and servers that are authorized to consume it. likewise, it must be able to guarantee the integrity of the information it receives from the clients and servers with which it collaborates so that it will only process such information if it knows that it has not been modified by a third party. to guarantee these two security qualities, the ability to identify the calling systems and their end-users is a prerequisite. what we have stated also applies to those calls that the api makes to third party servers. an api must never lose information, so it must be available to handle requests and process them in a reliable fashion.
identity is core to the world of security. you must be able to recognize the apps that consume your api, the users of the same and the servers that your api calls out to. likewise, your api should be able to identify itself to both apps and servers. you need an identity store to which you can refer to verify your recognition of apps and users. the identity store could be a database, but an ldap server is the most popular solution. active directory is a popular ldap implementation. in an ldap server, you typically store usernames, passwords, digital certificates, some personal details and the organization groups to which users belong. app ids can also be stored here. an identity provider is software which is dedicated to managing the interaction with the identity store(s) for authentication and authorization purposes. your api can function in this role though it is much more preferable to delegate this responsibility to the identity provider.
1.1 user and app authentication
when you are presented with an app id or a user’s username (claim) in a call to your api, you must be able to verify the authenticity of the claim. this is done with some form of a shared secret. when your api acts as identity provider, it typically authenticates the claim by passing the same credentials to the ldap server.
1.1.1 username and password credentials
this is the simplest form of authentication. when it is exposed to users, it places the burden of memorizing the password on them. when it is realized with system-to-system authentication, then a password to a server may end up being shared by multiple other apis. the use of username/password pairs as credentials is a very common practice but it is not recommended from two perspectives: passwords have a level of predictability whereas the ideal is to maximize on randomness or entropy. username-password pairs are a low entropy form of authentication. the maintenance of passwords is difficult. if you need to change a password, then you immediately affect all apps that make use of that password. until each of these has been reconfigured you have broken communication with them. as a consequence, there is no way you can block access to one app in particular without blocking all the apps that use the same username and password.
1.1.2 multi-factor authentication
recognizing the weakness of username and password credentials, an app using multi-factor authentication (mfa) demands from the user a one-time usage token she receives after authenticating with her credentials. this token may be delivered to her through sms when the app requests a multi-factor authentication (mfa) provider to do so. the user may also have a digital key which provides her with a token that the app can validate. an rsa securid is an example of this. when the app receives the token which it validates with the mfa provider, it proceeds to consume your api.
1.1.3 token based credentials a better alternative to username password credentials
a better alternative to username password credentials are token-based credentials, which provide higher entropy and a more secure form of authentication and authorization. the idea is for the identity provider to issue tokens based on an initial authentication request with username/password credentials. from then on, the app only has to send the token, so the net result is a great reduction in username/password credentials going to and fro over the network. also, tokens are usually issued with an expiration period and can be revoked. furthermore, because they are issued uniquely to each app, when you choose to revoke a particular token or if it expires, all the other apps can continue to use their tokens independently.
in figure 3, janet signs into her app. the app authenticates her and requests a token from the identity provider. this authenticates her with the identity store and then responds to the app with a token. the app proceeds to call the api with the token.
1.2 api and server authentication
your api must be able to authenticate itself to the apps which consume it. likewise, when your api interacts with servers, they must authenticate themselves to the api. in both cases, you strive to avoid man-in-the-middle attacks which sometimes take the form of malicious software pretending to be a server or indeed your api.
1.3 user and app authorization
1.3.1 role-based access control
typically, every business, enterprise or organization is divided into groups of employees around related business functions. for example, think of the nursing team and the medical doctor team and the catering team in a hospital. the employees working within an organization have a static function defined by these group boundaries.
this group information can be used when software users interact with an app and you need to restrict their access according to the authorization or access control rule in place for that software. you can use the group they belong to in order to identify their role when using the app. groups are role and app agnostic. they are purely business level divisions. ldap servers use the concept of groups for this purpose. identity providers are responsible for retrieving this group information from the identity store. a role is an app-specific definition of access control. a user will typically adopt multiple roles defined by each app she uses.
role-based access control (rbac) represents a very simple access control mechanism. an app need not keep a record of each user’s level of access to its functions and data. rather it uses roles to abstract away from those details and assign degrees of access to groups of users which the role represents.
1.3.2 attribute-based access control
going beyond the static assignment of roles to users based on the organizational groups to which they belong, attribute-based access control (abac) aims to facilitate the dynamic determination of access control based on some sort of circumstantial information available at the time of the api call. things like the time of day, the role, the location of the api, the location of the app and combinations of conditions contribute to the determination of the degree of access. xacml is a standard which defines the rules that must be executed in order to evaluate the level of access at the time of the api call. the understanding is that this may change from call to call. abac often dictates the requirement that your api will respond with subsets of data according to the access control decisions related to the user.
1.3.3 delegated access control with oauth 2.0
the http-based oauth 2.0 framework allows an app to obtain access to a resource exposed by your api either on its own behalf or on behalf of the user who owns the resource. thus it allows users to delegate access control to third party apps. to facilitate this, your api must collaborate with an oauth 2.0 authorization server, checking each incoming call for an access token, which it must validate with the authorization server. the response from the authorization server will indicate whether the access token is valid (it was issued by the oauth provider and it hasn’t expired) as well as the scope of access for which the token was issued.
1.4 federated identity
the token-based approach to authentication allows for the separation of the issuing of tokens from their validation and thus facilitates the centralization of identity management. the developer of each api need only concern herself with incorporating validation logic within the api so that upon invocation, it looks for the token in the request and then validates it with the centralized identity provider. if the token is deemed to be valid (the user or app to whom the token was issued have sufficient authorization for this call), then the api should proceed to process the call. the parties involved in this call form part of a single security context.
in other words, the identity provider is able to recognize and authenticate the app, the user, and the api because their identity and shared secret password are in its identity store. however, your api will not always be exposed to an app and user which the identity provider can possibly recognize. what if you wish to expose your api to an app in the hands of a user from another company or even from another business unit within your own company? large companies can have many security contexts with separate identity stores and providers. federated identity solves this problem and federated identity providers collaborate to facilitate the authentication and authorization of users who belong to different security contexts.
in figure 4, janet signs into her app. the app authenticates her and requests a token from the orders identity provider. this authenticates her with the orders identity store and then responds to the app with a token. the app proceeds to call either the orders api or the shipping api. in both cases, the orders identity provider validates the token. the orders identity provider and the shipping identity provider are federated. the shipping identity provider knows that the token has been signed by the orders identity provider to which it delegates the token validation.
1.4.1 single sign-on multi-experience
the security assertion markup language ( saml ) is an industry standard which has become a de facto standard for enterprise level identity federation. it allows identity providers to communicate authentication and authorization information about users to service providers in a standard way.
a saml assertion can be issued by an identity provider in one security context and be inherently understandable by an identity provider in another context. saml assertions typically convey information about the user including the organizational groups to which the user belongs, together with the expiry period of the assertion. no password information is provided. the identity provider which issues the assertion signs it. the identity provider which has to validate the assertion must have a trust relationship with the issuing identity provider (see digital signatures 2.1.1). the primary driver for the use of saml for use within the enterprise is single sign-on (sso). users don’t have to keep separate identities for every application software that they use. rather they sign on once with an identity provider and 8 from then on any links to applications allow them to bypass the login page of each of these. such a setup ultimately delivers the desired user experience of not having to maintain multiple sets of username and password credentials and of signing in once and subsequently bypassing login pages to all of the applications within the enterprise. this sso experience is usually delivered with a ui portal which has links to all relevant applications that eliminate the need for any further authentication by the user.
saml can also be used within the context of apis. we explore this next.
1.4.2 single sign-on single experience
the expectation that an identity provider in one security context will understand a token issued by an identity provider in another security context may very well be reasonable within the boundaries of a particular enterprise. however, this may not be so for external company acquisition scenarios and for dealings with partners and saas . the industrial standards which we will explore next facilitate the necessary interoperability which allows for any api to deliver its service to apps while relying upon a federated identity provider for the authentication and authorization of the users which are outside of its security context.
22.214.171.124 ws-security with saml assertions
ws-security (in particular ws-trust) allows identity providers to expose soap web services that will issue identity tokens to requesting apps. a saml assertion is one such possible token. the same app can then invoke a soap web service with the saml assertion in the header.
ws-security also caters for the general needs of integrity and confidentiality through xml signature and xml encryption .
126.96.36.199 openid connect with jwt id tokens
openid connect is built on top of oauth 2.0 to provide a federated identity mechanism that allows you to secure your api in a way similar to what you would get were you to exploit ws-security with saml. it was designed to support native and mobile apps while also catering for the enterprise federation cases. it is an attractive and much more lightweight approach to achieving sso within the enterprise than the corresponding ws-security with saml. its simple json/rest-based protocol has resulted in its accelerating adoption.
apart from oauth 2.0 access tokens, openid connect uses jwt (jot) id tokens, which contain information about the authenticated user in a standardized format. your api can make an access control decision by calling out to a userinfo endpoint on the identity provider to verify if the user pertains to a certain role. just like saml assertions, jwt id tokens are digitally signed (see digital signatures 2.1.1) so a federated identity provider can decide to accept them based on its trust relationship with the identity provider that issued them.
what’s next for api security?
in this series, i’ll continue to show how mulesoft’s anypoint platform can ensure that your api is highly available to respond to clients and can guarantee the integrity and confidentiality of the information it processes. we explore in depth the main security concerns and look at how the it industry has responded to those concerns with different types of methods. we present anypoint platform as fully capable of managing and hosting apis that are secure according to the highest industry standards.
Published at DZone with permission of Nial Darbey, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
From On-Prem to SaaS
Auditing Tools for Kubernetes
Top 10 Engineering KPIs Technical Leaders Should Know
SRE vs. DevOps