Skip to content

Commit

Permalink
fix(automotive): qr code for launching webex app (#4012)
Browse files Browse the repository at this point in the history
  • Loading branch information
xinhyao authored Nov 30, 2024
1 parent 99c21e8 commit daefe97
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,30 @@ 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 urlParams = new URLSearchParams(new URL(verificationUrl).search);
const userCode = urlParams.get('userCode');

if (userCode) {
const {services} = this.webex.internal;
const oauthHelperUrl = services.get('oauth-helper');
const newVerificationUrl = new URL(baseUrl);
newVerificationUrl.searchParams.set('usercode', userCode);
newVerificationUrl.searchParams.set('oauthhelper', oauthHelperUrl);
return newVerificationUrl.toString();
} else {
return verificationUrl;
}
},

/**
* Get an OAuth Login URL for QRCode. Generate QR code based on the returned URL.
* @instance
Expand Down Expand Up @@ -327,12 +351,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(Events.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,38 @@ 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=123456&oauthhelper=https%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();
});
});

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 +509,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 +518,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 daefe97

Please sign in to comment.