Skip to content

Commit

Permalink
fix: qr code for launching webex app
Browse files Browse the repository at this point in the history
  • Loading branch information
xinhyao committed Nov 28, 2024
1 parent c4ba3e5 commit 8bfab67
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,28 @@ const Authorization = WebexPlugin.extend({
});
},

/**
* Generate a QR code URL to launch the Webex app when scanning with the camera
* @instance
* @memberof AuthorizationBrowserFirstParty
* @param {String} verificationUrl
* @returns {String}
*/
_generateQRCodeVerificationUrl(verificationUrl = '') {
const baseUrl = 'https://web.webex.com/deviceAuth';
const match = verificationUrl.match(/[?&]userCode=([^&]+)/);
const userCode = match ? match[1] : null;

if (userCode) {
const {services} = this.webex.internal;
const oauthHelperUrl = services.get('oauth-helper');
const params = `usercode=${userCode}&oauthhelper=${oauthHelperUrl}`;
return `${baseUrl}?${encodeURIComponent(params)}`;
} else {
return verificationUrl;
}
},

/**
* Get an OAuth Login URL for QRCode. Generate QR code based on the returned URL.
* @instance
Expand Down Expand Up @@ -285,12 +307,13 @@ const Authorization = WebexPlugin.extend({
})
.then((res) => {
const {user_code, verification_uri, verification_uri_complete} = res.body;
const verificationUriComplete = this._generateQRCodeVerificationUrl(verification_uri_complete);
this.eventEmitter.emit('qRCodeLogin', {
eventType: 'getUserCodeSuccess',
userData: {
userCode: user_code,
verificationUri: verification_uri,
verificationUriComplete: verification_uri_complete,
verificationUriComplete,
}
});
// if device authorization success, then start to poll server to check whether the user has completed authorization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,52 @@ describe('plugin-authorization-browser-first-party', () => {
});
});

describe('#_generateQRCodeVerificationUrl()', () => {
it('should generate a QR code URL when a userCode is present', () => {
const verificationUrl = 'https://example.com/verify?userCode=123456';
const oauthHelperUrl = 'https://oauth-helper-a.wbx2.com/helperservice/v1';
const expectedUrl = 'https://web.webex.com/deviceAuth?usercode%3D123456%26oauthhelper%3Dhttps%3A%2F%2Foauth-helper-a.wbx2.com%2Fhelperservice%2Fv1';

const webex = makeWebex('http://example.com');

const oauthHelperSpy = sinon.stub(webex.internal.services, 'get').returns(oauthHelperUrl);
const result = webex.authorization._generateQRCodeVerificationUrl(verificationUrl);

assert.calledOnce(oauthHelperSpy);
assert.calledWithExactly(oauthHelperSpy, 'oauth-helper');
assert.equal(result, expectedUrl);

oauthHelperSpy.restore();
});

it('should return the original verificationUrl when userCode is not present', () => {
const verificationUrl = 'https://example.com/verify';
const webex = makeWebex('http://example.com');

const oauthHelperSpy = sinon.stub(webex.internal.services, 'get');
const result = webex.authorization._generateQRCodeVerificationUrl(verificationUrl);

assert.notCalled(oauthHelperSpy);
assert.equal(result, verificationUrl);

oauthHelperSpy.restore();
});

it('should return the original verificationUrl when it is an empty string', () => {
const verificationUrl = '';
const webex = makeWebex('http://example.com');

const oauthHelperSpy = sinon.stub(webex.internal.services, 'get');
const result = webex.authorization._generateQRCodeVerificationUrl(verificationUrl);

assert.notCalled(oauthHelperSpy);
assert.equal(result, verificationUrl);

oauthHelperSpy.restore();
});

});

describe('#initQRCodeLogin()', () => {
it('should prevent concurrent request if there is already a polling request', async () => {
const webex = makeWebex('http://example.com');
Expand Down Expand Up @@ -477,6 +523,7 @@ describe('plugin-authorization-browser-first-party', () => {
});
webex.request.onFirstCall().resolves({statusCode: 200, body: sampleData});
sinon.spy(webex.authorization, '_startQRCodePolling');
sinon.spy(webex.authorization, '_generateQRCodeVerificationUrl');
const emitSpy = sinon.spy(webex.authorization.eventEmitter, 'emit');

webex.authorization.initQRCodeLogin();
Expand All @@ -485,6 +532,7 @@ describe('plugin-authorization-browser-first-party', () => {

assert.calledTwice(webex.request);
assert.calledOnce(webex.authorization._startQRCodePolling);
assert.calledOnce(webex.authorization._generateQRCodeVerificationUrl);
assert.equal(emitSpy.getCall(0).args[1].eventType, 'getUserCodeSuccess');

const request = webex.request.getCall(0);
Expand Down

0 comments on commit 8bfab67

Please sign in to comment.