Adding a new provider to OAuth plugin is a two step process:
Create a new OAuth Provider in lusitanian/oauth
The first thing you will need to do is to add support for your new provider in lusitanian/oauth OAuth PHP library (https://github.com/Lusitanian/PHPoAuthLib). This library supports OAuth 1 & 2 authentication process and you can give a look at other providers to get started writing your own.
It's usually just a matter of a smart copy paste from the code of another provider, by updating:
- scopes
- api URL
- authorize URL
- access token URL
That said, you will sometimes have to adjust your code to support the web service's specificities. Read your web service's authentication docs carefully to understand who they implemented the OAuth authentication process and you'll be good to go.
So when your provider is ready, just perform a pull request on lusitanian/oauth OAuth PHP library on GitHub. We're pulling new providers on each Craft OAuth plugin release, so if you got your provider added to lusitanian's library, it will be automatically part of Craft OAuth plugin's next release.
At this point, if you add your new provider in Craft OAuth plugin code, it won't do anything and the provider will not appear in OAuth settings.
Create an OAuth Provider Source in the OAuth plugin
The OAuth provider source class makes the link between between your lusitanian/oauth provider and Craft OAuth plugin.
You should name the file based on lusitanian/oauth's provider file name and save it under oauth/providers
folder. For example if your lusitanian/oauth provider file is named Vimeo.php
, you provider source file will be named VimeoOAuthProviderSource.php
.
This file contains basic informations like the web service's console url, its name, and a simple getAccount() method, and when you'll add it to the oauth/providers folder, your provider will start showing up in Craft CP / Plugin / OAuth.
When you're ready with your OAuth provider source, perform a pull request on Craft OAuth plugin on GitHub (https://github.com/dukt/craft-oauth) and your provider will be added to the next release.
Checking that OAuth plugin is installed
In your plugin, you will have to check that OAuth is installed before using it in order to prevent errors.
In your templates you can check that OAuth plugin is installed this way:
{% if craft.oauth is not defined %}
{# OAuth plugin not installed #}
{% else %}
{# OAuth installed #}
{% endif %}
In your php files, you could do it that way:
$plugin = craft()->plugins->getPlugin('oauth');
if(!$plugin)
{
// oauth is not installed
}
else
{
// oauth is installed
}
I'm working on a step by step guide on how to implement OAuth in Craft plugin. It's not ready yet but I'll probably start to share the draft in August.