Download the PHP package shaunpersad/api-foundation without Composer

On this page you can find all versions of the php package shaunpersad/api-foundation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package api-foundation

0.1.6 Notes

Version 0.1.6 adds support for signing in with Google+. Note, however, that you do not need to be an actual Google+ user to sign in using Google+. You simply need a Google account of any kind. There are two supported ways to implement this in your app: the recommended hybrid server-side flow using a server code: https://developers.google.com/+/web/signin/server-side-flow and the pure server-side flow: https://developers.google.com/+/web/signin/redirect-uri-flow.

The hybrid server-side flow is supported in Api Foundation via the gplus_server_code Grant Type, where you must pass a gplus_server_code parameter which is the "one-time authorization code" specified in Google's documentation.

The pure server-side flow corresponds to the gplus_access_token Grant Type, where you must pass a gplus_access_token parameter which is an access_token gotten by any other means from Google.


This version also adds further flexibility by adding in new config options to specify database field names specific to your app.

Note: if you are upgrading from a previous version of this package, please add a field in your users table that will be used as the Google+ user id. If you are using our original migrations, you can simply run >php artisan migrate --package="shaunpersad/api-foundation" to add in the field automatically.

Introduction

This is a package for Laravel 4 that provides a basis for creating APIs. Particularly, it allows for OAuth 2.0 implementation using any Grant Type your application requires, including custom Grant Types.

Additionally, it also standardizes all API responses into an easily definable format.

Key Concepts

OAuth 2.0 in ApiFoundation

OAuth 2.0 support is built on top of bshaffer's Oauth2 Server Library for PHP: http://bshaffer.github.io/oauth2-server-php-docs/

For excellent descriptions of OAuth 2.0 and how it is implemented, please check out his documentation.

As a high-level introduction to how OAuth 2.0 is used in ApiFoundation, essentially there is a "Token" endpoint where, given particular parameters (such as a user's username and password), returns an Access Token which (usually) maps directly to a user and can then be used to authenticate future API requests.

Those particular parameters are determined by which Grant Type you choose to use in your API. There are several Grant Types to choose from:

Supporting multiple Grant Types means that your API can be used in numerous situations while still providing a secure method for access, including in mobile apps, in front-end JavaScript, or even completely server-side.

Installation

You may use ApiFoundation with new projects or existing, however existing projects will require some modification. We will start with the steps for a new project first.

New Project Installation

Install via composer.

require: "shaunpersad/api-foundation": "0.1.6"

Add the service provider to your list of providers in app/config/app.php:

'Shaunpersad\ApiFoundation\ApiFoundationServiceProvider'

Publish the included config file, to make it available to your project for modification:

php artisan config:publish shaunpersad/api-foundation

This copies the config file to app/config/packages/shaunpersad/api-foundation

Run the included migrations (Note: this will create a "users" table):

php artisan migrate --package="shaunpersad/api-foundation"

This is an included database seeder which you may wish to use as a basis for your own seeder: shaunpersad/api-foundation/src/Shaunpersad/ApiFoundation/Database/OAuthSeeder.php

The config file and the created tables are designed to work together out of the box, however should you choose to modify the users table, please check the config file to make sure you change the appropriate values.

Also, if you plan to utilize Facebook integration, please set a Facebook App ID and a Facebook App Secret in the config file.

Find the included sample-routes.php file: shaunpersad/api-foundation/src/Shaunpersad/ApiFoundation/sample-routes.php

In it, you will find the various routes you may wish to implement, which will be described in further detail in the "Endpoints" section. Copy these routes into your project.

Existing Project Installation

Install via composer.

require: "shaunpersad/api-foundation": "0.1.6"

Add the service provider to your list of providers in app/config/app.php:

'Shaunpersad\ApiFoundation\ApiFoundationServiceProvider'

Publish the included config file, to make it available to your project for modification:

php artisan config:publish shaunpersad/api-foundation

If you already have your own "users" table, DO NOT run the included migrations. Instead, create your own migration, then find the included create_oauth_tables migration file (shaunpersad/api-foundation/src/migrations/) and copy the code into your own migration file, then run this migration.

This is an included database seeder which you may wish to use as a basis for your own seeder: shaunpersad/api-foundation/src/Shaunpersad/ApiFoundation/Database/OAuthSeeder.php

If you are using your own "users" table, then you will likely need to modify the config file to point ApiFoundation to the correct fields in your users table. Note: you will need to have a field that corresponds to a "username", e.g. an email address or an actual username. You will also need to have a password field.

Also, if you plan to utilize Facebook integration, please set a Facebook App ID and a Facebook App Secret in the config file.

Next, you may need to extend the class that controls how ApiFoundation interacts with your database: ModelStorage. While technically you may override any and all methods, you should only have to override a select few to suit your needs:

If using Facebook integration, you may also need to extend the FacebookAccessToken Grant Type if you wish to control exactly how a Facebook access token gets exchanged for one of your Access Tokens. For example, if you do not wish to use the Facebook user's email address as their username.

In order to use your extended ModelStorage and/or FacebookAccessToken classes, you must also override the relevant IoC bindings, which may include oauth2, oauth2_grant_types, and/or oauth2_storage (see the "IoC Bindings" section).

Find the included sample-routes.php file: shaunpersad/api-foundation/src/Shaunpersad/ApiFoundation/sample-routes.php

In it, you will find the various routes you may wish to implement, which will be described in further detail in the "Usage" section. Please read through the comments for each route as you implement them.

Endpoints

The sample-routes.php file contains several routes which can be identified as the following kinds of API endpoints:

The Authorize endpoint

In the sample-routes.php file, this is the /authorize route. This endpoint is used with the Authorization Code Grant Type. A GET request to this endpoint should display a form or other method for a user to log in to your system. A POST request to this endpoint should process the login and redirect the user to either a specified "redirect_uri" or back to the form with an error message. If the user is redirected to the "redirect_uri", that URI should also contain either the Authorization Code ("code" query param), or the Access Token in the URL fragment if the Grant Type is "Implicit".

The Token endpoint

In the sample-routes.php file, this is the /api/v1/get-token route. This is the endpoint that, based on whichever Grant Type you are using, particular parameters are sent and an Access Token is received.

Required Parameters

The Redirect endpoint

In the sample-routes.php file, this is the /login-redirect route. This is the URI that you'd want the user to be redirected to after being authorized through the Authorize endpoint.

Resource endpoint

In the sample-routes.php file, this is the /api/v1/me route. This is an example of an API resource. Passing a valid Access Token to this route will return that authenticated user ("me") as a resource.

Facebook routes

There are two additional routes included to demonstrate the Facebook Access Token Grant Type. With the Facebook App ID and Secret supplied in the config file, the /get-facebook-login route will redirect you to Facebook to log in and authorize your app. After authorizing, Facebook will redirect you to the /facebook-login-redirect route, and display your Facebook access token. This Facebook access token can then be sent to the Token endpoint to exchange for one of your app's Access Tokens.

Google+ routes

There are three additional routes included to demonstrate the two Grant Types associated with Google+, corresponding to the two possible Google+ login flows. With the Google Client ID and Secret supplied in the config file, the /get-gplus-login route will redirect you to Google to log in and authorize your app. After authorizing, Google will redirect you to the /gplus-login-redirect route, and display your Google+ access token. This Google+ access token can then be sent to the Token endpoint to exchange for one of your app's Access Tokens.

IoC Bindings

Helper Classes

All of the response objects utilize the structure defined by the api_response_array binding.

Examples

For reproducibility, all examples shown have the following assumptions:

Using Authorization Code Grant Types

In your browser, navigate to http://apitest.local/authorize. You should get an error, as the comments in the sample-routes.php file state that:

You must also have the response_type, client_id, state, and redirect_uri set in the URL query, with response_type = "code" if not implicit (token if implicit) client_id = your client id, state = any random thing, redirect_uri = a valid redirect_uri from the database.

With the data from the included seeder, client_id = "testclient", redirect_uri = "http://apitest.local/login-redirect" Including these in the URL query will cause the login form will be displayed properly.

e.g. http://apitest.local/authorize?client_id=testclient&response_type=code&redirect_uri=http://apitest.local/login-redirect&state=sdjf

For error checking, try removing parameters.

Authorize the app by clicking the "yes" button, with valid credentials. If using the included seeder, [email protected] and password should suffice. You should then be redirected to the redirect_uri supplied, with the code parameter in the URL as your Authorization Code, or the access_token parameter in the URL fragment as your Access Token if the response_type parameter was set to token.

If you received an Authorization Code, you may then POST it (along with the other required params) to the Token endpoint to receive an Access Token

e.g. (Using CocoaRestClient): https://www.dropbox.com/s/c4m86xgu94fpr1r/Screenshot%202014-06-23%2017.14.41.png

Using the Password Grant Type

POST the required credentials and other params: https://www.dropbox.com/s/h7xmd9qlz7ft9vz/Screenshot%202014-06-23%2017.18.06.png

Using the Facebook Access Token Grant Type

In your browser, navigate to http://apitest.local/get-facebook-login

You should be redirected to Facebook to log in and authorize the app. Once you have authorized or if you have previously authorized the app, you will be redirected back to http://apitest.local/facebook-login-redirect, and your Facebook Access Token will be displayed.

You may then POST it (along with the other required params) to the Token endpoint: https://www.dropbox.com/s/dzaxzva56tdcc92/Screenshot%202014-06-23%2017.23.40.png

Accessing resources using Access Tokens

POST to the "me" endpoint with a valid Access Token:

  1. the seeded user: https://www.dropbox.com/s/dr48oanlpq2k9ju/Screenshot%202014-06-23%2017.27.06.png
  2. the facebook user: https://www.dropbox.com/s/h0t0f1e482llbu9/Screenshot%202014-06-23%2017.25.33.png

All versions of api-foundation with dependencies

PHP Build Version
Package Version
Requires php Version >=5.4.0
illuminate/support Version 4.2.*
bshaffer/oauth2-server-php Version v1.3
bshaffer/oauth2-server-httpfoundation-bridge Version v1.0
facebook/php-sdk-v4 Version 4.0.*
google/apiclient Version 1.0.*@beta
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package shaunpersad/api-foundation contains the following files

Loading the files please wait ....