|
| 1 | +<?php |
| 2 | + |
| 3 | +include 'MWOAuthClient.php'; |
| 4 | +$consumerKey = ''; |
| 5 | +$consumerSecret = ''; |
| 6 | + |
| 7 | +// Configure the connection to the wiki you want to use. Passing title=Special:OAuth as a |
| 8 | +// GET parameter makes the signature easier. Otherwise you need to call |
| 9 | +// $client->setExtraParam('title','Special:OAuth/whatever') for each step. |
| 10 | +// If your wiki uses wgSecureLogin, the canonicalServerUrl will point to http:// |
| 11 | +$config = new MWOAuthClientConfig( |
| 12 | + 'http://en.wikipedia.beta.wmflabs.org/w/index.php?title=Special:OAuth', // url to use |
| 13 | + false, // do we use SSL? (we should probably detect that from the url) |
| 14 | + false // do we validate the SSL certificate? Always use 'true' in production. |
| 15 | +); |
| 16 | +$config->canonicalServerUrl = 'http://en.wikipedia.beta.wmflabs.org'; |
| 17 | +// Optional clean url here (i.e., to work with mobile), otherwise the |
| 18 | +// base url just has /authorize& added |
| 19 | +$config->redirURL = 'http://en.wikipedia.beta.wmflabs.org/wiki/Special:OAuth/authorize?'; |
| 20 | +$cmrToken = new OAuthToken( $consumerKey, $consumerSecret ); |
| 21 | +$client = new MWOAuthClient( $config, $cmrToken ); |
| 22 | + |
| 23 | +session_start(); |
| 24 | + |
| 25 | +if ( !isset( $_GET['action'] ) ) { |
| 26 | + echo "<html><body><p><a href='webdemo.php?action=init'>Start OAuth</a></p></body></html>"; |
| 27 | + exit; |
| 28 | +} else { |
| 29 | + $action = $_GET['action']; |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +if ( $action == 'init' ) { |
| 34 | + |
| 35 | + // Step 1 - Get a request token |
| 36 | + list( $redir, $requestToken ) = $client->initiate(); |
| 37 | + $_SESSION['oauthreqtoken'] = "{$requestToken->key}:{$requestToken->secret}"; |
| 38 | + |
| 39 | + // Step 2 - Have the user authorize your app. |
| 40 | + header( "Location: $redir" ); |
| 41 | + exit; |
| 42 | + |
| 43 | +} elseif ( $action == 'finish' ) { |
| 44 | + |
| 45 | + $verifyCode = $_GET['oauth_verifier']; |
| 46 | + $recKey = $_GET['oauth_token']; |
| 47 | + list( $requestKey, $requestSecret ) = explode( ':', $_SESSION['oauthreqtoken'] ); |
| 48 | + $requestToken = new OAuthToken( $requestKey, $requestSecret ); |
| 49 | + unset( $_SESSION['oauthreqtoken'] ); |
| 50 | + |
| 51 | + //check for csrf |
| 52 | + if ( $requestKey !== $recKey ) { |
| 53 | + die( "CSRF detected" ); |
| 54 | + } |
| 55 | + |
| 56 | + // Step 3 - Exchange the request token and verification code for an access token |
| 57 | + $accessToken = $client->complete( $requestToken, $verifyCode ); |
| 58 | + |
| 59 | + // You're done! Setup your application's session state. Keep the accessToken |
| 60 | + // to use for later calls by your application into MediaWiki. |
| 61 | + session_regenerate_id(); |
| 62 | + $identity = $client->identify( $accessToken ); |
| 63 | + $_SESSION['oauthtoken'] = "{$accessToken->key}:{$accessToken->secret}"; |
| 64 | + $_SESSION['username'] = $identity->username; |
| 65 | + |
| 66 | + // Redirect to your application's main entry point |
| 67 | + header( "Location: webdemo.php?action=info" ); |
| 68 | + |
| 69 | + exit; |
| 70 | +} elseif ( $action == 'info' ) { |
| 71 | + // This is what you're app should do for logged in users |
| 72 | + if ( !isset( $_SESSION['username'] ) || !isset( $_SESSION['oauthtoken'] ) ) { |
| 73 | + die( "Lost Session, <a href='webdemo.php?action=init'>start over</a>" ); |
| 74 | + } |
| 75 | + |
| 76 | + list( $accessKey, $accessSecret ) = explode( ':', $_SESSION['oauthtoken'] ); |
| 77 | + $accessToken = new OAuthToken( $accessKey, $accessSecret ); |
| 78 | + |
| 79 | + // Check their current identity |
| 80 | + $identity = $client->identify( $accessToken ); |
| 81 | + echo "<pre>\n"; |
| 82 | + echo "Authenticated as user {$identity->username}\n"; |
| 83 | + |
| 84 | + // Do a simple API call as the user |
| 85 | + echo "Getting user info: "; |
| 86 | + echo $client->makeOAuthCall( |
| 87 | + $accessToken, |
| 88 | + 'https://localhost/wiki/api.php?action=query&meta=userinfo&uiprop=rights&format=json' |
| 89 | + ); |
| 90 | + echo "</pre>\n"; |
| 91 | + echo "<a href='webdemo.php?action=logout'>Logout</a>"; |
| 92 | + exit; |
| 93 | + |
| 94 | +} elseif ( $action == 'logout' ) { |
| 95 | + session_destroy(); |
| 96 | + echo "<a href='webdemo.php?action=init'>Start Over</a>"; |
| 97 | +} |
| 98 | + |
0 commit comments