1 | var OAuth2 = require('oauth').OAuth2,
|
2 | querystring = require('querystring'),
|
3 | util = require('util'),
|
4 | task = require('./base');
|
5 |
|
6 |
|
7 |
|
8 | var vkontakteConfig = project.config.consumerConfig.vkontakte;
|
9 | var vkontakteScopes = (vkontakteConfig ? vkontakteConfig.scopes : null);
|
10 |
|
11 | if (!vkontakteScopes) {
|
12 |
|
13 | util.extend (vkontakteConfig, {
|
14 | "scopes": {
|
15 | "notify": "notify",
|
16 | "contacts": "friends",
|
17 | "photos": "photos",
|
18 | "audio": "audio",
|
19 | "video": "video",
|
20 | "docs": "docs",
|
21 | "notes": "notes",
|
22 | "pages": "pages",
|
23 | "offers": "offers",
|
24 | "questions": "questions",
|
25 | "wall": "wall",
|
26 | "groups": "groups",
|
27 | "messages": "messages",
|
28 | "notifications": "notifications",
|
29 | "ads": "ads",
|
30 | "offline": "offline",
|
31 | "nohttps": "nohttps"
|
32 | }
|
33 | });
|
34 |
|
35 | vkontakteScopes = vkontakteConfig.scopes;
|
36 |
|
37 |
|
38 | }
|
39 |
|
40 |
|
41 |
|
42 | var vkontakte = module.exports = function(config) {
|
43 |
|
44 | this.scopes = [
|
45 | "contacts"
|
46 | ];
|
47 |
|
48 | this.init (config);
|
49 |
|
50 | };
|
51 |
|
52 | util.inherits (vkontakte, task);
|
53 |
|
54 | util.extend (vkontakte.prototype, {
|
55 |
|
56 | run: function() {
|
57 |
|
58 | var self = this;
|
59 | self.failed('use method [login|callback|profile]');
|
60 |
|
61 | },
|
62 |
|
63 | login: function () {
|
64 |
|
65 | var self = this;
|
66 | var req = self.req;
|
67 | var res = self.res;
|
68 | var query = req.url.query;
|
69 |
|
70 | var scopes = [];
|
71 |
|
72 | self.scopes.map(function(scope) {
|
73 | scopes.push (vkontakteScopes[scope]);
|
74 | });
|
75 |
|
76 | var getParams = {
|
77 | client_id: vkontakteConfig.appId,
|
78 | response_type : "code",
|
79 | redirect_uri: vkontakteConfig.callbackUrl,
|
80 | scope: scopes.join(','),
|
81 | };
|
82 |
|
83 | var redirectUrl = vkontakteConfig.requestTokenUrl + "?" + querystring.stringify(getParams);
|
84 |
|
85 | self.completed(redirectUrl);
|
86 | },
|
87 |
|
88 | callback: function() {
|
89 |
|
90 | var self = this;
|
91 | var req = self.req;
|
92 | var query = req.url.query;
|
93 |
|
94 | req.user.tokens = {};
|
95 |
|
96 | if (query.error || !query.code) {
|
97 | self.failed (query.error_description || "token was not accepted");
|
98 | }
|
99 |
|
100 | var oa = new OAuth2(vkontakteConfig.appId, vkontakteConfig.appSecret, vkontakteConfig.baseUrl, vkontakteConfig.authorizeUrl, vkontakteConfig.accessTokenUrl);
|
101 |
|
102 | oa.getOAuthAccessToken(
|
103 | query.code,
|
104 | {
|
105 | redirect_uri: vkontakteConfig.callbackUrl
|
106 | },
|
107 | function(error, access_token, refresh_token, results){
|
108 |
|
109 | if (error || !access_token) {
|
110 |
|
111 | self.failed(error || 'Bad request!');
|
112 |
|
113 | } else {
|
114 |
|
115 | req.user.tokens.oauth_access_token = access_token;
|
116 | if (refresh_token) req.user.tokens.oauth_refresh_token = refresh_token;
|
117 |
|
118 | self.completed (results.user_id);
|
119 | }
|
120 | });
|
121 | },
|
122 |
|
123 | profile: function() {
|
124 |
|
125 | var self = this;
|
126 | var req = self.req;
|
127 | var tokens = req.user.tokens;
|
128 |
|
129 | var options = {
|
130 | uid: self.userId,
|
131 | fields: 'uid,first_name,last_name,photo'
|
132 | };
|
133 |
|
134 | var oa = new OAuth2(vkontakteConfig.appId, vkontakteConfig.appSecret, vkontakteConfig.baseUrl, vkontakteConfig.authorizeUrl, vkontakteConfig.accessTokenUrl);
|
135 |
|
136 | oa.getProtectedResource(
|
137 | "https://api.vkontakte.ru/method/getProfiles?"+querystring.stringify(options),
|
138 | tokens.oauth_access_token,
|
139 | function (error, data, response) {
|
140 |
|
141 | if (error) {
|
142 | self.failed(error);
|
143 | } else {
|
144 | try {
|
145 | var user = JSON.parse(data).response[0];
|
146 | self.completed(self.mappingUser(user));
|
147 | } catch (e) {
|
148 | self.failed(e);
|
149 | }
|
150 | }
|
151 | });
|
152 | },
|
153 |
|
154 | mappingUser: function(user) {
|
155 |
|
156 | return {
|
157 | name: user.first_name+' '+user.last_name,
|
158 | email: "id"+user.uid+ "@vk.com",
|
159 | avatar: user.photo,
|
160 | link: "http://vk.com/id"+user.uid,
|
161 | externalId: user.uid,
|
162 | authType: 'vk'
|
163 | };
|
164 |
|
165 | }
|
166 | });
|