Tuesday, May 13, 2014

Creating a central Authentication Service for Appnexus

Introduction 

Before you can make calls to any AppNexus API service or report, you must use your username and password to get an authorization token. The token remains active for 2 hours, during which you do not need to re-authenticate.  Click here for more information about Appnexus Authentication Service.

Limitations 

The AppNexus API permits you to authenticate successfully 10 times per 5-minute period. Any subsequent authentication attempts within those 5 minutes will result in an error.

Objective 

  • Create an Authentication Service which could be used by all the services. 
  • Automatically renew the authentication token if it expires. 


Assumptions 

For the sake of this post, I'm assuming that you are using spring for dependency injection and the Authentication Service is being configured as Singleton.

AuthenticationService.java

public interface AuthenticationService {

 String getAuthenticationToken();

}

AuthenticationServiceImpl.java

@Component
public class AuthenticationServiceImpl implements AuthenticationService {

 private String authToken;
 private Date lastUpdated;

 private int validityInMinutes = 120;

 @Autowired
 private AuthenticationRequest authenticationRequest;

 @Autowired
 private AuthenticationClient authenticationClient;

 @Override
 public String getAuthenticationToken() {

  // if authToken is null, get new and update vars
  if (authToken == null || hasAuthTokenExpired()) {
   updateAuthToken();
  }

  return authToken;
 }

 private boolean hasAuthTokenExpired() {

  // check time, if more than validityInMinutes, return true

  DateTime last = new DateTime(lastUpdated);
  DateTime current = new DateTime();

  Period period = new Period(last, current);
  Minutes elapsed = Minutes.standardMinutesIn(period);

  if (elapsed.getMinutes() > validityInMinutes)
   return true;

  return false;
 }

 private void updateAuthToken() {

  ResponseContainer  authResponseContainer =     authenticationClient.authenticate(authenticationRequest);

  authToken = authResponseContainer.getResponse().getToken();
  lastUpdated = new Date();
 }
}

The above code is actually simple, it just follows the steps listed below:

  1. Each time an authentication token is requested, a check is performed to see if the authentication token is null, if yes, then "updateAuthToken" is called, which will retrieve a fresh authentication token from Appnexus and update the variable "authToken", which will be returned to the caller. 
  2. If an existing authentication token is found, then the last updated time is checked to make sure that the token is still valid. If yes, then the token is returned, otherwise "updateAuthToken" is called, which will take care of updating the token. 

Advantages

  1. Using this method, the whole logic of maintaining and supplying a valid authentication token to all the consumers is encapsulated in a single service. 
  2. This will also avoid the overhead of checking for authentication related errors in all the rest calls to Appnexus. 


Pay attention to the fact that "DateTime" has been used to calculate the elapsed duration. This belongs to joda date-time API. Kindly refer to the official documentation for more details.

Kindly post a comment if you either like it or if you feel the content is inappropriate in some way.

No comments :

Post a Comment