Minimal ActivityPub update endpoint

ActivityPub updates objects by posting an ActivityStreams2 Update Activity to the authenticated user's outbox (discovered from their profile).

I store my photo albums as ActivityStreams2 Collections, so this morning I made a minimal AP-compliant update endpoint today that lets me add captions to them. This is just the server componant. It lets me do:

 curl -vX POST -H "Content-Type: application/activity+json" -H "Authorization: secret-token" -d @as-update.json https://path/to/endpoint

where as-update.json contains:

  "@context": "",
  "type": "Update",
  "name": "Amy captioned a photo.",
  "actor": "",
  "object": {
    "id": "https://uri/of/photo.jpg",
    "name": "A brand new caption."

So far this replaces the entire object with the object embedded in the Activity. Hopefully we'll have a syntax to indicate partial updates in AP soon.

The code (PHP) at https://path/to/endpoint contains a few functions that are at the discretion of the server (how to verify the authenticated user can write, what to do with the activity once it gets it, and exactly how to perform the update):

function verify_token($token){
    // Magic to verify token passed in Authorization header here.
    // I check it against a protected file on the server that contains a randomly generated long string.
    return $response;

function store_activity($activity){
    // Arbitrary server logic to store the activity that was posted.
    // I just dump the JSON in a file.
    return true;

function make_update($activity){
    // Arbitrary server logic to perform the update on the object.
    // I parse the value of object in the activity to work out where its data is stored in the filesystem, then rewrite the appropriate JSON file.
    return true;

And here's the overall flow:

// Authentication
$headers = apache_request_headers();
if(isset($headers['Authorization'])) {
  $token = $headers['Authorization'];
  $response = verify_token($token);
  $me = @$response['me'];
  $iss = @$response['issued_by'];
  $client = @$response['client_id'];
  $scope = @$response['scope'];
  header("HTTP/1.1 403 Forbidden");
  echo "403: No authorization header set.";

  // Something went wrong with verification
  header("HTTP/1.1 401 Unauthorized");
  echo "401: Access token could not be verified.";
}elseif(stripos($me, "") === false || $scope != "update"){
  // The wrong person and scope was returned when the token was verified.
  header("HTTP/1.1 403 Forbidden");
  echo "403: Access token was not valid.";

  // Verified, good to go..

    $post = file_get_contents('php://input');

  if(isset($post) && !empty($post)){

    // Store activity
    $id = date("Y-m-d_h:i:s")."_".uniqid();

      // Perform the update 
        header("HTTP/1.1 201 Created");
        echo "Resource updated";
        header("HTTP/1.1 500 Internal Server Error");
        echo "500: Could not make update (probably a permissions issue). ";

      header("HTTP/1.1 500 Internal Server Error");
      echo "500: Could not store activity log (probably a permissions issue).";

    header("HTTP/1.1 400 Bad Request");
    echo "400: Nothing posted";



Obviously I've stripped out my implementation-specific stuff to make it easier to read. The actual code on my server is here: github/rhiaro/img/pub.php.

The next thing I need to do is make a client that...

🏷 activitypub activitystreams2 activitystreams as2 hacking json photos php server social socialwg swwg