title: Play Framework: How to deal with api keys
body: <p>I've implemented a REST API that handles user authorization with JWT (Json Web Token) and it works fine. Now I'm wondering how to deal with API keys to prevent unauthorized applications from using my API.</p>
  
  <p>The idea would be to add the API key in the request header and implement a custom action builder for it. Now the question is: since I already have a custom action builder that handles  authorization tokens, how do I integrate the new custom action builder?</p>
  
  <p>Assuming this is my JWT custom action builder...</p>
  
  <pre><code>class ApiRequest[A](
    val token: Token,
    request: Request[A]) extends WrappedRequest[A](request) {
  
    ...
  }
  
  class SecuredAction extends ActionBuilder[ApiRequest] {
  
    def invokeBlock[A](request: Request[A], block: (ApiRequest[A]) =&gt; Future[SimpleResult]) = {
    ...
    }
  }
  </code></pre>
  
  <p>... and this one my api_key custom action builder</p>
  
  <pre><code>class ApiKeyRequest[A](
    val apiKey: String,
    request: Request[A]) extends WrappedRequest[A](request) {
  
    ...
  }
  
  class ApiKeyAction extends ActionBuilder[ApiKeyRequest] {
  
    def invokeBlock[A](request: Request[A], block: (ApiKeyRequest[A]) =&gt; Future[SimpleResult]) = {
    ...
    }
  }
  </code></pre>
  
  <p>... how do I use them together in my controller?</p>
  
  <pre><code>object MyController extends Controller {
  
    // here I need to compose SecuredAction with ApiKeyAction...
    def doSomething = SecuredAction.async { implicit request =&gt; 
      ...
    }
  }
  </code></pre>
  
  <p>Thanks.</p>