Skip to content

Instantly share code, notes, and snippets.

@Danno040
Created April 15, 2015 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Danno040/fa49a35e67fb73cccf71 to your computer and use it in GitHub Desktop.
Save Danno040/fa49a35e67fb73cccf71 to your computer and use it in GitHub Desktop.
SSO Example code
<html>
<body>
<form method="POST" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
<label>API Key</label><br />
<input type="text" name="api_key" value="<?php echo isset($_POST['api_key']) ? $_POST['api_key'] : '' ?>" /><br />
<br />
<label>Domain</label><br />
<input type="text" name="domain" value="<?php echo isset($_POST['domain']) ? $_POST['domain'] : '' ?>" /><br />
<br />
<label>Email</label><br />
<input type="email" name="email" value="<?php echo isset($_POST['email']) ? $_POST['email'] : '' ?>" /><br />
<br />
<label>Display Name</label><br />
<input type="text" name="displayname" value="<?php echo isset($_POST['displayname']) ? $_POST['displayname'] : '' ?>" /><br />
<br />
<input type="checkbox" value="1" name="do-create" <?php echo isset($_POST['do-create']) ? ' checked="checked"' : '' ?> />
<label>Create If Missing</label><br />
<br />
<input type="submit" />
</form>
<?php
if( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
// Run curl request
$ch = curl_init('http://' . $_POST['domain'] . '/api/sso');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
'token' => $_POST['api_key'],
'email' => $_POST['email'],
'displayname' => $_POST['displayname'],
'do-create' => !empty($_POST['do-create']) ? 1 : 0,
)));
$response = curl_exec($ch);
$info = curl_getinfo($ch);
$data = json_decode($response, true);
echo "Request:";
echo "<pre>" . var_export(array(
'url' => 'http://' . $_POST['domain'] . '/api/sso',
'postfields' => array(
'token' => $_POST['api_key'],
'email' => $_POST['email'],
'displayname' => $_POST['displayname'],
'do-create' => !empty($_POST['do-create']) ? 1 : 0,
)
), true) . "</pre>";
echo "Response:";
echo "<pre>" . var_export(array(
'http_code' => $info['http_code'],
'content_type' => $info['content_type'],
'response' => $response,
'data' => $data
), true) . "</pre>";
if( is_array($data) && !empty($data['token']) ) {
?>
<a href="http://<?php echo $_POST['domain'] ?>/sign-in?token=<?php echo $data['token'] ?>">Sign In Link</a>
<?php
}
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment