Skip to content
Malouf Developer Docs

oCredentials: An intro

Introduction

When we need to store a user/pass or an auth token of a third-party service, we can use credentials to do so rather than storing them in an .env.

Credentials (oCredentials) are objects that can have one or more credential items (oCredentialItems). Each credential item is typed (oCredentialTypes). If you needed to store a username and password for a given provider, you would create one credential and two credential items, one for username and one for password, creating credential types for each one if not already found in the oCredentialTypes table.

Creating a new credential

  1. Create a new credential

    Open your favorite database management application and open the hq database. Create a new entry in oCredentials making sure you enter in a descriptive name and description. Write down the ID of your new credential as you’ll need it in the last step.

  2. Create any necessary credential types

    If you don’t see any types in the oCredentialTypes table, go ahead and create new entries following the structure of the other entries in that table now. Write down the ID(s) of the types that you will be using as you’ll need them in the next step.

  3. Create your credential items

    You’ll need to use tinker for this step, so jump into the hq docker container and then load tinker.

    docker exec -ti hq bash
    
    # Start up Tinker
    php artisan tinker

    Once tinker is up, run the following:

    # Create new credential item
    $credItem = new hq\eloquent\Credentials\CredentialItem;
    
    # This is the oCredentialTypes ID. Replace '10' with the relevant type ID.
    $credItem->type_id = 10;
    
    # This is the oCredential ID. Replace '7' with your credential ID.
    $credItem->credential_id = 7;
    
    # This is the user/pass/token/etc that needs stored
    $credItem->value = 'supersecurepassword';
    
    # Save it
    $credItem->save();

    That’s it!

  4. Bonus step

    If you changed __ENVIRONMENT__ to PRODUCTION in your malouf-settings.php file, be sure to change it back to DEVELOPMENT!