-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
108 lines (88 loc) · 3.29 KB
/
auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"use strict";
/** Makes requests */
var caller = require('./caller');
/**
* Authentication Object
* @param obj
* Ex: {
* client_id: 'XXXXKEY',
* client_secret: 'XXXXKEY_SECRET',
* scope: 'lifelog.profile.read+lifelog.activities.read+lifelog.locations.read',
* }
* @returns {{redirectURL: Function, getAccessToken: Function, refreshToken: Function}}
*/
var Auth = function (obj){
/** Check for required parameters */
if(!obj.client_id
|| !obj.scope
|| !obj.client_secret){
throw "At least one parameter: client_id, scope, client_secret is null";
}
/** Client ID **/
var client_id = obj.client_id;
/** Client Secret **/
var client_secret = obj.client_secret;
/** Scope **/
var scope = obj.scope;
return {
/**
* Get Redirect URL
* @param code Unique Identifier used for callbacks
*/
redirectURL: function(state){
if(state){
return 'https://platform.lifelog.sonymobile.com/oauth/2/authorize?client_id='+client_id+'&scope='+scope+'&state='+state;
}
return 'https://platform.lifelog.sonymobile.com/oauth/2/authorize?client_id='+client_id+'&scope='+scope;
},
/**
* Get Access Token from Code Retrieved in the Callback
* @param mixed String:code or Object:Object returned from the callback
* Ex:
* 'ma7cbM9d'
* or
* {
* "scope": "lifelog.profile.read lifelog.activities.read lifelog.locations.read",
* "state": "null",
* "code": "ma7cbM9d"
* }
*
* @returns obj Authorization Code Response
*/
getAccessToken: function(obj){
var code;
if(typeof obj === 'string'){ // assume it's a code
code = obj;
} else if(typeof obj === 'object'){
code = obj.code;
} else{
throw "Null parameter in Auth.getAccessToken(). It must be a code or a returned object from Lifelog callback";
}
if(!code) throw "'The authorization code you received in response to your authenticate request' is null";
var body =
'client_id='+client_id+
'&client_secret='+client_secret+
'&grant_type=authorization_code' +
'&code='+code;
return caller.makePostRequest({
headers: {'content-type' : 'application/x-www-form-urlencoded'},
url: 'https://platform.lifelog.sonymobile.com/oauth/2/token',
body: body
}, true);
},
refreshToken: function(refreshToken){
if(!refreshToken) throw "refreshToken parameter is undefined";
var body =
'client_id='+client_id+
'&client_secret='+client_secret+
'&grant_type=refresh_token' +
'&refresh_token='+refreshToken;
return caller.makePostRequest({
headers: {'content-type' : 'application/x-www-form-urlencoded'},
url: 'https://platform.lifelog.sonymobile.com/oauth/2/refresh_token',
body: body
}, true);
}
}
};
module.exports = Auth;