Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ npm install @smartcar/auth
### Smartcar CDN

```html
<script src="https://javascript-sdk.smartcar.com/2.9.0/sdk.js"></script>
<script src="https://javascript-sdk.smartcar.com/2.10.0/sdk.js"></script>
```

## SDK reference
Expand Down Expand Up @@ -178,4 +178,4 @@ https://application-backend.com/page?error=access_denied&error_description=User+
[tag-image]: https://img.shields.io/github/tag/smartcar/javascript-sdk.svg

<!-- Please do not modify or remove this, it is used by the build process -->
[version]: 2.9.0
[version]: 2.10.0
1 change: 1 addition & 0 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ Invalid subscription error returned by Connect.
| error | <code>Error</code> | something went wrong in Connect; this normally indicates that the user denied access to your application or does not have a connected vehicle |
| code | <code>String</code> | the authorization code to be exchanged from a backend sever for an access token |
| [state] | <code>Object</code> | contains state if it was set on the initial authorization request |
| [virtualKeyUrl] | <code>String</code> | virtual key URL used by Tesla to register Smartcar's virtual key on a vehicle. This registration will be required in order to use any commands on a Tesla vehicle. It is an optional argument as it is only included in specific cases. |

<a name="WindowOptions"></a>

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@smartcar/auth",
"version": "2.9.0",
"version": "2.10.0",
"description": "javascript auth sdk for the smartcar",
"main": "dist/npm/sdk.js",
"license": "MIT",
Expand Down
1 change: 1 addition & 0 deletions src/redirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
make: params.get('make'),
model: params.get('model'),
year: params.get('year'),
virtualKeyUrl: params.get('virtual_key_url'),
};

// if no `app_origin` given, post to same origin as redirect page (assuming
Expand Down
8 changes: 7 additions & 1 deletion src/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class Smartcar {
* backend sever for an access token
* @param {Object} [state] - contains state if it was set on the initial
* authorization request
* @param {String} [virtualKeyUrl] - virtual key URL used by Tesla to register
* Smartcar's virtual key on a vehicle. This registration will be required in order to use
* any commands on a Tesla vehicle. It is an optional argument as it is only included in
* specific cases.
*/

/**
Expand Down Expand Up @@ -138,6 +142,8 @@ class Smartcar {

const err = generateError(message.error, message.errorDescription);

const virtualKeyUrl = message.virtualKeyUrl;

/**
* Call `onComplete` with parameters even if developer is not using
* a Smartcar-hosted redirect. Regardless of if they are using a
Expand All @@ -150,7 +156,7 @@ class Smartcar {
* parameters they must also handle populating the corresponding query
* parameters in their redirect uri.
*/
this.onComplete(err, message.code, originalState);
this.onComplete(err, message.code, originalState, virtualKeyUrl);
}
};

Expand Down
5 changes: 5 additions & 0 deletions test/unit/redirect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ describe('redirect', () => {
model: null,
vin: null,
year: null,
virtualKeyUrl: null,
},
selfHostedOrigin,
);
Expand Down Expand Up @@ -125,6 +126,7 @@ describe('redirect', () => {
model: null,
vin: null,
year: null,
virtualKeyUrl: null,
},
appOrigin,
);
Expand All @@ -142,6 +144,7 @@ describe('redirect', () => {
const make = 'BMW';
const model = 'M3';
const year = '2013';
const virtualKeyUrl = 'https://www.tesla.com/_ak/smartcar.com';

const params = new URLSearchParams();
params.set('code', code);
Expand All @@ -152,6 +155,7 @@ describe('redirect', () => {
params.set('make', make);
params.set('model', model);
params.set('year', year);
params.set('virtual_key_url', virtualKeyUrl);

const cdnOrigin = `${CDN_ORIGIN}/redirect?app_origin=${appOrigin}&${params.toString()}`;

Expand All @@ -173,6 +177,7 @@ describe('redirect', () => {
model,
vin,
year,
virtualKeyUrl,
},
appOrigin,
);
Expand Down
43 changes: 40 additions & 3 deletions test/unit/sdk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,42 @@ describe('sdk', () => {

smartcar.messageHandler(event);

expect(smartcar.onComplete).toBeCalledWith(null, expect.anything(), expect.anything());
expect(smartcar.onComplete).toBeCalledWith(
null,
expect.anything(),
expect.anything(),
undefined,
);
});

test('fires onComplete with virtualKeyUrl when included', () => {
const options = {
clientId: 'clientId',
redirectUri: `${CDN_ORIGIN}?app_origin=https://app.com`,
scope: ['read_vehicle_info', 'read_odometer'],
// eslint-disable-next-line no-unused-vars, no-empty-function
onComplete: jest.fn((__, _) => {}),
};

const smartcar = new Smartcar(options);

const event = {
data: {
name: 'SmartcarAuthMessage',
isSmartcarHosted: true,
code: 'super-secret-code',
errorDescription: 'this doesnt matter',
state: getEncodedState(smartcar.instanceId, 'some-state'),
virtualKeyUrl: 'https://www.tesla.com/_ak/smartcar.com',
},
origin: CDN_ORIGIN,
};

smartcar.messageHandler(event);

expect(smartcar.onComplete).toBeCalledWith(null, 'super-secret-code', 'some-state', 'https://www.tesla.com/_ak/smartcar.com');
});

test('fires onComplete w/o error when error: null in postMessage', () => {
const options = {
clientId: 'clientId',
Expand All @@ -387,7 +420,7 @@ describe('sdk', () => {

smartcar.messageHandler(event);

expect(smartcar.onComplete).toBeCalledWith(null, 'super-secret-code', 'some-state');
expect(smartcar.onComplete).toBeCalledWith(null, 'super-secret-code', 'some-state', undefined);
});

test('fires onComplete w/o error when error key not in postMessage', () => {
Expand All @@ -414,7 +447,7 @@ describe('sdk', () => {

smartcar.messageHandler(event);

expect(smartcar.onComplete).toBeCalledWith(null, 'super-secret-code', 'some-state');
expect(smartcar.onComplete).toBeCalledWith(null, 'super-secret-code', 'some-state', undefined);
});

test(// eslint-disable-next-line max-len
Expand Down Expand Up @@ -456,6 +489,7 @@ describe('sdk', () => {
new Smartcar.VehicleIncompatible(errorDescription, vehicleInfo),
'super-secret-code',
'some-state',
undefined,
);
});

Expand Down Expand Up @@ -601,6 +635,7 @@ describe('sdk', () => {
new Smartcar.AccessDenied(errorDescription),
'super-secret-code',
'some-state',
undefined,
);
});

Expand Down Expand Up @@ -635,6 +670,7 @@ describe('sdk', () => {
new Smartcar.InvalidSubscription(errorDescription),
'super-secret-code',
'some-state',
undefined,
);
});

Expand Down Expand Up @@ -670,6 +706,7 @@ describe('sdk', () => {
Error(`Unexpected error: ${error} - ${errorDescription}`),
'super-secret-code',
'some-state',
undefined,
);
});
});
Expand Down