auth_dropbox.php 6.55 KB
Newer Older
Steve Johnson's avatar
Steve Johnson committed
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
<?php        

    /* 
    Here to get a Dropbox access token. 
    User must have previously authenticated with Dropbox through the MSE user portal. The access
    token is valid until revoked (no refresh token)
    Called from sessionToken.php
    */
    function auth_dropbox($uid, $appid) {

        // Token is static, no refresh required.
        $auth = load_userapp($uid, $appid);
        if (!$auth) {
            header('HTTP/1.0 404 Not Found');
            dbg_log("auth_dropbox - userapp not found for user/app: ".$uid." - ".$appid);
            echo "<h4>404 Not Found.</h4>";  
            echo "<p>uid: ".$uid."   appid: ".$appid;
            exit(1);                  
        }

        dbg_log("auth record: ".$auth);
        // Add our application identifier (informational)
        $auth['appid'] = $appid;

        // Return token to caller
        $auth_json = json_encode($auth);

        // Return token to caller
        header('HTTP/1.0 200 OK');
        header('Content-type: application/json');
        echo $auth_json;
    }

    // Don't think this is needed.
    /*
    function logout_dropbox() {
        unset($_SESSION['access_token']);
    }
    */

    // Here to generate a DropBox access token.
    function authurl_dropbox($appid, $redirect_uri) {
        $app = load_application($appid);
        $client_id = $app['clientId'];
        $url = "https://www.dropbox.com/1/oauth2/authorize?response_type=code&client_id=".$client_id."&redirect_uri=".$redirect_uri;
        dbg_log("authurl_dropbox: ".$url);
        return $url;
    }

    /* Sample responses

    - code: (code expires in one hour from issue)
    7JzIe4Mbv8AAAAAAAAAAARwzIWLDrgIw_0fuBk8M9ck

    - token:
    {
        "access_token": "q_LNXMgn-k0AAAAAAAAAAaNjHU9yBp4dqHGwVQDNrdL5p9VPOXczMcuCVnhXD3sc",
        "token_type": "bearer",
        "uid": "9518453"
    }

    - account info:
    {
        "referral_link": "https://db.tt/32Ld5W8I",
        "display_name": "S J",
        "uid": 9518453,
        "country": "US",
        "quota_info": {
            "datastores": 0,
            "shared": 358496,
            "quota": 5100273664,
            "normal": 3136970287
        },
        "email": "steve@svenyonson.com"
    }

     */

    /*
    function parse_response($ch,$response) {
        $result = array();
        $header_size = curl_getinfo($ch,CURLINFO_HEADER_SIZE);
        $result['header'] = substr($response, 0, $header_size);
        $result['body'] = substr( $response, $header_size );
        $result['http_status'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $result['last_url'] = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

        //dbg_log("parse_response: ".$response);
        //dbg_log(" - http_status: ".$result['http_status']);
        //dbg_log(" - header_size: ".$header_size);
        //dbg_log(" - header: ".$result['header']);
        //dbg_log(" - body: ".$result['body']);

        return $result;
    }
    */

    /*
    // This is only used for debugging (commented out below)
    function exec_curl_log($ch) {
        if (!isset($_SESSION['request_num'])) {
            $_SESSION['request_num'] = 0;
        }
        $log_dir = dirname(__FILE__).'/log';
        if (!file_exists($log_dir)) {
            mkdir($log_dir, 0777, true);
        }
        $f = fopen($log_dir.'/request_'.$_SESSION['request_num'].'.txt', 'w');
        curl_setopt_array($ch, array(
            //CURLOPT_URL            => $url, 
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_FOLLOWLOCATION => 1,
            CURLOPT_VERBOSE        => 1,
            CURLOPT_STDERR         => $f,
        ));
        $response = curl_exec($ch);
        fclose($f); 
        $_SESSION['request_num'] = $_SESSION['request_num']+1;

        return $response;       
    }
    */

    // Here to redeem an access code for an access token. We pass the redirect_uri only
    // for validation - no redirect occurs.
    function access_token_dropbox($appid, $code, $redirect_uri) {

        dbg_log("access_token_dropbox(".$appid.", ".$code.", ".$redirect_uri);

        global $client_id, $client_secret;

        $app = load_application($appid);
        $client_id = $app['clientId'];
        $client_secret = $app['clientSecret'];

        $url = "https://api.dropbox.com/1/oauth2/token?code=".$code."&grant_type=authorization_code";
        $fields = array('client_id' => $client_id, 'client_secret' => $client_secret, 'redirect_uri' => $redirect_uri);

        dbg_log(" - fields: ".json_encode($fields));

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, 1);

        $response = exec_curl_log($ch);
        //$response = curl_exec($ch);
        $message = parse_response($ch, $response);
        curl_close($ch);

        dbg_log("access_token_dropbox - POST (token) response: ".$message['http_status']);
        if ($message['http_status'] == 200) {
            
            $token = json_decode($message['body'], true);
            $access_token = $token['access_token'];
            $auth_str = "Bearer ".$access_token;

            dbg_log("auth_str: ".$auth_str);

            $url = "https://api.dropbox.com/1/account/info";
            // Note: $options is an array of strings, not an associative array!!
            $options = array('Authorization: '.$auth_str);

            $ch = curl_init($url);
            curl_setopt($ch, CURLOPT_HTTPGET, 1);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $options);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HEADER, 1);
            $response = curl_exec($ch);
            //$response = exec_curl_log($ch);
            $message = parse_response($ch, $response);
            curl_close($ch);

            dbg_log("access_token_dropbox - GET (account/info) response: ".$message['http_status']);

            if ($message['http_status'] == 200) {
                $account_info = json_decode($message['body'], true);
                $email = $account_info['email'];
                $token['sp_email'] = $email;
                $pieces = explode("@",$email);
                if (count($pieces)>1) {
                    $token['sp_user'] = $pieces[0];            
                }
            }
            else {
                $token['sp_email'] = "unknown";
            }

            $token_json = json_encode($token);
            dbg_log("token: ".$token_json);
            return $token_json;
        }
        else {
            dbg_log("error: ".$response);
        }
        return NULL;
    }
?>