-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstrategy.js
148 lines (143 loc) · 4.92 KB
/
strategy.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const OAuth2Strategy = require('passport-oauth').OAuth2Strategy;
const InternalOauthError = require('passport-oauth').InternalOAuthError;
const Dropbox = require('dropbox').Dropbox;
const fetch = require('isomorphic-fetch');
/**
* Dropbox strategy
*
* Options:
* - clientID
* - clientSecret
* - callbackURL
* <optional>
* - passReqToCallback (default false) - directs passport to send the request object
* to the verfication callback
*
* Examples:
*
* With request
* const strategy = new DropboxStrategy(
* {
* clientID: '<clientid>',
* clientSecret: '<clientSecret>',
* callbackURL: '<callbackURL>,
* passReqToCallback: true
* },
* async (request, accessToken, refreshToken, profile, done) => {
* done(null, <userId>);
* }
*
* Without request
* const strategy = new DropboxStrategy(
* {
* clientID: '<clientid>',
* clientSecret: '<clientSecret>',
* callbackURL: '<callbackURL>,
* passReqToCallback: false
* },
* async (accessToken, refreshToken, profile, done) => {
* done(null, <userId>);
* }
*
* Response object: Docker personal
*
* {
* "account_id": "dbid:AAH4f99T0taONIb-OurWxbNQ6ywGRopQngc",
* "name": {
* "given_name": "Franz",
* "surname": "Ferdinand",
* "familiar_name": "Franz",
* "display_name": "Franz Ferdinand (Personal)",
* "abbreviated_name": "FF"
* },
* "email": "[email protected]",
* "email_verified": false,
* "disabled": false,
* "locale": "en",
* "referral_link": "https://db.tt/ZITNuhtI",
* "is_paired": false,
* "account_type": {
* ".tag": "basic"
* },
* "root_info": {
* ".tag": "user",
* "root_namespace_id": "3235641",
* "home_namespace_id": "3235641"
* },
* "profile_photo_url": "https://dl-web.dropbox.com/account_photo/get/dbaphid%3AAAHWGmIXV3sUuOmBfTz0wPsiqHUpBWvv3ZA?vers=1556069330102\u0026size=128x128",
* "country": "US"
* }
*
* Response Object: Docker for Business
*
* {
* "account_id": "dbid:AAH4f99T0taONIb-OurWxbNQ6ywGRopQngc",
* "name": {
* "given_name": "Franz",
* "surname": "Ferdinand",
* "familiar_name": "Franz",
* "display_name": "Franz Ferdinand (Personal)",
* "abbreviated_name": "FF"
* },
* "email": "[email protected]",
* "email_verified": true,
* "disabled": false,
* "locale": "en",
* "referral_link": "https://db.tt/ZITNuhtI",
* "is_paired": true,
* "account_type": {
* ".tag": "business"
* },
* "root_info": {
* ".tag": "user",
* "root_namespace_id": "3235641",
* "home_namespace_id": "3235641"
* },
* "country": "US",
* "team": {
* "id": "dbtid:AAFdgehTzw7WlXhZJsbGCLePe8RvQGYDr-I",
* "name": "Acme, Inc.",
* "sharing_policies": {
* "shared_folder_member_policy": {
* ".tag": "team"
* },
* "shared_folder_join_policy": {
* ".tag": "from_anyone"
* },
* "shared_link_create_policy": {
* ".tag": "team_only"
* }
* },
* "office_addin_policy": {
* ".tag": "disabled"
* }
* },
* "team_member_id": "dbmid:AAHhy7WsR0x-u4ZCqiDl5Fz5zvuL3kmspwU"
* }
*/
class Strategy extends OAuth2Strategy{
constructor(options, verify) {
options = options || {};
options.authorizationURL = options.authorizationURL || 'https://www.dropbox.com/oauth2/authorize';
options.tokenURL = options.tokenURL || 'https://api.dropboxapi.com/oauth2/token';
super(options, verify);
this.name = 'dropbox';
this._passReqToCallback = options.passReqToCallback || false;
}
/**
* returns user profile for passport
* @param {*} accessToken
* @param {*} done
*/
async userProfile(accessToken, done) {
const dbx = new Dropbox({ accessToken: accessToken, fetch: fetch });
try {
const response = await dbx.usersGetCurrentAccount();
return done(null, response);
} catch (e) {
console.log(e);
return done(new InternalOauthError('failed to fetch user profile', e));
}
}
}
module.exports = Strategy;