auth_usps.php 6.2 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
<?php        
    /* 
    Here to get a USPS access token. 
    User must have previously authenticated with USPS through the MSE user portal. The access
    token is valid until revoked (no refresh token)
    Called from usps/php/login_token.php


    */
    function auth_usps($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_usps - 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;
    }

    function login_usps($uid, $appid, $ccode) {

        dbg_log("login_usps: ".$uid." - ".$appid." - ".$ccode);

        $realm = 'MSE User Authentication';

        // Token is static, no refresh required.
        $auth = load_userapp($uid, $appid);
        if (!$auth) {
            header("WWW-Authenticate: Basic/MSE realm=$realm");
            header("HTTP/1.0 401 Unauthorized");
            dbg_log("login_usps - user not authorized for service: ".$uid." - ".$appid);
            echo "<h4>login_usps - user not authorized for service: ".$uid." - ".$appid."</h4>";  
            exit(1);                  
        }

        $app = load_application($appid);

        $url = base_uri().$app['remoteLoginUrl'];

        $fields = array('token' => $auth['access_token'], 'code' => $ccode);

        dbg_log(" - ".$url." - 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(" - response: ".json_encode($message));

    }

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

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

    /*
    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);

        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_usps($appid, $code, $redirect_uri) {

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

        global $client_id, $client_secret;

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

        $url = base_uri()."/mse/test/usps/php/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_usps - POST (token) response: ".$message['http_status']);
        dbg_log("body:  ".$message['body']);
        if ($message['http_status'] == 200) {
            $token = json_decode($message['body'], true);
            // TODO: add any additional fields here.
            $token_json = json_encode($token);
            dbg_log("token: ".$token_json);
            return $token_json;
        }
        else {
            dbg_log("error: ".$response);
        }
        return NULL;
    }

    /*
    function base_uri() {
        $protocol = 'http://';
        if (isset($_SERVER['HTTPS']) 
            && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) 
            || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') 
        {
            $protocol = 'https://';
        }
        return $protocol.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
    }
    */

?>