• Stars
    star
    936
  • Rank 46,913 (Top 1.0 %)
  • Language
    PHP
  • License
    MIT License
  • Created almost 11 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

OAuth 1 Client

OAuth 1.0 Client

Latest Stable Version Software License Build Status Coverage Status Quality Score Total Downloads

OAuth 1 Client is an OAuth RFC 5849 standards-compliant library for authenticating against OAuth 1 servers.

It has built in support for:

  • Bitbucket
  • Magento
  • Trello
  • Tumblr
  • Twitter
  • Uservoice
  • Xing

Adding support for other providers is trivial. The library requires PHP 7.1+ and is PSR-2 compatible.

Third-Party Providers

If you would like to support other providers, please make them available as a Composer package, then link to them below.

These providers allow integration with other providers not supported by oauth1-client. They may require an older version so please help them out with a pull request if you notice this.

Terminology (as per the RFC 5849 specification):

client
    An HTTP client (per [RFC2616]) capable of making OAuth-
    authenticated requests (Section 3).

server
    An HTTP server (per [RFC2616]) capable of accepting OAuth-
    authenticated requests (Section 3).

protected resource
    An access-restricted resource that can be obtained from the
    server using an OAuth-authenticated request (Section 3).

resource owner
    An entity capable of accessing and controlling protected
    resources by using credentials to authenticate with the server.

credentials
    Credentials are a pair of a unique identifier and a matching
    shared secret.  OAuth defines three classes of credentials:
    client, temporary, and token, used to identify and authenticate
    the client making the request, the authorization request, and
    the access grant, respectively.

token
    A unique identifier issued by the server and used by the client
    to associate authenticated requests with the resource owner
    whose authorization is requested or has been obtained by the
    client.  Tokens have a matching shared-secret that is used by
    the client to establish its ownership of the token, and its
    authority to represent the resource owner.

The original community specification used a somewhat different
terminology that maps to this specifications as follows (original
community terms provided on left):

Consumer:  client

Service Provider:  server

User:  resource owner

Consumer Key and Secret:  client credentials

Request Token and Secret:  temporary credentials

Access Token and Secret:  token credentials

Install

Via Composer

$ composer require league/oauth1-client

Usage

Bitbucket

$server = new League\OAuth1\Client\Server\Bitbucket([
    'identifier' => 'your-identifier',
    'secret' => 'your-secret',
    'callback_uri' => "http://your-callback-uri/",
]);

Trello

$server =  new League\OAuth1\Client\Server\Trello([
    'identifier' => 'your-identifier',
    'secret' => 'your-secret',
    'callback_uri' => 'http://your-callback-uri/',
    'name' => 'your-application-name', // optional, defaults to null
    'expiration' => 'your-application-expiration', // optional ('never', '1day', '2days'), defaults to '1day'
    'scope' => 'your-application-scope' // optional ('read', 'read,write'), defaults to 'read'
]);

Tumblr

$server = new League\OAuth1\Client\Server\Tumblr([
    'identifier' => 'your-identifier',
    'secret' => 'your-secret',
    'callback_uri' => "http://your-callback-uri/",
]);

Twitter

$server = new League\OAuth1\Client\Server\Twitter([
    'identifier' => 'your-identifier',
    'secret' => 'your-secret',
    'callback_uri' => "http://your-callback-uri/",
    'scope' => 'your-application-scope' // optional ('read', 'write'), empty by default
]);

Xing

$server = new League\OAuth1\Client\Server\Xing([
    'identifier' => 'your-consumer-key',
    'secret' => 'your-consumer-secret',
    'callback_uri' => "http://your-callback-uri/",
]);

Showing a Login Button

To begin, it's advisable that you include a login button on your website. Most servers (Twitter, Tumblr etc) have resources available for making buttons that are familiar to users. Some servers actually require you use their buttons as part of their terms.

<a href="authenticate.php">Login With Twitter</a>

Retrieving Temporary Credentials

The first step to authenticating with OAuth 1 is to retrieve temporary credentials. These have been referred to as request tokens in earlier versions of OAuth 1.

To do this, we'll retrieve and store temporary credentials in the session, and redirect the user to the server:

// Retrieve temporary credentials
$temporaryCredentials = $server->getTemporaryCredentials();

// Store credentials in the session, we'll need them later
$_SESSION['temporary_credentials'] = serialize($temporaryCredentials);
session_write_close();

// Second part of OAuth 1.0 authentication is to redirect the
// resource owner to the login screen on the server.
$server->authorize($temporaryCredentials);

The user will be redirected to the familiar login screen on the server, where they will login to their account and authorise your app to access their data.

Retrieving Token Credentials

Once the user has authenticated (or denied) your application, they will be redirected to the callback_uri which you specified when creating the server.

Note, some servers (such as Twitter) require that the callback URI you specify when authenticating matches what you registered with their app. This is to stop a potential third party impersonating you. This is actually part of the protocol however some servers choose to ignore this.

Because of this, we actually require you specify a callback URI for all servers, regardless of whether the server requires it or not. This is good practice.

You'll need to handle when the user is redirected back. This will involve retrieving token credentials, which you may then use to make calls to the server on behalf of the user. These have been referred to as access tokens in earlier versions of OAuth 1.

if (isset($_GET['oauth_token']) && isset($_GET['oauth_verifier'])) {
    // Retrieve the temporary credentials we saved before
    $temporaryCredentials = unserialize($_SESSION['temporary_credentials']);

    // We will now retrieve token credentials from the server
    $tokenCredentials = $server->getTokenCredentials($temporaryCredentials, $_GET['oauth_token'], $_GET['oauth_verifier']);
}

Now, you may choose to do what you need with the token credentials. You may store them in a database, in the session, or use them as one-off and then forget about them.

All credentials, (client credentials, temporary credentials and token credentials) all implement League\OAuth1\Client\Credentials\CredentialsInterface and have two sets of setters and getters exposed:

var_dump($tokenCredentials->getIdentifier());
var_dump($tokenCredentials->getSecret());

In earlier versions of OAuth 1, the token credentials identifier and token credentials secret were referred to as access token and access token secret. Don't be scared by the new terminology here - they are the same. This package is using the exact terminology in the RFC 5849 OAuth 1 standard.

Twitter will send back an error message in the denied query string parameter, allowing you to provide feedback. Some servers do not send back an error message, but rather do not provide the successful oauth_token and oauth_verifier parameters.

Accessing User Information

Now you have token credentials stored somewhere, you may use them to make calls against the server, as an authenticated user.

While this package is not intended to be a wrapper for every server's API, it does include basic methods that you may use to retrieve limited information. An example of where this may be useful is if you are using social logins, you only need limited information to confirm who the user is.

The four exposed methods are:

// User is an instance of League\OAuth1\Client\Server\User
$user = $server->getUserDetails($tokenCredentials);

// UID is a string / integer unique representation of the user
$uid = $server->getUserUid($tokenCredentials);

// Email is either a string or null (as some providers do not supply this data)
$email = $server->getUserEmail($tokenCredentials);

// Screen name is also known as a username (Twitter handle etc)
$screenName = $server->getUserScreenName($tokenCredentials);

League\OAuth1\Client\Server\User exposes a number of default public properties and also stores any additional data in an extra array - $user->extra. You may also iterate over a user's properties as if it was an array, foreach ($user as $key => $value).

Examples

Examples may be found under the resources/examples directory, which take the usage instructions here and go into a bit more depth. They are working examples that would only you substitute in your client credentials to have working.

Testing

$ phpunit

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

More Repositories

1

flysystem

Abstraction for local and remote filesystems
PHP
13,202
star
2

oauth2-server

A spec compliant, secure by default PHP OAuth 2.0 Server
PHP
6,362
star
3

omnipay

A framework agnostic, multi-gateway payment processing library for PHP 5.6+
PHP
5,813
star
4

fractal

Output complex, flexible, AJAX/RESTful data structures.
PHP
3,511
star
5

oauth2-client

Easy integration with OAuth 2.0 service providers.
PHP
3,508
star
6

csv

CSV data manipulation made easy in PHP
PHP
3,282
star
7

commonmark

Highly-extensible PHP Markdown parser which fully supports the CommonMark and GFM specs.
PHP
2,655
star
8

glide

Wonderfully easy on-demand image manipulation library with an HTTP based API.
PHP
2,527
star
9

climate

PHP's best friend for the terminal.
PHP
1,865
star
10

html-to-markdown

Convert HTML to Markdown with PHP
PHP
1,619
star
11

flysystem-aws-s3-v3

[READYONLY SUB-SPLIT]Flysystem Adapter for AWS SDK V3
PHP
1,528
star
12

skeleton

A skeleton repository for League Packages
PHP
1,525
star
13

event

Event package for your app and domain
PHP
1,505
star
14

plates

Native PHP template system
PHP
1,468
star
15

geotools

Geo-related tools PHP 7.3+ library built atop Geocoder and React libraries
PHP
1,352
star
16

color-extractor

Extract colors from an image like a human would do.
PHP
1,283
star
17

mime-type-detection

League Mime Type Detection
PHP
1,218
star
18

uri

[READ-ONLY] URI manipulation Library
PHP
1,013
star
19

pipeline

League\Pipeline
PHP
939
star
20

tactician

A small, flexible command bus
PHP
854
star
21

container

Small but powerful dependency injection container
PHP
828
star
22

period

PHP's time range API
PHP
714
star
23

route

Fast PSR-7 based routing and dispatch component including PSR-15 middleware, built on top of FastRoute.
PHP
638
star
24

iso3166

A PHP library providing ISO 3166-1 data.
PHP
629
star
25

factory-muffin

Enables the rapid creation of objects for testing
PHP
533
star
26

openapi-psr7-validator

It validates PSR-7 messages (HTTP request/response) against OpenAPI specifications
PHP
500
star
27

uri-interfaces

League URI Interfaces
PHP
439
star
28

shunt

[ABANDONED] PHP library for executing commands on multiple remote machines, via SSH
PHP
436
star
29

config

Simple yet expressive schema-based configuration library for PHP apps
PHP
436
star
30

uri-parser

RFC3986/RFC3987 compliant URI parser
PHP
392
star
31

oauth2-google

Google Provider for the OAuth 2.0 Client
PHP
383
star
32

flysystem-cached-adapter

Flysystem Adapter Cache Decorator.
PHP
356
star
33

statsd

A library for working with StatsD
PHP
351
star
34

flysystem-bundle

Symfony bundle integrating Flysystem into Symfony 4.2+ applications
PHP
350
star
35

url

A simple PHP library to parse and manipulate URLs
PHP
347
star
36

booboo

A modern error handler capable of logging and formatting errors in a variety of ways.
PHP
338
star
37

monga

Simple and swift MongoDB abstraction.
PHP
328
star
38

omnipay-common

Core components for the Omnipay PHP payment processing library
PHP
327
star
39

flysystem-sftp

[READ-ONLY SUBSPLIT] Flysystem Adapter for SFTP
PHP
310
star
40

uri-components

[READ-ONLY] League URI components objects
PHP
305
star
41

oauth2-facebook

Facebook Provider for the OAuth 2.0 Client
PHP
297
star
42

omnipay-paypal

PayPal driver for the Omnipay PHP payment processing library
PHP
291
star
43

tactician-bundle

Bundle to integrate Tactician with Symfony projects
PHP
245
star
44

uri-schemes

Collection of URI Immutable Value Objects
PHP
215
star
45

uri-manipulations

Functions and Middleware to manipulate URI Objects
PHP
198
star
46

uri-hostname-parser

A lightweight hostname parser according to public suffix list ICANN section
PHP
195
star
47

omnipay-stripe

Stripe driver for the Omnipay PHP payment processing library
PHP
179
star
48

json-guard

Validation of json-schema.org compliant schemas.
PHP
175
star
49

oauth2-server-bundle

Symfony bundle for the OAuth2 Server.
PHP
170
star
50

commonmark-ext-table

The table extension for CommonMark PHP implementation
PHP
127
star
51

flysystem-local

PHP
117
star
52

glide-laravel

Glide adapter for Laravel
PHP
111
star
53

oauth2-github

GitHub Provider for the OAuth 2.0 Client
PHP
103
star
54

flysystem-ziparchive

Flysystem Adapter for ZipArchive's
PHP
102
star
55

omnipay-example

Example application for Omnipay PHP payments library
PHP
97
star
56

glide-symfony

Glide adapter for Symfony
PHP
91
star
57

oauth2-linkedin

LinkedIn Provider for the OAuth 2.0 Client
PHP
81
star
58

tactician-container

Load Tactician handlers from any PSR-11/container-interop container
PHP
75
star
59

stack-attack

StackPHP Middleware based on Rack::Attack
PHP
74
star
60

flysystem-webdav

[READ ONLY] WebDAV adapter for Flysystem
PHP
70
star
61

flysystem-memory

Flysystem Memory Adapter
PHP
69
star
62

flysystem-dropbox

Flysystem Adapter for Dropbox [ABANDONED] replacement: https://packagist.org/packages/spatie/flysystem-dropbox
PHP
67
star
63

stack-robots

StackPHP middleware providing robots.txt disallow for non-production environments
PHP
67
star
64

oauth2-instagram

Instagram Provider for the OAuth 2.0 Client
PHP
65
star
65

tactician-logger

Adds PSR-3 logging support to the Tactician Command Bus
PHP
62
star
66

omnipay-mollie

Mollie driver for the Omnipay PHP payment processing library
PHP
61
star
67

di

An Ultra-Fast Dependency Injection Container. DEPRECATED
PHP
58
star
68

tactician-doctrine

Tactician plugins for the Doctrine ORM, primarily transactions
PHP
57
star
69

omnipay-authorizenet

Authorize.Net driver for the Omnipay payment processing library
PHP
57
star
70

flysystem-azure-blob-storage

PHP
54
star
71

omnipay-sagepay

Sage Pay driver for the Omnipay PHP payment processing library
PHP
53
star
72

flysystem-aws-s3-v2

Flysystem Adapter for AWS SDK V2
PHP
50
star
73

DEPRECATED-squery

PHP wrapper for osquery
PHP
49
star
74

phpunit-coverage-listener

Report code coverage statistics to third-party services
PHP
48
star
75

thephpleague.github.io

The League of Extraordinary Packages website
SCSS
45
star
76

construct-finder

PHP code construct finder
PHP
40
star
77

factory-muffin-faker

A wrapper around faker for factory muffin
PHP
39
star
78

flysystem-rackspace

Flysystem Adapter for Rackspace
PHP
38
star
79

uri-query-parser

a parser and a builder to work with URI query string the right way in PHP
PHP
37
star
80

flysystem-azure

Flysystem adapter for the Windows Azure.
PHP
35
star
81

omnipay-braintree

Braintree Driver for Omnipay Gateway
PHP
34
star
82

json-reference

A library for working with JSON References.
PHP
33
star
83

commonmark-extras

Useful extensions for the league/commonmark parser
PHP
28
star
84

flysystem-sftp-v3

PHP
28
star
85

uploads

Receive, validate, and distribute uploaded files.
PHP
27
star
86

flysystem-replicate-adapter

Flysystem Adapter Decorator for Replicating Filesystems.
PHP
25
star
87

omnipay-dummy

Dummy driver for the Omnipay PHP payment processing library
PHP
25
star
88

omnipay-worldpay

WorldPay driver for the Omnipay PHP payment processing library
PHP
24
star
89

omnipay-paymentexpress

PaymentExpress driver for the Omnipay PHP payment processing library
PHP
24
star
90

object-mapper

PHP
22
star
91

uri-src

URI manipulation Library
PHP
21
star
92

flysystem-google-cloud-storage

PHP
21
star
93

flysystem-async-aws-s3

PHP
21
star
94

omnipay-migs

MIGS driver for the Omnipay PHP payment processing library
PHP
21
star
95

flysystem-ftp

[SUB-SPLIT] Flysystem FTP Adapter
PHP
21
star
96

omnipay-firstdata

First Data driver for the Omnipay PHP payment processing library
PHP
21
star
97

omnipay-payfast

PayFast driver for the Omnipay PHP payment processing library
PHP
21
star
98

tactician-bernard

Tactician integration with the Bernard queueing library
PHP
20
star
99

omnipay-multisafepay

MultiSafepay driver for the Omnipay PHP payment processing library
PHP
19
star
100

flysystem-gridfs

GridFS Adapter for Flysystem
PHP
19
star