<p><strong>Edit</strong></p>

<p>Read about it from <a href="https://craftcms.stackexchange.com/a/1288/57">the man himself</a>.</p>

<hr>

<p>I haven't looked at the internals of Dukt's oAuth plugin to know how it works, but the proper way to do it would be for his plugin to provide a hook (or hooks) that let's other plugins register their own oAuth providers.</p>

<p>If it's not currently setup to do that, you can make a <a href="http://mailto:hello@dukt.net" rel="nofollow noreferrer">feature request</a> or <a href="https://github.com/dukt/craft-oauth" rel="nofollow noreferrer">pull request</a>.</p>

CC BY-SA 3.0
2014-07-19T23:12:33.093Z
2014-07-20T16:52:08.067Z
2017-04-13T12:51:00.253Z
Brad Bell
<p>Adding a new provider to OAuth plugin is a two step process:</p>

<p><strong>Create a new OAuth Provider in lusitanian/oauth</strong></p>

<p>The first thing you will need to do is to add support for your new provider in <a href="https://github.com/Lusitanian/PHPoAuthLib">lusitanian/oauth OAuth PHP library</a>. This library supports OAuth 1 &amp; 2 authentication process and you can give a look at other providers to get started writing your own. </p>

<p>It's usually just a matter of a smart copy paste from the code of another provider, by updating:</p>

<ul>
<li>scopes</li>
<li>api URL</li>
<li>authorize URL</li>
<li>access token URL</li>
</ul>

<p>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.</p>

<p>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.</p>

<p>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.</p>

<p><strong>Create an OAuth Provider Source in the OAuth plugin</strong></p>

<p>The OAuth provider source class makes the link between between your lusitanian/oauth provider and Craft OAuth plugin.</p>

<p>You should name the file based on lusitanian/oauth's provider file name and save it under <code>oauth/providers</code> folder. For example if your lusitanian/oauth provider file is named <code>Vimeo.php</code>, you provider source file will be named <code>VimeoOAuthProviderSource.php</code>.</p>

<p>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.</p>

<p>When you're ready with your OAuth provider source, perform a pull request on <a href="https://github.com/dukt/craft-oauth">Craft OAuth plugin on GitHub</a> and your provider will be added to the next release.</p>

<p><strong>Checking that OAuth plugin is installed</strong></p>

<p>In your plugin, you will have to check that OAuth is installed before using it in order to prevent errors.</p>

<p>In your templates you can check that OAuth plugin is installed this way:</p>

<pre><code>{% if craft.oauth is not defined %}

    {# OAuth plugin not installed #}

{% else %}

    {# OAuth installed #}

{% endif %}
</code></pre>

<p>In your php files, you could do it that way:</p>

<pre><code>$plugin = craft()-&gt;plugins-&gt;getPlugin('oauth');

if(!$plugin)
{
    // oauth is not installed
}
else
{
    // oauth is installed
}
</code></pre>

<p>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.</p>

CC BY-SA 3.0
2014-07-20T15:18:23.217Z
2014-07-20T15:18:23.217Z
Benjamin David
<p>I’m creating a plugin that will ultimately create new entries using the Moves API (<a href="https://dev.moves-app.com" rel="nofollow">https://dev.moves-app.com</a>). The Moves API needs OAuth make authenticated calls on a user's account, so I'm using Dukt's OAuth plugin (<a href="https://dukt.net/craft/oauth" rel="nofollow">https://dukt.net/craft/oauth</a>) to handle the OAuth authentication process.</p>

<p>I actually have it up and running, but to create an Oauth provider that isn't supported natively by Dukt's plugin, I've added files to their folders.</p>

<p>Here’s what I’ve done.</p>

<ol>
<li><p>Created a Moves.php file in the <code>craft/plugins/oauth/vendor/chrisnharvey/oauth/src/Oauth/Provider/</code> folder that sets up the necessary info to communicate w/ the Moves API - Link to source: <a href="https://gist.github.com/bwlng/2cb486c8d83001deae19#file-moves-php" rel="nofollow">https://gist.github.com/bwlng/2cb486c8d83001deae19#file-moves-php</a></p></li>
<li><p>Created a MovesOAuthProviderSource.php file in the <code>craft/plugins/oauth/vendor/chrisnharvey/oauth/providers/</code>folder that echoes the other providers Dukt supports out of the box. Link to source: <a href="https://gist.github.com/bwlng/2cb486c8d83001deae19#file-movesoauthprovidersource-php" rel="nofollow">https://gist.github.com/bwlng/2cb486c8d83001deae19#file-movesoauthprovidersource-php</a></p></li>
</ol>

<p>Is there a better way to do this? Ideally want to require the Dukt plugin be installed, and then keep the code I've linked to above in my own folder in <code>craft/plugins/moves</code>.</p>

<plugin-development><plugins>
What's the proper way to create a plugin that uses and extends another author’s plugin?
# What's the proper way to create a plugin that uses and extends another author’s plugin?

(Asked by: username_0 on 2014-07-19)

I’m creating a plugin that will ultimately create new entries using the Moves API (<https://dev.moves-app.com>). The Moves API needs OAuth make authenticated calls on a user's account, so I'm using Dukt's OAuth plugin (<https://dukt.net/craft/oauth>) to handle the OAuth authentication process.

I actually have it up and running, but to create an Oauth provider that isn't supported natively by Dukt's plugin, I've added files to their folders.

Here’s what I’ve done.

1. Created a Moves.php file in the `craft/plugins/oauth/vendor/chrisnharvey/oauth/src/Oauth/Provider/` folder that sets up the necessary info to communicate w/ the Moves API - Link to source: <https://gist.github.com/bwlng/2cb486c8d83001deae19#file-moves-php>
2. Created a MovesOAuthProviderSource.php file in the `craft/plugins/oauth/vendor/chrisnharvey/oauth/providers/`folder that echoes the other providers Dukt supports out of the box. Link to source: <https://gist.github.com/bwlng/2cb486c8d83001deae19#file-movesoauthprovidersource-php>

Is there a better way to do this? Ideally want to require the Dukt plugin be installed, and then keep the code I've linked to above in my own folder in `craft/plugins/moves`.

**Tags:** plugin-development, plugins

---

## 2 answers

### Answer by username_2 on 2014-07-20. Score: 6 | Accepted answer

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. 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 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.


---

### Answer by username_1 on 2014-07-19. Score: 3

**Edit**

Read about it from [the man himself](https://craftcms.stackexchange.com/a/1288/57).

---

I haven't looked at the internals of Dukt's oAuth plugin to know how it works, but the proper way to do it would be for his plugin to provide a hook (or hooks) that let's other plugins register their own oAuth providers.

If it's not currently setup to do that, you can make a [feature request](http://mailto:hello@dukt.net) or [pull request](https://github.com/dukt/craft-oauth).


---

