Class: OAuth2Driver

OAuth2Driver()

The OAuth2Driver class handles the OAuth2 pairing wizard if an oauth2ClientConfiguration is provided by calling super.onInit(oauth2ClientConfiguration) or if Homey.app.oauth2ClientConfig is set to a valid OAuth2Client configuration. It will make sure Homey.app.OAuth2ClientManager exists and exposes the OAuth2Client associated with this driver via the oauth2Client property.

Constructor

new OAuth2Driver()

Example
const OAuth2Driver = require('homey-wifidriver').OAuth2Driver;
const ExampleAPI = require('example-api');

const oauth2ClientConfig = {
	url: `https://api.example.com/authorize?response_type=code&client_id=${Homey.env.API_CLIENT_ID}&redirect_uri=https://callback.athom.com/oauth2/callback/`,
	tokenEndpoint: 'https://api.example.com/token',
	key: Homey.env.API_CLIENT_ID,
	secret: Homey.env.API_CLIENT_SECRET,
	allowMultipleAccounts: false,
};

class myDriver extends OAuth2Driver { // Which extends WebAPIDriver

	onInit(){
		// This will override onPair() to handle OAuth2 pairing
		super.onInit({oauth2ClientConfig});
	}

	// This method will be called if a oauth2ClientConfig is provided onInit. The data object contains
	// an authenticated OAuth2Account instance which will be destroyed automatically after pairing
	// has ended.
	onPairOAuth2ListDevices() {

     // API calls will be injected with 'Authorization: Bearer <access_token>' from OAuth2Account instance
		this.apiGetCall({uri: 'http://example.api.com/list/devices'})
	        .then(devices => {
	            this.log(devices);
 	        return devices;
	        });
	}

Extends

Methods

customOAuth2OnPair(socket)

This method will be called when the user starts pairing a new device, it executes a OAuth2 flow to retrieve devices linked to the users account.

Parameters:
Name Type Description
socket

onInit(options)

This method needs to be called from a driver.onInit(), it will create an OAuth2Client and override the default onPair() method to handle the OAuth2 Flow.

Parameters:
Name Type Description
options Object
Properties
Name Type Description
oauth2ClientConfig Object

OAuth2Client configuration object

Properties
Name Type Description
url string

OAuth2 authorization url

tokenEndpoint string

OAuth2 token endpoint

key string

OAuth2 client key

secret string

OAuth2 client secret

allowMultipleAccounts string

Specifies whether multiple accounts per client are allowed

Overrides: