class NpmTokenFacetImpl extends FacetSupport implements NpmTokenFacet {

  @Override
  public Response login(final Context context) {
    final Payload payload = context.getRequest().getPayload();
    if (payload == null) {
      return NpmResponses.badRequest("Missing body");
    }
    StorageFacet storageFacet = facet(StorageFacet.class);
    try (TempBlob tempBlob = storageFacet.createTempBlob(payload, NpmFacetUtils.HASH_ALGORITHMS)) {
      NestedAttributesMap request = NpmJsonUtils.parse(tempBlob);
      String token = npmTokenManager.login(request.get("name", String.class), request.get("password", String.class));
      if (null != token) {
        NestedAttributesMap response = new NestedAttributesMap("response", Maps.newHashMap());
        response.set("ok", Boolean.TRUE.toString());
        response.set("rev", "_we_dont_use_revs_any_more");
        response.set("id", "org.couchdb.user:undefined");
        response.set("token", token);
        return HttpResponses.created(new BytesPayload(NpmJsonUtils.bytes(response), ContentTypes.APPLICATION_JSON));
      }
      else {
        return NpmResponses.badCredentials("Bad username or password");
      }
    }
    catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  @Inject public  NpmTokenFacetImpl(final NpmTokenManager npmTokenManager);

}