Skip to content
This repository has been archived by the owner on Oct 10, 2020. It is now read-only.

Upgrade to OAuth 2

mptre edited this page May 9, 2012 · 1 revision

Upgrade to OAuth 2

Version 2.0.0 of the wrapper introduced support for authentication using OAuth 2. This shiny new release of the OAuth protocol has simplified the authentication flow for consumers in a very positive manner. Therefore the wrapper was in need of a quite large refactoring process.

For all you using using older releases of the wrapper (i.e authentication using OAuth 1) and are planing to upgrade: I would recommend you to read the OAuth 2 documentation in order to fully understand the new authentication "dance". There's also a real OAuth 2 implementation available over here. The source code is hosted on GitHub. Take a closer look at the controller named sessions (also embedded below) which includes methods for all necessary authentication steps.

<?php
class Sessions extends MY_Controller {

    function Sessions() {
        parent::MY_Controller();
    }

    function callback() {
        // equivalent to $_GET['code']
        $code = (strlen($this->input->get('code')))
            ? $this->input->get('code')
            : null;

        if ($code) {
            try {
                $access_token = $this->soundcloud->accessToken($code);
            } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
                show_error($e->getMessage());
            }

            try {
                $me = json_decode($this->soundcloud->get('me'), true);
            } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
                show_error($e->getMessage());
            }

            $user_data = array(
                'access_token' => $access_token['access_token'],
                'refresh_token' => $access_token['refresh_token'],
                'expires' => time() + $access_token['expires_in'],
                'id' => $me['id'],
                'username' => $me['username'],
                'name' => $me['full_name'],
                'avatar' => $me['avatar_url']
            );

            if ($user = $this->user->add($user_data)) {
                set_cookie(
                    array(
                        'name' => 'user',
                        'value' => $user->hash(),
                        'expire' => 86400
                    )
                );

                redirect($this->_redirect_uri());
            }
        }
    }

    function connect() {
        $this->data['header']['title'] = 'Connect';
        $this->data['view']['authorize_url'] = $this->soundcloud->getAuthorizeUrl();

        $this->view();
    }

    function disconnect() {
        $this->user->delete();

        delete_cookie('user');

        redirect($this->_redirect_uri('sessions/connect'));
    }

    function refresh() {
        try {
            $access_token = $this->soundcloud->accessTokenRefresh(
                $this->user->refresh_token
            );
        } catch (Services_Soundcloud_Invalid_Http_Response_Code_Exception $e) {
            redirect($this->_redirect_uri('sessions/connect'));
        }

        $access_token['expires'] = $access_token['expires_in'] + time();
        $user_data = array_merge($this->data['all']['user'], $access_token);

        $this->user->update($user_data);

        $this->data['header']['title'] = 'Refresh';
        $this->data['view']['minutes'] = round($access_token['expires_in'] / 60);
        $this->data['view']['redirect_uri'] = (strlen($this->input->get('redirect_uri')))
            ? $this->input->get('redirect_uri')
            : $this->_redirect_uri();

        $this->view();
    }

}
Clone this wiki locally