Skip to content

Instantly share code, notes, and snippets.

@LinZap
Last active December 8, 2015 05:15
Show Gist options
  • Save LinZap/2ae9133d19e0e48455cf to your computer and use it in GitHub Desktop.
Save LinZap/2ae9133d19e0e48455cf to your computer and use it in GitHub Desktop.
OAuth prototype~ by Zap
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>MyWeb</title>
<script>
window.addEventListener("message", receiveMessage, false);
function receiveMessage(event){
console.log("OK... receiveMessage");
console.log(event);
}
</script>
</head>
<body>
<h1>MyWeb</h1>
<p>use oauth</p>
<button id="btn_login">Login</button>
<script>
NodeList.prototype.on=function(evn,callback){
var i=0,len=this.length;
for(;i<len;i++) this[i].addEventListener(evn,callback);
return this;
}
Element.prototype.on = function(evn,callback){
this.addEventListener(evn,callback);
return this;
}
function $(selector){
return document.querySelectorAll(selector);
}
$('#btn_login').on('click',function(){
var url = "http://10.21.20.113/oauth/server.php";
var w = window.open(url, "_blank", "toolbar=yes, scrollbars=yes, resizable=no, top=0, left=0, width=400, height=400");
w.postMessage("The user is 'bob' and the password is 'secret'",url);
});
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Oauth Web</title>
<script>
window.addEventListener("message", receiveMessage, false);
function receiveMessage(event){
console.log(event);
}
</script>
</head>
<body>
<h1>Oauth Web</h1>
<p>Oatuth</p>
<hr>
<pre>
<?php print_r($_SERVER); // $_SERVER['HTTP_REFERER'] ?>
</pre>
<button onclick="OK()">I Accept</button>
<script>
function OK(){
// here return "http://localhost/myweb/index.php"
// opener is parent window
console.log(window.opener);
var reutn_url="http://localhost/myweb/index.php";
window.opener.postMessage("Hello,I got it",reutn_url)
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment