summaryrefslogtreecommitdiff
blob: 431ba910088b0123b9502dbbab03554c6c858e93 (plain)
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
<?php

if ( PHP_SAPI !== 'cli' ) {
	die( "CLI-only test script\n" );
}

/**
 * A basic client for overall testing
 */

require '../lib/OAuth.php';

$consumerKey = 'dpf43f3p2l4k3l03';
$consumerSecret = 'kd94hf93k423kf44';
$baseurl = 'https://localhost/wiki/index.php?title=Special:OAuth';
$endpoint = $baseurl . '/initiate&format=json&oauth_callback=oob';

$endpoint_acc = $baseurl . '/token&format=json';

$c = new OAuthConsumer( $consumerKey, $consumerSecret );
$parsed = parse_url( $endpoint );
$params = [];
parse_str( $parsed['query'], $params );
$req_req = OAuthRequest::from_consumer_and_token( $c, null, "GET", $endpoint, $params );
$hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
$sig_method = $hmac_method;
$req_req->sign_request( $sig_method, $c, null );

$headers = [ $req_req->to_header() ];

echo "Calling: $endpoint\nHeader: {$headers[0]}\n\n";

$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $endpoint );
curl_setopt( $ch, CURLOPT_PORT, 443 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $ch, CURLOPT_HEADER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
$data = curl_exec( $ch );

if ( !$data ) {
	echo 'Curl error: ' . curl_error( $ch );
}

echo "Returned: $data\n\n";

$token = json_decode( $data );

print "Visit $baseurl/authorize&oauth_token={$token->key}&oauth_consumer_key=$consumerKey\n";

// ACCESS TOKEN
print "Enter the verification code:\n";
$fh = fopen( "php://stdin", "r" );
$line = fgets( $fh );

$rc = new OAuthConsumer( $token->key, $token->secret );
$parsed = parse_url( $endpoint_acc );
parse_str( $parsed['query'], $params );
$params['oauth_verifier'] = trim( $line );

$acc_req = OAuthRequest::from_consumer_and_token( $c, $rc, "GET", $endpoint_acc, $params );
$acc_req->sign_request( $sig_method, $c, $rc );

echo "Calling: $acc_req\n";

unset( $ch );
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, (string)$acc_req );
curl_setopt( $ch, CURLOPT_PORT, 443 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $ch, CURLOPT_HEADER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
$data = curl_exec( $ch );
if ( !$data ) {
	echo 'Curl error: ' . curl_error( $ch );
}

echo "Returned: $data\n\n";