Skip to content

Commit

Permalink
Merge pull request #119 from ucfcdl/UDOIT_issue75
Browse files Browse the repository at this point in the history
Fix to major issue with Oauth2 saving API keys
  • Loading branch information
bagofarms committed Mar 29, 2016
2 parents d571862 + 2e5e3fc commit b431cf6
Show file tree
Hide file tree
Showing 21 changed files with 58 additions and 22 deletions.
Empty file modified .bowerrc
100644 → 100755
Empty file.
Empty file modified .buildpacks
100644 → 100755
Empty file.
Empty file modified HEROKU.md
100644 → 100755
Empty file.
Empty file modified Procfile
100644 → 100755
Empty file.
Empty file modified UDOIT_Release.pdf
100644 → 100755
Empty file.
Empty file modified app.json
100644 → 100755
Empty file.
Empty file modified bower.json
100644 → 100755
Empty file.
22 changes: 11 additions & 11 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file modified config/herokuConfig.php
100644 → 100755
Empty file.
Empty file modified config/settings.php
100644 → 100755
Empty file.
Empty file modified db_mysql_setup.php
100644 → 100755
Empty file.
Empty file modified db_pg_setup.php
100644 → 100755
Empty file.
Empty file modified lib/db.php
100644 → 100755
Empty file.
Empty file modified package.json
100644 → 100755
Empty file.
Empty file modified phpfpm_custom.conf
100644 → 100755
Empty file.
Binary file added public/assets/img/udoit_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 37 additions & 2 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,36 @@
$sth->execute();

$result = $sth->fetchAll();
// print_r($result);

/* TODO:
if (isset($result[0])) {
$_SESSION['refresh_token'] = $result[0]['api_key'];
//Exchange code for API key (Can break this part into its own function, have it return the api key)
$url = $base_url . '/login/oauth2/token';
$postdata = array(
'grant_type' => 'refresh_token',
'client_id' => $oauth2_id,
'redirect_uri' => $oauth2_uri,
'client_secret' => $oauth2_key,
'refresh_token' => $_SESSION['refresh_token']
);
$post = http_build_query($postdata);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = json_decode(curl_exec($ch));
curl_close($ch);
$_SESSION['api_key'] = $response->access_token;
}
*/

if (isset($result[0])) {
$_SESSION['api_key'] = $result[0]['api_key'];
Expand All @@ -97,9 +127,14 @@
// Do we have an API key?
if (isset($_SESSION['api_key'])) {
//If we do, test it out
$url = $base_url.'/api/v1/users/'.$_SESSION['launch_params']['custom_canvas_user_id'].'/profile?access_token='.$_SESSION['api_key'];
$resp = Request::get($url)->send();
$url = $base_url.'api/v1/users/'.$_SESSION['launch_params']['custom_canvas_user_id'].'/profile';
$resp = Request::get($url)
->addHeader('Authorization', 'Bearer '.$_SESSION['api_key'])
->send();
$redirect = !isset($resp->body->id);
// echo $url;
// print_r($resp);
// die();
} else {
//Otherwise, redirect to the oauth2 process
$redirect = true;
Expand Down
19 changes: 10 additions & 9 deletions public/oauth2response.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ function printError($msg){
$url = $base_url . '/login/oauth2/token';

$postdata = array(
'grant_type' => 'authorization_code',
'client_id' => $oauth2_id,
'redirect_uri' => $oauth2_uri,
'client_secret' => $oauth2_key,
Expand All @@ -68,24 +69,24 @@ function printError($msg){

$_SESSION['api_key'] = $response->access_token;

// TODO: Modify to save refresh token instead
// Save API Key to DB
$dbh = include('../lib/db.php');


$sth = $dbh->prepare("SELECT * FROM $db_user_table WHERE id=:userid");
$sth = $dbh->prepare("SELECT * FROM $db_user_table WHERE id=:userid LIMIT 1");
$sth->bindParam(':userid', $_SESSION['launch_params']['custom_canvas_user_id'], PDO::PARAM_INT);
$sth->execute();

if($sth->rowCount()) {
$sth = $dbh->prepare("UPDATE $db_user_table (api_key, date_created) VALUES (:key, :time)");
}
else {
$sth = $dbh->prepare("INSERT INTO $db_user_table (id, api_key, date_created) VALUES (:userid, :key, :time)");
$sth->bindParam(':userid', $_SESSION['launch_params']['custom_canvas_user_id'], PDO::PARAM_INT);
$result = $sth->fetchAll();

if(isset($result[0])) {
$sth = $dbh->prepare("UPDATE $db_user_table SET api_key=:key WHERE id=:userid LIMIT 1");
} else {
$sth = $dbh->prepare("INSERT INTO $db_user_table (id, api_key, date_created) VALUES (:userid, :key, CURRENT_TIMESTAMP)");
}

$sth->bindParam(':key', $_SESSION['api_key'], PDO::PARAM_STR);
$sth->bindValue(':time', time(), PDO::PARAM_INT);
$sth->bindParam(':userid', $_SESSION['launch_params']['custom_canvas_user_id'], PDO::PARAM_INT);
$sth->execute();

session_write_close();
Expand Down
Empty file modified public/reports/.gitkeep
100644 → 100755
Empty file.
Empty file modified templates/error.php
100644 → 100755
Empty file.
Empty file modified templates/template.php
100644 → 100755
Empty file.

0 comments on commit b431cf6

Please sign in to comment.