@@ -17,8 +17,15 @@ function dispatchResult() {
1717
1818window . addEventListener ( 'load' , dispatchResult ) ;
1919
20- const popupFeatures = 'toolbar=no, menubar=no, width=600, height=800, top=100, left=100' ;
21- const popupName = 'Dropbox OAuth' ;
20+ const windowName = 'Dropbox OAuth' ;
21+ const defaultWindowOptions = {
22+ toolbar : 'no' ,
23+ menubar : 'no' ,
24+ width : 600 ,
25+ height : 800 ,
26+ top : 100 ,
27+ left : 100 ,
28+ } ;
2229
2330/**
2431 * @class DropboxPopup
@@ -27,16 +34,57 @@ const popupName = 'Dropbox OAuth';
2734 * @param {string } [options.clientId] - The client id for your app.
2835 * @param {string } [options.clientSecret] - The client secret for your app.
2936 * @param {string } [options.redirectUri] - The redirect Uri to return to once auth is complete.
37+ * @param {string } [options.tokenAccessType] - type of token to request. From the following:
38+ * legacy - creates one long-lived token with no expiration
39+ * online - create one short-lived token with an expiration
40+ * offline - create one short-lived token with an expiration with a refresh token
41+ * @param {Array<string> } [options.scope] - scopes to request for the grant
42+ * @param {string } [options.includeGrantedScopes] - whether or not to include
43+ * previously granted scopes.
44+ * From the following:
45+ * user - include user scopes in the grant
46+ * team - include team scopes in the grant
47+ * Note: if this user has never linked the app, include_granted_scopes must be None
48+ * @param {boolean } [options.usePKCE] - Whether or not to use Sha256 based PKCE.
49+ * PKCE should be only use on client apps which doesn't call your server.
50+ * It is less secure than non-PKCE flow but can be used if you are unable to safely
51+ * retrieve your app secret
52+ * @param {object } windowOptions
53+ * @param {number } [windowOptions.width] - The width of the popup window in pixels.
54+ * @param {number } [windowOptions.height] - The height of the popup window in pixels.
55+ * @param {number } [windowOptions.top] - The number of pixels from the top of the screen.
56+ * @param {number } [windowOptions.left] - The number of pixels from the left side of the screen.
57+ * @param {object } [windowOptions.additionalParams] - Any additional parameters desired to be used
58+ * with the window.open() command. Note, by default, we add the parameters toolbar=no and menubar=no
59+ * in order to ensure this opens as a popup.
3060 */
3161export default class DropboxPopup {
32- constructor ( options ) {
62+ constructor ( options , windowOptions ) {
3363 this . clientId = options . clientId ;
34- this . clientSecret = options . clientSecret ;
64+ this . redirectUri = options . redirectUri ;
65+ this . clientSecret = options . clientSecret || '' ;
66+ this . tokenAccessType = options . tokenAccessType || 'offline' ;
67+ this . scope = options . scope || null ;
68+ this . includeGrantedScopes = options . includeGrantedScopes || 'none' ;
69+ this . usePKCE = options . usePKCE || false ;
70+
3571 this . authObject = new DropboxAuth ( {
3672 clientId : this . clientId ,
3773 clientSecret : this . clientSecret ,
3874 } ) ;
39- this . redirectUri = options . redirectUri ;
75+
76+ this . state = Math . random ( ) . toString ( 36 ) . substring ( 7 ) ;
77+
78+ // Set window options with format of key=value,key=value...
79+ const overlayedWindowOptions = Object . assign ( defaultWindowOptions , windowOptions ) ;
80+ this . windowOptions = '' ;
81+ Object . keys ( overlayedWindowOptions ) . forEach ( ( key ) => {
82+ if ( this . windowOptions === '' ) {
83+ this . windowOptions = `${ key } =${ overlayedWindowOptions [ key ] } ` ;
84+ } else {
85+ this . windowOptions = this . windowOptions . concat ( `, ${ key } =${ overlayedWindowOptions [ key ] } ` ) ;
86+ }
87+ } ) ;
4088 }
4189
4290 /**
@@ -49,8 +97,8 @@ export default class DropboxPopup {
4997 window . removeEventListener ( 'message' , this . handleRedirect ) ;
5098 this . callback = callback ;
5199 this . callback . bind ( this ) ;
52- const authUrl = this . authObject . getAuthenticationUrl ( this . redirectUri , '' , 'code' , 'offline' ) ;
53- const popupWindow = window . open ( authUrl , popupName , popupFeatures ) ;
100+ const authUrl = this . authObject . getAuthenticationUrl ( this . redirectUri , this . state , 'code' , this . tokenAccessType , this . scope , this . includeGrantedScopes , this . usePKCE ) ;
101+ const popupWindow = window . open ( authUrl , windowName , this . windowOptions ) ;
54102 popupWindow . focus ( ) ;
55103
56104 window . addEventListener ( 'message' , ( event ) => this . handleRedirect ( event ) , false ) ;
0 commit comments