Thursday, August 22, 2013

URL shortening for REST API

REST APIs are easy to document. And their use often involves small variations in the URI. Therefore , URI shortening and parameter abbreviation helps particularly on mobile devices.  The difference between a regular URL shortening and this kind of smaller URI's is that these will not necessarily be a hash. The URI's can have meaningful abbreviations given that we could  control which API's they map to  and because the APIs are limited. The shortened URI' s could  be stored in a database and the URI's they map to could  still be available independently. This means that we can develop the MVC route based API's as usual and have this service as an add on. The service merely redirects to the actual API"s as HTTP 302 and not require any static or dynamic pages assigned to the short URIs. This service could even live in the http proxy, if the API uses one. The proxy can be transparent on this redirection.
API documentation has to be kept in sync with this short URI mapping. Documentation can be generated from the config routes in which case the short APIs may need their own listing and correspond API documentation redirects. if the framework were to allow such short URI services, then thereis no need for an external service or database and short URi's  could be provisioned in the config files itself. This will also help keep the documentation together.
Query string or Request body parameter abbreviation requires changes on the API implementation. Parameters need not be specified with their full names but by their first initial or some shortener convention.  Only the parameter lookup changes so they can be kept in sync with their corresponding actual parameters. Together with abbreviations, short URI and documentation provide compelling convenience. Statistics could be gathered for both short and regular URI's to warrant continued usage.

Wednesday, August 21, 2013

This is  a quick overview of the OAuth RFC 6749 and will cover some salient features mentioned there.
Implicit workflow is described as follows:
The client is issued an access token directly instead of the authorization code. This is done on the basis of resource owner authorization The client is issued an access token. Client is not verified.  The access token is sent back in the uri fragment. Unauthorized parties could gain access to it. In some cases client can be verified with the redirect uri. Access tokens are issued with the minimal scope.  Phishing attacks are possible where the access token can be switched with the another token previously issued to the attacker. Due to such security considerations, when authorization code grant is available, implicit might not be provisioned.
Other security scenarios include the following:
1) A malicious client can impersonate another client and obtain access to protected resources. This can be avoided with the registration of redirect uri and receiving authorization responses. The authentication server must authenticate the client whenever possible.
2) Access tokens must be kept confidential in transit and storage. TLS can be used for transit and database could be encrypted.
3) Authorization servers may issue refresh tokens to both web application and native application clients. Refresh tokens must be issued only to the client who was previously issued the token. The authorization server could also use refresh token rotation where the clients who used the previous token can be identifed.
4) Authorization codes must be short lived since it is used for the exchange of a token.
5) The redirect_uri parameter can be manipulated.


Tuesday, August 20, 2013


In the previous post, there was a mention for cross user access or admin access, however in this post we talk about infrastructure support for mobile devices. One of the things we discussed was expiration of access. With mobile devices and other applications, there can be convenience provided to the user such that her login efforts are minimized. For example the authorization website where the user login to grant access to a client, may choose to remain signed in for the duration of a session that is longer the token expiry time.  Clients in expiry of their tokens need not request the user to login again. That can be maintained by the site. Further the user may grant indefinite time access or until explicit revoke at the website such that the client could continue to have the web site send redirects. Strictly speaking this is not OAuth. It's just a convenience provisioned outside the OAuth.

Monday, August 19, 2013

OAuth testing discussion continued.

In the previous posts we discussed that OAuth testing depends on the nature of the token. The token is granted to a client for access to a user's resources. Therefore, OAuth APIs are largely qualified by user first and then client. So APIs are resource qualified in the form '/v1/users/me/clients/clientId'.  The reverse order of '/v1/clients' is generally not needed to be maintained because the clients don't have any access unless a user authorizes a resource.
That said, it is important to consider admin access such as one that can reach across users. Is it necessary to have admin access ? or should users manage their own client authorizations ? This is the topic we will briefly consider.
Resources that are not user-specific are generally less demanding on security as opposed to resources that are user owned. For example, the reward cards and balances on them that a user has is considered confidential and so no other user should have access to the same. However, what if there is governance required such as when the law enforcement agencies want to know how may coffee drinks you need to have before you get your free one ? Or even more pertinent where was your last purchase store in case you died ? Governance has a notorious connotation to big brother snooping but in our technical discussion here we refrain from such discussion and consider the case where it is important for another user or system to know information of other users. This could be as benign as friends sharing card information to see which card needs to be used first for the free drink. In OAuth spec, there is no limitation against admin access or access to other users information. In OAuth there is no mention for grouping users or clients. In OAuth there is no mention for API aliasing or shortening or parameter abbreviation for ease of use by one or more users especially from handheld devices. This is left to individual vendors to determine.
In the case where vendors choose to have cross user access or admin client access, the nature of the token granted is relevant. This could be scoped to include certain information such as last store while excluding certain information as credit cards or social security number. Basically, the protocol does not and should not prevent granularity of access and accessors be it the client or the user.
This definitely calls for OAuth tests so that security, functionality and performance are not affected. The RFC discusses several security implications from the user and client point of view however testing may need to be customized to the implementation. The implementation can be as simple as requiring all authorizations owned and delegated to the end user. 
Bearer token recommendations from RFC
1) Bearer tokens must be safeguarded since it gives access to the bearer. It should not appear in the clear such as in the header or in cookies. It should be passed only over secured traffic.
2) TLS certificate chains should be validated otherwise DNS hijackers can steal the token and gain unintended access
3) https should be used for all OAuth communications. The transport layer security is necessary for encrypting the traffic and securing the endpoints.
4) Bearer tokens should not be stored in the cookies. Implementations must not store bearer tokens in cookies because it can lead to cross site forgery attacks.
5) Bearer tokens should only be issued as short lived. One hour or less is recommended. Using short lived bearer tokens means that very few will gain access to it to misuse it.
6) Bearer tokens should always be scoped, scoping their use to the designated user or party. This is important because we don't want to grant universal access.
7) Bearer tokens should not be passed in page URLs since browses, web servers, and other software may not adequately secure URLS. The token may appear in web server logs and other data structures.
Appropriate error codes must be returned to deny specific requests. These include:
1) invalid_request - resource access error response This covers cases where the request is to be denied on grounds of invalid user or client. Http Status code is 400
2) invald_token - This is also a resource access error response. This covers the case where the token may have expired or the client has used a fabricated token. Http Status code is 401 (unauthorized)
3) insufficient_scope - this is also a resource access error response. This covers the case where the token has not been scoped and may cause security vulnerability. Http Status code is 403 (forbidden).
An example of a successful response could be
     HTTP/1.1 200 OK
     Content-Type: application/json;charset=UTF-8
     Cache-Control: no-store
     Pragma: no-cache

     {
       "access_token":"mF_9.B5f-4.1JqM",
       "token_type":"Bearer",
       "expires_in":3600,
       "refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA"
     }


Sunday, August 18, 2013

OAuth testing discussion continued

The previous post on OAuth testing discussed the mapping of user and client for grant of token. Therefore testing discussed mostly on the setup of a user and client for the purpose of grant token. The token has a certain expiry time. There was some mention about scope and state. In this post, we discuss the revokes
When the authorization website grants access to a client from a user, the client can get a "bearer" token to access the protected resources. This token can be used by anyone for any of the APIs.  The authorization server must implement TLS so that the passwords and bearer tokens can flow.
OAuth only mandates that the token are issued for less than an hour. However clients, user and the authorization website could establish trust relationship the first time the authorization occurs. This is outside the scope of OAuth and may be provided as a mere convenience to the user from having to sign on each time for the client.
If this relationship were to be persisted it would merely be a table mapping the client with the user on this server. The records could be added when the authorization completes and revoked when the user explictly wants to clean up the clients she has authorized. The authorization website identifies the user by the user id and the client by the client id. When the client requests the authorization again subsequently, the website may have the user signed on from a previous session and hence uses the same to generate a token for the client. Neither the user specifies password again, nor does the client change request anything different other than accepting the redirect uri response with the token it is expecting.
Care should be taken to ensure that retries by the user do not create duplicate rows in the table maintained by the authorization website. In addition to user explicit revokes, the website may grant additional session time than the usual token expiry time. This can be facilitated with the use of a timestamp.
Lastly the website may do periodic cleanups and archival of the trust relationships. The default case could be to not store the trust relationships and have the token expirty time force re-authentication.
In the previous post, there was a sample code for objective C. The XCode project comes with a test  project that can be used for testing the code in the project. Here is an example for the code in the test:

//
//  Starbucks_API_ExplorerTests.m
//  Starbucks API ExplorerTests


#import "Starbucks_API_ExplorerTests.h"

@implementation Starbucks_API_ExplorerTests

- (void)setUp
{
    [super setUp];
}

- (void)tearDown
{
    // Tear-down code here.
    
    [super tearDown];
}

- (void)testExample
{
    STFail(@"Unit tests are not implemented yet in Starbucks API ExplorerTests");
}

@end