Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 5 down vote accepted

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.

share|improve this answer

Edit

Read about it from the man himself.


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 or pull request.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.