Skip to content

Commit

Permalink
Update location for redirect uri (#34)
Browse files Browse the repository at this point in the history
* update location for redirect uri

* minor update

* update version

* fix coverage

* add more tests
  • Loading branch information
ishangulhane authored Apr 10, 2020
1 parent 9f12a1e commit e2e9261
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import AppID from 'ibmcloud-appid-js';

From the CDN:
```html
<script src="https://cdn.appid.cloud.ibm.com/appid-0.3.2.min.js"></script>
<script src="https://cdn.appid.cloud.ibm.com/appid-0.4.0.min.js"></script>
```

Or for development purposes use the minified file in this repo:
Expand Down
22 changes: 11 additions & 11 deletions dist/appid.min.js

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions dist/appid.umd.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ibmcloud-appid-js",
"version": "0.3.2",
"version": "0.4.0",
"description": "IBM Cloud App ID SDK for Single Page Applications",
"keywords": [
"SPA",
Expand Down
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,15 @@ class AppID {
async signin() {
this._validateInitalize();
const endpoint = this.openIdConfigResource.getAuthorizationEndpoint();
return this.utils.performOAuthFlowAndGetTokens({origin: this.window.origin, clientId: this.clientId, endpoint});
let origin = this.window.location.origin;
if (!origin) {
origin = this.window.location.protocol + "//" + this.window.location.hostname + (this.window.location.port ? ':' + this.window.location.port : '');
}
return this.utils.performOAuthFlowAndGetTokens({
origin,
endpoint,
clientId: this.clientId
});
}

/**
Expand Down
43 changes: 39 additions & 4 deletions test/AppIdTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,24 @@ describe('AppID tests', () => {
openIdConfigResource: new OpenIdConfigurationResourceMock(),
utils: new Utils(),
requestHandler: new RequestHandlerMock(),
w: {origin: 'http://localhost:3005'},
w: {location : {origin: 'http://localhost:3005'}},
url: URL
});
await appID.init(defaultInit);
let res = await appID.signin();
assert.equal(res.accessToken, 'accessToken');
assert.equal(res.idToken, 'idToken');
assert.equal(res.accessTokenPayload, 'tokenPayload');
});

it('should return tokens with no origin ', async () => {
const appID = new AppID({
popup: new PopupControllerMock({invalidState: false, error: false, invalidOrigin: false}),
iframe: new IFrameControllerMock({invalidState: false, error: false, invalidOrigin: false}),
openIdConfigResource: new OpenIdConfigurationResourceMock(),
utils: new Utils(),
requestHandler: new RequestHandlerMock(),
w: {location : {protocol: 'http', hostname: 'localhost', port: '3005'}},
url: URL
});
await appID.init(defaultInit);
Expand All @@ -75,7 +92,25 @@ describe('AppID tests', () => {
openIdConfigResource: new OpenIdConfigurationResourceMock(),
utils: new Utils(),
requestHandler: new RequestHandlerMock(),
w: {origin: 'localhost'},
w: {location : {origin: 'localhost'}},
url: URL
});
try {
await appID.init(defaultInit);
await appID.signin();
} catch (e) {
assert.equal(e.description, constants.INVALID_STATE);
}
});

it('should return error with no origin - invalid state', async () => {
const appID = new AppID({
popup: new PopupControllerMock({invalidState: true, error: false}),
iframe: new IFrameControllerMock({invalidState: false, error: false, invalidOrigin: false}),
openIdConfigResource: new OpenIdConfigurationResourceMock(),
utils: new Utils(),
requestHandler: new RequestHandlerMock(),
w: {location : {protocol: 'http', hostname: 'localhost'}},
url: URL
});
try {
Expand All @@ -93,7 +128,7 @@ describe('AppID tests', () => {
openIdConfigResource: new OpenIdConfigurationResourceMock(),
utils: new Utils(),
requestHandler: new RequestHandlerMock(),
w: {origin: 'localhost'},
w: {location : {origin: 'localhost'}},
url: URL
});
try {
Expand Down Expand Up @@ -330,4 +365,4 @@ describe('AppID tests', () => {
}
});
})
});
});

0 comments on commit e2e9261

Please sign in to comment.