1
1
'use strict' ;
2
2
3
3
// Helper functions for accessing the vkontakte API.
4
+
4
5
var https = require ( 'https' ) ;
5
6
var Parse = require ( 'parse/node' ) . Parse ;
7
+ var logger = require ( '../logger' ) . default ;
6
8
7
9
// Returns a promise that fulfills iff this user id is valid.
8
- function validateAuthData ( authData ) {
9
- return request ( "users.get?v=V&access_token=" + authData . access_token ) . then ( function ( response ) {
10
- if ( response && response . response && response . response [ 0 ] . uid == authData . id ) {
11
- return ;
10
+ function validateAuthData ( authData , params ) {
11
+ return vkOAuth2Request ( params ) . then ( function ( response ) {
12
+ if ( response && response && response . access_token ) {
13
+ return request ( "api.vk.com" , "method/secure.checkToken?token=" + authData . access_token + "&client_secret=" + params . appSecret + "&access_token=" + response . access_token ) . then ( function ( response ) {
14
+ if ( response && response . response && response . response . user_id == authData . id ) {
15
+ return ;
16
+ }
17
+ throw new Parse . Error ( Parse . Error . OBJECT_NOT_FOUND , 'Vk auth is invalid for this user.' ) ;
18
+ } ) ;
12
19
}
13
- throw new Parse . Error ( Parse . Error . OBJECT_NOT_FOUND , 'Vk auth is invalid for this user.' ) ;
20
+ logger . error ( 'Vk Auth' , 'Vk appIds or appSecret is incorrect.' ) ;
21
+ throw new Parse . Error ( Parse . Error . OBJECT_NOT_FOUND , 'Vk appIds or appSecret is incorrect.' ) ;
14
22
} ) ;
15
23
}
16
24
25
+ function vkOAuth2Request ( params ) {
26
+ var promise = new Parse . Promise ( ) ;
27
+ return promise . then ( function ( ) {
28
+ if ( ! params || ! params . appIds || ! params . appIds . length || ! params . appSecret || ! params . appSecret . length ) {
29
+ logger . error ( 'Vk Auth' , 'Vk auth is not configured. Missing appIds or appSecret.' ) ;
30
+ throw new Parse . Error ( Parse . Error . OBJECT_NOT_FOUND , 'Vk auth is not configured. Missing appIds or appSecret.' ) ;
31
+ }
32
+ return request ( "oauth.vk.com" , "access_token?client_id=" + params . appIds + "&client_secret=" + params . appSecret + "&v=5.59&grant_type=client_credentials" )
33
+ } )
34
+ }
35
+
17
36
// Returns a promise that fulfills iff this app id is valid.
18
37
function validateAppId ( ) {
19
38
return Promise . resolve ( ) ;
20
39
}
21
40
22
41
// A promisey wrapper for api requests
23
- function request ( path ) {
42
+ function request ( host , path ) {
24
43
return new Promise ( function ( resolve , reject ) {
25
- https . get ( "https://api.vk.com/method /" + path , function ( res ) {
44
+ https . get ( "https://" + host + " /" + path , function ( res ) {
26
45
var data = '' ;
27
46
res . on ( 'data' , function ( chunk ) {
28
47
data += chunk ;
@@ -40,4 +59,4 @@ function request(path) {
40
59
module . exports = {
41
60
validateAppId : validateAppId ,
42
61
validateAuthData : validateAuthData
43
- } ;
62
+ } ;
0 commit comments