client.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. require_once("common.inc.php");
  3. $key = @$_REQUEST['key'];
  4. $secret = @$_REQUEST['secret'];
  5. $token = @$_REQUEST['token'];
  6. $token_secret = @$_REQUEST['token_secret'];
  7. $endpoint = @$_REQUEST['endpoint'];
  8. $action = @$_REQUEST['action'];
  9. $dump_request = @$_REQUEST['dump_request'];
  10. $user_sig_method = @$_REQUEST['sig_method'];
  11. $sig_method = $hmac_method;
  12. if ($user_sig_method) {
  13. $sig_method = $sig_methods[$user_sig_method];
  14. }
  15. $test_consumer = new OAuthConsumer($key, $secret, NULL);
  16. $test_token = NULL;
  17. if ($token) {
  18. $test_token = new OAuthConsumer($token, $token_secret);
  19. }
  20. if ($action == "request_token") {
  21. $parsed = parse_url($endpoint);
  22. $params = array();
  23. parse_str($parsed['query'], $params);
  24. $req_req = OAuthRequest::from_consumer_and_token($test_consumer, NULL, "GET", $endpoint, $params);
  25. $req_req->sign_request($sig_method, $test_consumer, NULL);
  26. if ($dump_request) {
  27. Header('Content-type: text/plain');
  28. print "request url: " . $req_req->to_url(). "\n";
  29. print_r($req_req);
  30. exit;
  31. }
  32. Header("Location: $req_req");
  33. }
  34. else if ($action == "authorize") {
  35. $callback_url = "$base_url/client.php?key=$key&secret=$secret&token=$token&token_secret=$token_secret&endpoint=" . urlencode($endpoint);
  36. $auth_url = $endpoint . "?oauth_token=$token&oauth_callback=".urlencode($callback_url);
  37. if ($dump_request) {
  38. Header('Content-type: text/plain');
  39. print("auth_url: " . $auth_url);
  40. exit;
  41. }
  42. Header("Location: $auth_url");
  43. }
  44. else if ($action == "access_token") {
  45. $parsed = parse_url($endpoint);
  46. $params = array();
  47. parse_str($parsed['query'], $params);
  48. $acc_req = OAuthRequest::from_consumer_and_token($test_consumer, $test_token, "GET", $endpoint, $params);
  49. $acc_req->sign_request($sig_method, $test_consumer, $test_token);
  50. if ($dump_request) {
  51. Header('Content-type: text/plain');
  52. print "request url: " . $acc_req->to_url() . "\n";
  53. print_r($acc_req);
  54. exit;
  55. }
  56. Header("Location: $acc_req");
  57. }
  58. ?>
  59. <html>
  60. <head>
  61. <title>OAuth Test Client</title>
  62. </head>
  63. <body>
  64. <div><a href="index.php">server</a> | <a href="client.php">client</a></div>
  65. <h1>OAuth Test Client</h1>
  66. <h2>Instructions for Use</h2>
  67. <p>This is a test client that will let you test your OAuth server code. Enter the appropriate information below to test.</p>
  68. <p>Note: we don't store any of the information you type in.</p>
  69. <form method="POST" name="oauth_client">
  70. <h3>Choose a Signature Method</h3>
  71. <select name="sig_method">
  72. <?php
  73. foreach ($sig_methods as $name=> $method) {
  74. $selected = "";
  75. if ($name == $sig_method->get_name()) {
  76. $selected = " selected='selected'";
  77. }
  78. print "<option value='$name'$selected>$name</option>\n";
  79. }
  80. ?>
  81. </select>
  82. <h3>Enter The Endpoint to Test</h3>
  83. endpoint: <input type="text" name="endpoint" value="<?php echo $endpoint; ?>" size="100"/><br />
  84. <small style="color: green">Note: You can include query parameters in there to have them parsed in and signed too</small>
  85. <h3>Enter Your Consumer Key / Secret</h3>
  86. consumer key: <input type="text" name="key" value="<?php echo $key; ?>" /><br />
  87. consumer secret: <input type="text" name="secret" value="<?php echo $secret;?>" /><br />
  88. dump request, don't redirect: <input type="checkbox" name="dump_request" value="1" <?php if ($dump_request) echo 'checked="checked"'; ?>/><br />
  89. make a token request (don't forget to copy down the values you get)
  90. <input type="submit" name="action" value="request_token" />
  91. <h3>Enter Your Request Token / Secret</h3>
  92. token: <input type="text" name="token" value="<?php echo $token; ?>" /><br />
  93. token secret: <input type="text" name="token_secret" value="<?php echo $token_secret; ?>" /><br />
  94. <p><strong>Don't forget to update your endpoint to point at the auth or access token url</strong></p>
  95. try to authorize this token: <input type="submit" name="action" value="authorize" /><br />
  96. try to get an access token: <input type="submit" name="action" value="access_token" /><br />
  97. <h3>Currently Supported Signature Methods</h3>
  98. <p>Current signing method is: <?php echo $sig_method->get_name() ?></p>
  99. <ul>
  100. <?php
  101. foreach ($sig_methods as $key => $method) {
  102. print "<li>$key";
  103. if ($key != $sig_method->get_name()) {
  104. print "(<a href='?sig_method=$key'>switch</a>)";
  105. }
  106. print "</li>\n";
  107. }
  108. ?>
  109. </ul>
  110. <?php
  111. if ("RSA-SHA1" == $sig_method->get_name()) {
  112. // passing test_server as a dummy referecne
  113. print "<pre>" . $sig_method->fetch_private_cert($test_server). "</pre>\n";
  114. print "<pre>" . $sig_method->fetch_public_cert($test_server) . "</pre>\n";
  115. }
  116. ?>
  117. <h3>Further Resources</h3>
  118. <p>There is also a <a href="index.php">test server</a> implementation in here.</p>
  119. <p>The code running this example can be downloaded from the PHP section of the OAuth google code project: <a href="http://code.google.com/p/oauth/">http://code.google.com/p/oauth/</a>
  120. </body>