jonnypickett/moesif-laravel

This package is abandoned and no longer maintained. The author suggests using the studio308/moesif-laravel package instead.

Moesif SDK for Laravel 8.*

0.2.2 2023-02-03 21:52 UTC

README

Built For Laravel Latest Version on Packagist Total Downloads Software License

Moesif SDK for Laravel 8.*

Compatibility

Currently only compatible with Laravel 8.*

Installation

Via Composer

$ composer require studio308/moesif-laravel

Laravel uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider.

Configuration

You will need to provide your Moesif Application ID in order to get this working.

You can find your Application ID from the Moesif Dashboard -> Top Right Menu -> Installation

If you don't need to change any other config values, you can get away with just setting the appropriate env variable. The necessary env variable for the Application ID is:

MOESIF_APPLICATION_ID

The other env variable that you can set is:

MOESIF_DEBUG

Some sensible defaults are set for you in the config file, but if you need to modify it, you can modify it to fit your needs after publishing it with the command below.

php artisan vendor:publish

(DO NOT PUT YOUR APPLICATION ID IN THE CONFIG FILE. USE THE ENV VARIABLE.)

Usage

Add the moesif middleware to the api middleware group in your Http/Kernel

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            ...
        ],

        'api' => [
            ...
            'moesif',
            ...
        ],
    ];

Configuration Documentation

Below is an example config/moesif.php configuration file.

<?php

return array(
    
    /*
    |--------------------------------------------------------------------------
    | Moesif Application ID
    |--------------------------------------------------------------------------
    |
    | This is the Moesif application id.
    |
    */

    'applicationId' => env('MOESIF_APPLICATION_ID'),

    /*
    |--------------------------------------------------------------------------
    | Skip
    |--------------------------------------------------------------------------
    |
    | Return true if the event is to be skipped.
    |
    */

    'skip' => function ($request, $response) {
        $host = explode('.', $request->server('HTTP_HOST'));
        return $host[0] != 'api';
    },

    /*
    |--------------------------------------------------------------------------
    | Mask Request Headers
    |--------------------------------------------------------------------------
    |
    | Add or remove request headers.
    |
    */

    'maskRequestHeaders' => function ($headers) {
        if (isset($headers['authorization'])) {
            $headers['authorization'] = str_repeat('*', 18);
        }
        return $headers;
    },

    /*
    |--------------------------------------------------------------------------
    | Mask Request Body
    |--------------------------------------------------------------------------
    |
    | Remove any fields from body that you don't want sent to Moesif.
    |
    */

    'maskRequestBody' => function ($body) {
        if (isset($body['password'])) {
            $body['password'] = str_repeat('*', 18);
        }
        return $body;
    },

    /*
    |--------------------------------------------------------------------------
    | Mask Response Headers
    |--------------------------------------------------------------------------
    |
    | Add or remove response headers.
    |
    */

    'maskResponseHeaders' => function ($headers) {
        return $headers;
    },

    /*
    |--------------------------------------------------------------------------
    | Mask Response Body
    |--------------------------------------------------------------------------
    |
    | Remove any fields from body that you don't want sent to Moesif.
    |
    */

    'maskResponseBody' => function ($body) {
        if (isset($body['token'])) {
            $body['token'] = str_repeat('*', 18);
        }
        return $body;
    },

    /*
    |--------------------------------------------------------------------------
    | Identify User ID
    |--------------------------------------------------------------------------
    |
    | Identify the user.
    |
    */

    'identifyUserId' => function ($request, $response) {
        return null;
    },

    /*
    |--------------------------------------------------------------------------
    | Identify Session ID
    |--------------------------------------------------------------------------
    |
    | Identify the session.
    |
    */

    'identifySessionId' => function ($request, $response) {
        if ($request->hasSession()) {
            return $request->session()->getId();
        } else {
            return null;
        }
    },

    /*
    |--------------------------------------------------------------------------
    | Meta Data
    |--------------------------------------------------------------------------
    |
    | Add any extra data to be sent to Moesif.
    |
    */

    'getMetaData' => function ($request, $response) {
        return [];
    },

    /*
    |--------------------------------------------------------------------------
    | Tags
    |--------------------------------------------------------------------------
    |
    | Add any tags to be sent to Moesif.
    |
    */

    'addTags' => function ($request, $response) {
        return '';
    },

    /*
    |--------------------------------------------------------------------------
    | API Version
    |--------------------------------------------------------------------------
    |
    | Identify the API Version.
    |
    */

    'apiVersion' => function ($request, $response) {
        return null;
    },

    /*
    |--------------------------------------------------------------------------
    | Debug
    |--------------------------------------------------------------------------
    |
    | Set to true to see debug information.
    |
    */

    'debug' => env('MOESIF_DEBUG', false),
    
);

Documentation for the various config values is below.

applicationId

Type: String Required, a string that identifies your application.

identifyUserId

Type: ($request, $response) => String Optional, a function that takes a $request and $response and return a string for userId.

identifyCompany

Type: ($request, $response) => String Optional, a function that takes a $request and $response and return a string for companyId.

identifySessionId

Type: ($request, $response) => String Optional, a function that takes a $request and $response and return a string for sessionId. Moesif automatically sessionizes by processing at your data, but you can override this via identifySessionId if you're not happy with the results.

getMetadata

Type: ($request, $response) => Associative Array Optional, a function that takes a $request and $response and returns $metdata which is an associative array representation of JSON.

apiVersion

Type: String Optional, a string to specifiy an API Version such as 1.0.1, allowing easier filters.

maskRequestHeaders

Type: $headers => $headers Optional, a function that takes a $headers, which is an associative array, and returns an associative array with your sensitive headers removed/masked.

maskRequestBody

Type: $body => $body Optional, a function that takes a $body, which is an associative array representation of JSON, and returns an associative array with any information removed.

maskResponseHeaders

Type: $headers => $headers Optional, same as above, but for Responses.

maskResponseBody

Type: $body => $body Optional, same as above, but for Responses.

skip

Type: ($request, $response) => String Optional, a function that takes a $request and $response and returns true if this API call should be not be sent to Moesif.

debug

Type: Boolean Optional, If true, will print debug messages using Illuminate\Support\Facades\Log

Credits for Moesif Laravel SDK

Additional Tips:

  • The forked (i.e. non-blocking way) of sending data is using exec() with a cURL command. The Php exec() command can be successful but the cURL itself may have 401 errors. So after integration, if you don't see events and data show up in your Moesif Dash. Please turn on debug option, then the cURL command itself will be logged. You can execute that cURL command and see what the issues are. The most common thing to check is if the Application ID is set correctly.

Other integrations

To view more more documentation on integration options, please visit the Integration Options Documentation.