pryv 2.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.jsdoc-conf.json +29 -0
- package/.mocharc.js +13 -0
- package/LICENSE.md +27 -0
- package/README.md +723 -0
- package/package.json +57 -0
- package/scripts/setup-environment-dev.sh +28 -0
- package/scripts/upload.sh +15 -0
- package/src/Auth/AuthController.js +276 -0
- package/src/Auth/AuthStates.js +20 -0
- package/src/Auth/LoginMessages.js +29 -0
- package/src/Auth/index.js +43 -0
- package/src/Browser/CookieUtils.js +51 -0
- package/src/Browser/LoginButton.js +199 -0
- package/src/Browser/index.js +55 -0
- package/src/Connection.js +331 -0
- package/src/Pryv.js +19 -0
- package/src/Service.js +197 -0
- package/src/ServiceAssets.js +162 -0
- package/src/index-socket.io-monitor.js +4 -0
- package/src/index.html +17 -0
- package/src/index.js +3 -0
- package/src/lib/browser-getEventStreamed.js +80 -0
- package/src/lib/json-parser.js +156 -0
- package/src/utils.js +136 -0
- package/test/Browser.AuthController.test.js +97 -0
- package/test/Browser.test.js +79 -0
- package/test/Connection.test.js +455 -0
- package/test/Service.test.js +89 -0
- package/test/ServiceAssets.test.js +79 -0
- package/test/Y.png +0 -0
- package/test/browser-index.js +11 -0
- package/test/browser-tests.html +31 -0
- package/test/helpers.js +8 -0
- package/test/load-test-account.js +108 -0
- package/test/test-data.js +92 -0
- package/test/utils.test.js +68 -0
- package/web-demos/auth-with-redirection.html +72 -0
- package/web-demos/auth.html +77 -0
- package/web-demos/custom-login-button.html +158 -0
- package/web-demos/index.html +186 -0
- package/web-demos/service-info.json +13 -0
- package/web-demos/stream-examples.html +80 -0
- package/webpack.config.js +71 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
const testData = require('./test-data.js');
|
|
2
|
+
const Pryv = require('../src');
|
|
3
|
+
|
|
4
|
+
const conn = new Pryv.Connection(testData.apiEndpointWithToken);
|
|
5
|
+
|
|
6
|
+
const TEST_DATA_VERSION = "v1";
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
// -- check if account is already loaded
|
|
10
|
+
const res = conn.api([{
|
|
11
|
+
"method": "events.get",
|
|
12
|
+
"params": {
|
|
13
|
+
"streams": ["data"],
|
|
14
|
+
"types": ["test/version"],
|
|
15
|
+
"limit": 1
|
|
16
|
+
}
|
|
17
|
+
}]).then((res, err) => {
|
|
18
|
+
if (res[0].events[0] && res[0].events[0].content === TEST_DATA_VERSION) {
|
|
19
|
+
console.log('TEST ACCOUNT IS UP TO DATE');
|
|
20
|
+
} else {
|
|
21
|
+
doLoad();
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
function doLoad() {
|
|
26
|
+
let query = [
|
|
27
|
+
{ // trash eventual stream data
|
|
28
|
+
"method": "streams.delete",
|
|
29
|
+
"params": {
|
|
30
|
+
"id": "data",
|
|
31
|
+
"mergeEventsWithParent": false
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
{ // clear from trash eventual stream data
|
|
35
|
+
"method": "streams.delete",
|
|
36
|
+
"params": {
|
|
37
|
+
"id": "data",
|
|
38
|
+
"mergeEventsWithParent": false
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
"method": "streams.create",
|
|
43
|
+
"params": {
|
|
44
|
+
"id": "data",
|
|
45
|
+
"name": "Test Data",
|
|
46
|
+
"clientData": {
|
|
47
|
+
"pryv-browser:charts": {
|
|
48
|
+
"mass/kg": {
|
|
49
|
+
"settings": {
|
|
50
|
+
"color": "#1abc9c",
|
|
51
|
+
"style": "line",
|
|
52
|
+
"transform": "none",
|
|
53
|
+
"interval": "auto"
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
"method": "events.create",
|
|
62
|
+
"params": {
|
|
63
|
+
"streamId": "data",
|
|
64
|
+
"type": "test/version",
|
|
65
|
+
"content": TEST_DATA_VERSION
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
];
|
|
69
|
+
|
|
70
|
+
const now = (new Date()).getTime() / 1000;
|
|
71
|
+
const steps = 60 * 60 ; // 1 hours
|
|
72
|
+
for (let i = 0; i < 10000; i++) {
|
|
73
|
+
query.push(
|
|
74
|
+
{
|
|
75
|
+
"method": "events.create",
|
|
76
|
+
"params": {
|
|
77
|
+
"time": now - i * steps,
|
|
78
|
+
"streamId": "data",
|
|
79
|
+
"type": "mass/kg",
|
|
80
|
+
"content": Math.sin(i / 180)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const res = conn.api(query).then((res, err) => {
|
|
87
|
+
trashFirst200DeleteFirst100(res).then((res2, err2) => {
|
|
88
|
+
console.log(res2);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async function trashFirst200DeleteFirst100(res) {
|
|
94
|
+
const query = [];
|
|
95
|
+
for (let i = 5; i < 205; i++) {
|
|
96
|
+
const k = (i < 105) ? 2 : 1;
|
|
97
|
+
for (j = 0; j < k; j++) { // do twice for the firsts 100
|
|
98
|
+
console.log(res[i]);
|
|
99
|
+
query.push({
|
|
100
|
+
"method": "events.delete",
|
|
101
|
+
"params": {
|
|
102
|
+
"id": res[i].event.id
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return await conn.api(query);
|
|
108
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
const superagent = require('superagent');
|
|
2
|
+
|
|
3
|
+
const username = 'jslibtest5';
|
|
4
|
+
const serviceInfoUrl = 'https://reg.pryv.me/service/info';
|
|
5
|
+
//const serviceInfoUrl = 'https://l.rec.la:4443/reg/service/info';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Data used for tests
|
|
9
|
+
*/
|
|
10
|
+
const testData = {
|
|
11
|
+
username: username,
|
|
12
|
+
password: username,
|
|
13
|
+
serviceInfoUrl: serviceInfoUrl,
|
|
14
|
+
token: null,
|
|
15
|
+
serviceInfo: null,
|
|
16
|
+
apiEndpoint: null,
|
|
17
|
+
apiEndpointWithToken: null
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
async function prepare() {
|
|
22
|
+
if (testData.token != null) return testData;
|
|
23
|
+
console.log('Preparing test Data..');
|
|
24
|
+
// fetch serviceInfo
|
|
25
|
+
|
|
26
|
+
const serviceInfo = (await superagent.get(serviceInfoUrl)).body;
|
|
27
|
+
if (serviceInfo.api == null) throw 'Invalid service Info ' + JSON.stringify(serviceInfo);
|
|
28
|
+
// test if user exists
|
|
29
|
+
const userExists = (await superagent.get(serviceInfo.register + username + '/check_username')).body;
|
|
30
|
+
if (typeof userExists.reserved === 'undefined') throw 'Invalid user exists ' + JSON.stringify(userExists);
|
|
31
|
+
|
|
32
|
+
let hostingCandidate = null;
|
|
33
|
+
if (! userExists.reserved) { // create user
|
|
34
|
+
// get available hosting
|
|
35
|
+
const hostings = (await superagent.get(serviceInfo.register + 'hostings').set('accept', 'json')).body;
|
|
36
|
+
findOneHostingKey(hostings, 'N');
|
|
37
|
+
function findOneHostingKey(o, parentKey) {
|
|
38
|
+
for (const key of Object.keys(o)) {
|
|
39
|
+
if (parentKey === 'hostings') {
|
|
40
|
+
hostingCandidate = key;
|
|
41
|
+
return key;
|
|
42
|
+
}
|
|
43
|
+
if (typeof o[key] !== 'string')
|
|
44
|
+
findOneHostingKey(o[key], key);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
if (hostingCandidate == null) throw 'Cannot find hosting in: ' + JSON.stringify(hostings);
|
|
48
|
+
const hosting = hostingCandidate;
|
|
49
|
+
|
|
50
|
+
// create user
|
|
51
|
+
await superagent.post(serviceInfo.register + 'user')
|
|
52
|
+
.send({
|
|
53
|
+
appid: 'js-lib-test',
|
|
54
|
+
hosting: hosting,
|
|
55
|
+
username: username,
|
|
56
|
+
password: username,
|
|
57
|
+
email: username + '@pryv.io',
|
|
58
|
+
invitationtoken: 'enjoy',
|
|
59
|
+
languageCode: 'en',
|
|
60
|
+
referer: 'test-suite'
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
const apiEndpoint = serviceInfo.api.replace('{username}', username);
|
|
64
|
+
// login user
|
|
65
|
+
const loginRes = await superagent.post(apiEndpoint + 'auth/login')
|
|
66
|
+
.set('Origin', 'https://l.rec.la')
|
|
67
|
+
.send({ username: username, password: username, appId: 'js-lib-test' });
|
|
68
|
+
|
|
69
|
+
// create data stream
|
|
70
|
+
try {
|
|
71
|
+
const streamRes = await superagent.post(apiEndpoint + 'streams').set('authorization', loginRes.body.token).send(
|
|
72
|
+
{
|
|
73
|
+
id: 'data',
|
|
74
|
+
name: 'Data'
|
|
75
|
+
}
|
|
76
|
+
)
|
|
77
|
+
} catch (e) {
|
|
78
|
+
}
|
|
79
|
+
if ((loginRes.body == null) || (loginRes.body.token == null)) throw 'Failed login process during testData prepare' + loginRes.text;
|
|
80
|
+
testData.serviceInfo = serviceInfo;
|
|
81
|
+
testData.token = loginRes.body.token;
|
|
82
|
+
|
|
83
|
+
const regexAPIandToken = /(.+):\/\/(.+)/gm;
|
|
84
|
+
const res = regexAPIandToken.exec(apiEndpoint);
|
|
85
|
+
testData.apiEndpointWithToken = res[1] + '://' + testData.token + '@' + res[2];
|
|
86
|
+
testData.apiEndpoint = apiEndpoint;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
testData.prepare = prepare;
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
module.exports = testData;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
|
|
2
|
+
const should = chai.should();
|
|
3
|
+
const expect = chai.expect;
|
|
4
|
+
|
|
5
|
+
const testData = require('./test-data.js');
|
|
6
|
+
|
|
7
|
+
describe('utils', function () {
|
|
8
|
+
|
|
9
|
+
before(async function () {
|
|
10
|
+
this.timeout(5000);
|
|
11
|
+
await testData.prepare();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('extractTokenAndApiEndpoint', function (done) {
|
|
15
|
+
const tokenAndAPI = Pryv.utils
|
|
16
|
+
.extractTokenAndApiEndpoint(testData.apiEndpointWithToken);
|
|
17
|
+
testData.token.should.equals(tokenAndAPI.token);
|
|
18
|
+
|
|
19
|
+
(testData.apiEndpoint).should.equals(tokenAndAPI.endpoint);
|
|
20
|
+
done();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('extractTokenAndApiEndpoint should work without token', function (done) {
|
|
24
|
+
const tokenAndAPI = Pryv.utils
|
|
25
|
+
.extractTokenAndApiEndpoint(testData.apiEndpoint);
|
|
26
|
+
|
|
27
|
+
should.not.exist(tokenAndAPI.token);
|
|
28
|
+
|
|
29
|
+
(testData.apiEndpoint).should.equals(tokenAndAPI.endpoint);
|
|
30
|
+
done();
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('extractTokenAndApiEndpoint should fail on invalid url', function (done) {
|
|
34
|
+
let error = null;
|
|
35
|
+
try {
|
|
36
|
+
const tokenAndAPI = Pryv.utils
|
|
37
|
+
.extractTokenAndApiEndpoint('blip');
|
|
38
|
+
} catch (e) {
|
|
39
|
+
error = e;
|
|
40
|
+
|
|
41
|
+
return done();
|
|
42
|
+
}
|
|
43
|
+
should.exist(error);
|
|
44
|
+
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('buildAPIEndpoint with token', function (done) {
|
|
48
|
+
const apiEndpoint = Pryv.utils
|
|
49
|
+
.buildPryvApiEndpoint({
|
|
50
|
+
token: testData.token,
|
|
51
|
+
endpoint: testData.apiEndpoint});
|
|
52
|
+
apiEndpoint.should.equals(testData.apiEndpointWithToken);
|
|
53
|
+
done();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('buildAPIEndpoint without token', function (done) {
|
|
57
|
+
const apiEndpoint = Pryv.utils
|
|
58
|
+
.buildPryvApiEndpoint({
|
|
59
|
+
token: null,
|
|
60
|
+
endpoint: testData.apiEndpoint
|
|
61
|
+
});
|
|
62
|
+
apiEndpoint.should.equals(testData.apiEndpoint);
|
|
63
|
+
done();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html>
|
|
3
|
+
|
|
4
|
+
<head>
|
|
5
|
+
<title>Auth with redirection - Pryv JS lib</title>
|
|
6
|
+
<script src="../pryv.js"></script>
|
|
7
|
+
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
|
|
8
|
+
<meta content="utf-8" http-equiv="encoding">
|
|
9
|
+
</head>
|
|
10
|
+
|
|
11
|
+
<body>
|
|
12
|
+
<span id="pryv-button"></span>
|
|
13
|
+
<script>
|
|
14
|
+
let authSettings = {
|
|
15
|
+
spanButtonID: 'pryv-button', // span id the DOM that will be replaced by the Service specific button
|
|
16
|
+
onStateChange: function pryvAuthStateChange(state) { // called each time the authentication state changed
|
|
17
|
+
switch(state.id) {
|
|
18
|
+
case Pryv.Auth.AuthStates.LOADING:
|
|
19
|
+
console.log('Loading service information...');
|
|
20
|
+
break;
|
|
21
|
+
case Pryv.Auth.AuthStates.INITIALIZED:
|
|
22
|
+
console.log('Service information is retrieved so authorization can start. You can display login / registration screen or redirect to the our hosted app - web - auth application.');
|
|
23
|
+
break;
|
|
24
|
+
case Pryv.Auth.AuthStates.AUTHORIZED:
|
|
25
|
+
console.log('User is authorized and can access his personal data');
|
|
26
|
+
break;
|
|
27
|
+
case Pryv.Auth.AuthStates.SIGNOUT:
|
|
28
|
+
console.log('User just logged off, please delete all the session related data');
|
|
29
|
+
break;
|
|
30
|
+
case Pryv.Auth.AuthStates.ERROR:
|
|
31
|
+
console.log('Error:', state?.message);
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
}, // event Listener for Authentication steps
|
|
35
|
+
authRequest: { // See: https://api.pryv.com/reference/#auth-request
|
|
36
|
+
requestingAppId: 'lib-js-test',
|
|
37
|
+
languageCode: 'fr', // optional (default english)
|
|
38
|
+
returnURL: 'auto#',
|
|
39
|
+
requestedPermissions: [{
|
|
40
|
+
streamId: 'test',
|
|
41
|
+
defaultName: 'Test',
|
|
42
|
+
level: 'read',
|
|
43
|
+
}],
|
|
44
|
+
// the login page where user will be redirected
|
|
45
|
+
returnURL: 'https://l.rec.la:9443/demos/auth-with-redirection.html#'
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const serviceInfoUrl = null; // 'https://api.pryv.com/lib-js/demos/service-info.json';
|
|
50
|
+
const serviceInfoJson = {
|
|
51
|
+
"register": "https://reg.pryv.me",
|
|
52
|
+
"access": "https://access.pryv.me/access",
|
|
53
|
+
"api": "https://{username}.pryv.me/",
|
|
54
|
+
"name": "Pryv Lab",
|
|
55
|
+
"home": "https://www.pryv.com",
|
|
56
|
+
"support": "https://pryv.com/helpdesk",
|
|
57
|
+
"terms": "https://pryv.com/pryv-lab-terms-of-use/",
|
|
58
|
+
"eventTypes": "https://api.pryv.com/event-types/flat.json",
|
|
59
|
+
"assets": {
|
|
60
|
+
"definitions": "https://pryv.github.io/assets-pryv.me/index.json"
|
|
61
|
+
}};
|
|
62
|
+
(async function () {
|
|
63
|
+
let service = await Pryv.Auth.setupAuth(
|
|
64
|
+
authSettings,
|
|
65
|
+
serviceInfoUrl,
|
|
66
|
+
serviceInfoJson
|
|
67
|
+
);
|
|
68
|
+
})();
|
|
69
|
+
</script>
|
|
70
|
+
</body>
|
|
71
|
+
|
|
72
|
+
</html>
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html>
|
|
3
|
+
|
|
4
|
+
<head>
|
|
5
|
+
<title>Auth - Pryv JS lib</title>
|
|
6
|
+
<script src="../pryv.js"></script>
|
|
7
|
+
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
|
|
8
|
+
<meta content="utf-8" http-equiv="encoding">
|
|
9
|
+
</head>
|
|
10
|
+
|
|
11
|
+
<body>
|
|
12
|
+
<span id="pryv-button"></span>
|
|
13
|
+
<script>
|
|
14
|
+
const authSettings = {
|
|
15
|
+
spanButtonID: 'pryv-button', // span id the DOM that will be replaced by the Service specific button
|
|
16
|
+
onStateChange: function pryvAuthStateChange(state) { // called each time the authentication state changed
|
|
17
|
+
switch(state.id) {
|
|
18
|
+
case Pryv.Auth.AuthStates.LOADING:
|
|
19
|
+
console.log('Loading service information...');
|
|
20
|
+
break;
|
|
21
|
+
case Pryv.Auth.AuthStates.INITIALIZED:
|
|
22
|
+
console.log('Service information is retrieved so authorization can start. You can display login / registration screen or redirect to the our hosted app - web - auth application.');
|
|
23
|
+
break;
|
|
24
|
+
case Pryv.Auth.AuthStates.AUTHORIZED:
|
|
25
|
+
console.log('User is authorized and can access his personal data');
|
|
26
|
+
break;
|
|
27
|
+
case Pryv.Auth.AuthStates.SIGNOUT:
|
|
28
|
+
console.log('User just logged off, please delete all the session related data');
|
|
29
|
+
break;
|
|
30
|
+
case Pryv.Auth.AuthStates.ERROR:
|
|
31
|
+
console.log('Error:', state?.message);
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
}, // event Listener for Authentication steps
|
|
35
|
+
authRequest: { // See: https://api.pryv.com/reference/#auth-request
|
|
36
|
+
requestingAppId: 'lib-js-test',
|
|
37
|
+
requestedPermissions: [
|
|
38
|
+
{
|
|
39
|
+
streamId: 'test',
|
|
40
|
+
defaultName: 'test',
|
|
41
|
+
level: 'manage'
|
|
42
|
+
}
|
|
43
|
+
],
|
|
44
|
+
clientData: {
|
|
45
|
+
'app-web-auth:description': {
|
|
46
|
+
'type': 'note/txt',
|
|
47
|
+
'content': 'This is a consent message.'
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
// referer: 'my test with lib-js', // optional string to track registration source
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const serviceInfoUrl = null;// 'https://api.pryv.com/lib-js/demos/service-info.json';
|
|
55
|
+
const serviceInfoJson = {
|
|
56
|
+
"register": "https://reg.pryv.me",
|
|
57
|
+
"access": "https://access.pryv.me/access",
|
|
58
|
+
"api": "https://{username}.pryv.me/",
|
|
59
|
+
"name": "Pryv Lab",
|
|
60
|
+
"home": "https://www.pryv.com",
|
|
61
|
+
"support": "https://pryv.com/helpdesk",
|
|
62
|
+
"terms": "https://pryv.com/pryv-lab-terms-of-use/",
|
|
63
|
+
"eventTypes": "https://api.pryv.com/event-types/flat.json",
|
|
64
|
+
"assets": {
|
|
65
|
+
"definitions": "https://pryv.github.io/assets-pryv.me/index.json"
|
|
66
|
+
}};
|
|
67
|
+
(async function () {
|
|
68
|
+
service = await Pryv.Auth.setupAuth(
|
|
69
|
+
authSettings,
|
|
70
|
+
serviceInfoUrl,
|
|
71
|
+
serviceInfoJson
|
|
72
|
+
);
|
|
73
|
+
})();
|
|
74
|
+
</script>
|
|
75
|
+
</body>
|
|
76
|
+
|
|
77
|
+
</html>
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html>
|
|
3
|
+
|
|
4
|
+
<head>
|
|
5
|
+
<title>Custom Login button - Pryv JS lib</title>
|
|
6
|
+
<script src="../pryv.js"></script>
|
|
7
|
+
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
|
|
8
|
+
<meta content="utf-8" http-equiv="encoding">
|
|
9
|
+
</head>
|
|
10
|
+
|
|
11
|
+
<body>
|
|
12
|
+
<button type="button" id="pryv-button">My custom button</button>
|
|
13
|
+
<script>
|
|
14
|
+
const authSettings = {
|
|
15
|
+
spanButtonID: 'pryv-button', // span id the DOM that will be replaced by the Service specific button
|
|
16
|
+
authRequest: { // See: https://api.pryv.com/reference/#auth-request
|
|
17
|
+
requestingAppId: 'custom-login-button-example',
|
|
18
|
+
requestedPermissions: [
|
|
19
|
+
{
|
|
20
|
+
streamId: 'test',
|
|
21
|
+
defaultName: 'test',
|
|
22
|
+
level: 'manage'
|
|
23
|
+
}
|
|
24
|
+
]
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
class MyLoginButton {
|
|
29
|
+
|
|
30
|
+
constructor(authSettings, service) {
|
|
31
|
+
this.authSettings = authSettings;
|
|
32
|
+
this.service = service;
|
|
33
|
+
this.serviceInfo = service.infoSync();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async init() {
|
|
37
|
+
const loginButtonSpan = document.getElementById(this.authSettings.spanButtonID);
|
|
38
|
+
loginButtonSpan.addEventListener('click', this.onClick.bind(this));
|
|
39
|
+
this.loginButtonSpan = loginButtonSpan;
|
|
40
|
+
|
|
41
|
+
this._cookieKey = 'pryv-libjs-' + this.authSettings.authRequest.requestingAppId;
|
|
42
|
+
|
|
43
|
+
this.auth = new Pryv.Auth.AuthController(this.authSettings, this.service, this);
|
|
44
|
+
await this.auth.init();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
onClick() {
|
|
48
|
+
this.auth.handleClick();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async onStateChange (state) {
|
|
52
|
+
switch (state.status) {
|
|
53
|
+
case Pryv.Auth.AuthStates.LOADING:
|
|
54
|
+
this.text = 'Loading visual assets';
|
|
55
|
+
break;
|
|
56
|
+
case Pryv.Auth.AuthStates.INITIALIZED:
|
|
57
|
+
this.text = 'Sign into: ' + this.serviceInfo.name;
|
|
58
|
+
break;
|
|
59
|
+
case Pryv.Auth.AuthStates.NEED_SIGNIN:
|
|
60
|
+
const loginUrl = state.authUrl || state.url; // .url is deprecated
|
|
61
|
+
if (this.authSettings.authRequest.returnURL) { // open on same page (no Popup)
|
|
62
|
+
location.href = loginUrl;
|
|
63
|
+
return;
|
|
64
|
+
} else {
|
|
65
|
+
startLoginScreen(this, loginUrl);
|
|
66
|
+
}
|
|
67
|
+
break;
|
|
68
|
+
case Pryv.Auth.AuthStates.AUTHORIZED:
|
|
69
|
+
this.text = 'Signed in as ' + state.username;
|
|
70
|
+
this.saveAuthorizationData({
|
|
71
|
+
apiEndpoint: state.apiEndpoint,
|
|
72
|
+
username: state.username
|
|
73
|
+
});
|
|
74
|
+
break;
|
|
75
|
+
case Pryv.Auth.AuthStates.SIGNOUT:
|
|
76
|
+
const message = 'Do you wish to sign out?';
|
|
77
|
+
if (confirm(message)) {
|
|
78
|
+
this.deleteAuthorizationData();
|
|
79
|
+
this.auth.init();
|
|
80
|
+
}
|
|
81
|
+
break;
|
|
82
|
+
case Pryv.Auth.AuthStates.ERROR:
|
|
83
|
+
this.text = getErrorMessage(this, state.message);
|
|
84
|
+
break;
|
|
85
|
+
default:
|
|
86
|
+
console.log('WARNING Unhandled state for Login: ' + state.status);
|
|
87
|
+
}
|
|
88
|
+
if (this.loginButtonSpan) {
|
|
89
|
+
this.loginButtonSpan.innerHTML = this.text;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
saveAuthorizationData(authData) {
|
|
94
|
+
console.log('You should save this object to the storage', authData);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
getAuthorizationData() {
|
|
98
|
+
return Pryv.Browser.CookieUtils.get(this._cookieKey);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async deleteAuthorizationData() {
|
|
102
|
+
console.log('You should delete saved data from the storage');
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function startLoginScreen(loginButton, authUrl) {
|
|
107
|
+
console.log('My custom popup that is huge:');
|
|
108
|
+
let screenX = typeof window.screenX !== 'undefined' ? window.screenX : window.screenLeft,
|
|
109
|
+
screenY = typeof window.screenY !== 'undefined' ? window.screenY : window.screenTop,
|
|
110
|
+
outerWidth = typeof window.outerWidth !== 'undefined' ?
|
|
111
|
+
window.outerWidth : document.body.clientWidth,
|
|
112
|
+
outerHeight = typeof window.outerHeight !== 'undefined' ?
|
|
113
|
+
window.outerHeight : (document.body.clientHeight - 22),
|
|
114
|
+
width = 900,
|
|
115
|
+
height = 500,
|
|
116
|
+
left = parseInt(screenX + ((outerWidth - width) / 2), 10),
|
|
117
|
+
top = parseInt(screenY + ((outerHeight - height) / 2.5), 10),
|
|
118
|
+
features = (
|
|
119
|
+
'width=' + width +
|
|
120
|
+
',height=' + height +
|
|
121
|
+
',left=' + left +
|
|
122
|
+
',top=' + top +
|
|
123
|
+
',scrollbars=yes'
|
|
124
|
+
);
|
|
125
|
+
loginButton.popup = window.open(authUrl, 'Your custom Sign-in pop-up', features);
|
|
126
|
+
|
|
127
|
+
if (!loginButton.popup) {
|
|
128
|
+
console.log('Pop-up blocked. A second click should allow it.');
|
|
129
|
+
} else if (window.focus) {
|
|
130
|
+
loginButton.popup.focus();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const serviceInfoUrl = null; // 'https://api.pryv.com/lib-js/demos/service-info.json';
|
|
135
|
+
const serviceInfoJson = {
|
|
136
|
+
"register": "https://reg.pryv.me",
|
|
137
|
+
"access": "https://access.pryv.me/access",
|
|
138
|
+
"api": "https://{username}.pryv.me/",
|
|
139
|
+
"name": "Pryv Lab",
|
|
140
|
+
"home": "https://www.pryv.com",
|
|
141
|
+
"support": "https://pryv.com/helpdesk",
|
|
142
|
+
"terms": "https://pryv.com/pryv-lab-terms-of-use/",
|
|
143
|
+
"eventTypes": "https://api.pryv.com/event-types/flat.json",
|
|
144
|
+
"assets": {
|
|
145
|
+
"definitions": "https://pryv.github.io/assets-pryv.me/index.json"
|
|
146
|
+
}};
|
|
147
|
+
(async function () {
|
|
148
|
+
let service = await Pryv.Auth.setupAuth(
|
|
149
|
+
authSettings,
|
|
150
|
+
serviceInfoUrl,
|
|
151
|
+
serviceInfoJson,
|
|
152
|
+
MyLoginButton,
|
|
153
|
+
);
|
|
154
|
+
})();
|
|
155
|
+
</script>
|
|
156
|
+
</body>
|
|
157
|
+
|
|
158
|
+
</html>
|