Giter Site home page Giter Site logo

oauth2-sc2's Introduction

SteemConnect V2: OAuth2 Client / Provider

Build Status Codecov Latest Stable Version License

This library is a simple, easy implement OAuth2 client for SteemConnect V2 clients.

It takes the pain from integrating and parsing all the specifics and delivers a great authorization flow for those who aim to handle authorization through SteemConnect V2.

0. Why?

Well, OAuth2 is not that complicated, this project started while building a SteemConnect V2 SDK for PHP, so this project is the Authorization part of another package coming on the next days.

1. Installation.

All you need to do is install this library as a dependency on your project, through composer:

composer require hernandev/oauth2-sc2

2. Usage:

Before using thins library, keep in mind you'll need a SteemConnect application client ID and secret.

2.1. Configuring:

It could not be more simple. Just create a config instance, passing your application credentials:

use SteemConnect\OAuth2\Config\Config;

// creates the configuration object:
$config = new Config('your.app', 'your-long-secret-id-here-keep-it-secret');

After setting your credentials, you will need to decide with scopes you will ask permission to:

// set the scopes you want.
$config->setScopes([
    'login', 'vote', 'comment', 'comment_delete'
]);

If you are not sure about the scopes you will need, those are documented on the SteemConnect wiki.

Finally, we will configure, to which URL the user should return, after granting you the permissions:

// set the return / callback URL.
$config->setReturnUrl('https://my-steem-app-is-awesome.com/login/return');

2.2. Redirecting to Authorization:

That was really, all you need to configure to use the library, pretty cool ham?

Now, of course, you need to redirect the users, to SteemConnect where they will authorize you to act on their behalf.

use SteemConnect\OAuth2\Provider\Provider;

// first, we create a provider instance, passing the configuration needed:
$provider = new Provider($config);

// finally, we can get the redirect URL, so we can send users to SteemConnect:

// get the URL string that you will redirect to.
$provider->getAuthorizationUrl()

2.3. Parsing the Return:

Guess what? super hard to do:

You will need both the config and provider code used before, so I assume you will be clever and put that logic on a common place.

// just call the parse return URL and this library will automatically exchange the access code by the actual token: 
$token = $provider->parseReturn();

On the previous call, $token is an instance of AccessToken class, which can be used for the following things:

// gets the actual token that will be used on requests.
$token->getToken();

// gets the expiration date, so you know the token is no longer valid.
$token->getExpires();

// gets the refresh token, which may be used to issue a new token, if the offline scope was requested.
$token->getRefreshToken();

// gets the standard ID for the account authorizing your app, it means, this field retrieves the account @username
$token->getResourceOwnerId();

2.4. Storing Tokens.

After obtained a given user's Access Token, in most cases, you will need to store the tokens for later usage.

Whatever the approach for storing the tokens (browser session / database), you will need to serialize the tokens and later, decode then.

This can be easily done, by two methods on the Provider:

Encoding:

// encode the AccessToken instance into a JSON string.
$tokenJson = $provider->encodeToken($token);

Decoding:

// decode the JSON string token back into a AccessToken instance.
$token = $provider->decodeToken($tokenJson);

2.5. Refreshing Expired Tokens.

Whenever the offline scope is used to issue a token, the resulting AccessToken instance will contain a field called `refresh_token. The refresh token can be used for issuing a new one.

To issue a new token, using a existing one, that contains a refresh token, just do:

// get a new token from the existing / expired one.
$newToken = $provider->refreshToken($token);

OR

If you only stored the refresh_token field value, you may use that string directly, by doing:

// get a new AccessToken using the refresh token string.
$token = $provider->refreshTokenString($refreshTokenString);  

2.5. Extras:

Of course there are extras!

This library implements the ResourceOwner interface, which means you can also query right away some information about the account which granted you permissions:

// gets the resource owner instance.
$owner = $provider->getResourceOwner($token);

// now you can use any key you may see on steemd.com directly on that $owner object!!!

$owner->profile_json;
$owner->balance;
$owner->reputation;
// and so on...

That's All Folks!

oauth2-sc2's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

akintunde102

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.