Skip to content

Commit

Permalink
fix: too fast polling
Browse files Browse the repository at this point in the history
- added exported Events object for better consumer side API
  • Loading branch information
maxinteger committed Nov 29, 2024
1 parent 53045fd commit 6b0a33e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ const lodash = require('lodash');
const OAUTH2_CSRF_TOKEN = 'oauth2-csrf-token';
const OAUTH2_CODE_VERIFIER = 'oauth2-code-verifier';

/**
* Authorization plugin events
*/
export const Events = {
/**
* QR code login events
*/
qRCodeLogin: 'qRCodeLogin',
};

/**
* Browser support for OAuth2. Automatically parses the URL query for an
* authorization code
Expand Down Expand Up @@ -293,7 +303,7 @@ const Authorization = WebexPlugin.extend({
*/
initQRCodeLogin() {
if (this.pollingTimer) {
this.eventEmitter.emit('qRCodeLogin', {
this.eventEmitter.emit(Events.qRCodeLogin, {
eventType: 'getUserCodeFailure',
data: {message: 'There is already a polling request'},
});
Expand All @@ -317,7 +327,7 @@ const Authorization = WebexPlugin.extend({
})
.then((res) => {
const {user_code, verification_uri, verification_uri_complete} = res.body;
this.eventEmitter.emit('qRCodeLogin', {
this.eventEmitter.emit(Events.qRCodeLogin, {
eventType: 'getUserCodeSuccess',
userData: {
userCode: user_code,
Expand All @@ -329,7 +339,7 @@ const Authorization = WebexPlugin.extend({
this._startQRCodePolling(res.body);
})
.catch((res) => {
this.eventEmitter.emit('qRCodeLogin', {
this.eventEmitter.emit(Events.qRCodeLogin, {
eventType: 'getUserCodeFailure',
data: res.body,
});
Expand All @@ -345,15 +355,15 @@ const Authorization = WebexPlugin.extend({
*/
_startQRCodePolling(options = {}) {
if (!options.device_code) {
this.eventEmitter.emit('qRCodeLogin', {
this.eventEmitter.emit(Events.qRCodeLogin, {
eventType: 'authorizationFailure',
data: {message: 'A deviceCode is required'},
});
return;
}

if (this.pollingTimer) {
this.eventEmitter.emit('qRCodeLogin', {
this.eventEmitter.emit(Events.qRCodeLogin, {
eventType: 'authorizationFailure',
data: {message: 'There is already a polling request'},
});
Expand All @@ -365,7 +375,7 @@ const Authorization = WebexPlugin.extend({

this.pollingExpirationTimer = setTimeout(() => {
this.cancelQRCodePolling(false);
this.eventEmitter.emit('qRCodeLogin', {
this.eventEmitter.emit(Events.qRCodeLogin, {
eventType: 'authorizationFailure',
data: {message: 'Authorization timed out'},
});
Expand Down Expand Up @@ -395,7 +405,7 @@ const Authorization = WebexPlugin.extend({
// if the pollingId has changed, it means that the polling request has been canceled
if (this.currentPollingId !== this.pollingId) return;

this.eventEmitter.emit('qRCodeLogin', {
this.eventEmitter.emit(Events.qRCodeLogin, {
eventType: 'authorizationSuccess',
data: res.body,
});
Expand All @@ -415,7 +425,7 @@ const Authorization = WebexPlugin.extend({
// if the statusCode is 428 which means that the authorization request is still pending
// as the end user hasn't yet completed the user-interaction steps. So keep polling.
if (res.statusCode === 428) {
this.eventEmitter.emit('qRCodeLogin', {
this.eventEmitter.emit(Events.qRCodeLogin, {
eventType: 'authorizationPending',
data: res.body,
});
Expand All @@ -425,7 +435,7 @@ const Authorization = WebexPlugin.extend({

this.cancelQRCodePolling();

this.eventEmitter.emit('qRCodeLogin', {
this.eventEmitter.emit(Events.qRCodeLogin, {
eventType: 'authorizationFailure',
data: res.body,
});
Expand All @@ -446,7 +456,7 @@ const Authorization = WebexPlugin.extend({
*/
cancelQRCodePolling(withCancelEvent = true) {
if (this.pollingTimer && withCancelEvent) {
this.eventEmitter.emit('qRCodeLogin', {
this.eventEmitter.emit(Events.qRCodeLogin, {
eventType: 'pollingCanceled',
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ registerPlugin('authorization', Authorization, {
proxies,
});

export {default} from './authorization';
export {default, Events} from './authorization';
export {default as config} from './config';

0 comments on commit 6b0a33e

Please sign in to comment.