This repository has been archived by the owner on Apr 18, 2019. It is now read-only.
forked from erikeldridge/openid_oauth_hybrid_app
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
150 lines (114 loc) · 5.26 KB
/
index.php
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<!--
/**
* This is the init script. Load this in a browser and click the log in button.
* After rolling through the openid flow, it'll call back to proxy.php
* to fetch profile data, and then display profile pic and name.
*/
-->
<div style="border-bottom: 1px solid #ddd; padding: 1ex" id="header">
<div style="display:none" id="auth">
<a href="openid/?openid_identifier=yahoo.com">
<img src="http://l.yimg.com/a/i/reg/openid/buttons/2_new.png" id="openid" style="border:none"/>
</a>
<p>--- or ---</p>
<!-- oauthmod -->
<a href="oauth/" id="oauth">
auth w/ oauth
</a>
</div>
<div id="profile" style="display:none">
<span id="name"></span> - <a href="." id="logout">logout</a><br/>
<img id="pic" src="http://l.yimg.com/a/i/ww/met/anim_loading_sm_082208.gif"/>
</div>
</div>
<script type="text/javascript" src="http://yui.yahooapis.com/combo?3.0.0b1/build/yui/yui-min.js"></script>
<script>
YUI().use('node', 'event', 'io', 'json', 'cookie', function (Y) {
var proxyUri = 'proxy.php',
handleIoComplete = function (id, o, args) {
//Try/catch described in http://developer.yahoo.com/yui/3/json/#errors
try {
var json = Y.JSON.parse(o.responseText);
Y.log(json);
//display profile pic & nickname.
Y.get('#profile').setStyle('display', 'block');
Y.get('#name').set('innerHTML', json.query.results.profile.nickname);
Y.get('#pic').set('src', json.query.results.profile.image.imageUrl);
//show log out link
Y.get('#logout').on("click", function () {
Y.Cookie.remove('session');
});
} catch (e) {
Y.log(e);
}
},
handleClick = function (e) {
//Def. handle of openid auth window.
var popup = null,
//Def. handle for popup monitoring interval described below.
popupCheckInterval = null,
//Def. fn to catch openid when auth is complete, or handle failure.
checkPopup = function () {
//If openid is defined, auth is complete, so ...
if (popup.session) {
//register session state
Y.Cookie.set('session', 'true');
//... hide openid/oauth ui, ...
Y.get('#auth').setStyle('display', 'none');
//... fetch profile data, ...
Y.on('io:complete',
handleIoComplete,
this
);
Y.io(proxyUri);
//... stop monitoring popup.
clearInterval(popupCheckInterval);
//If popup handle ceases to point to anything, ...
} else if (Y.Lang.isUndefined(popup)) {
//... stop monitoring popup.
clearInterval(popupCheckInterval);
}
},
//Define fn to spawn popup window for openid authentication.
launchPopup = function (url) {
popup = window.open(
url,
//unique id
new Date().getTime(),
'toolbar=0,scrollbars=1,location=1,statusbar=1,menubar=0,'
+'resizable=1,width=500,height=500,left = 200,top = 200'
);
//Monitor popup to determine when auth is complete.
popupCheckInterval = window.setInterval(
checkPopup,
1000
);
};
//If login button clicked ...
if (e.target.get('id') && ('openid' === e.target.get('id'))) {
//...cancel default action and ...
e.halt();
//...init openid authentication in popup using href of a-tag around button.
launchPopup(e.target.get('parentNode').get('href'));
} else if (e.target.get('id') && ('oauth' === e.target.get('id'))) {
//...cancel default action and ...
e.halt();
//...init openid authentication in popup using href of a-tag around button.
launchPopup(e.target.get('href'));
}
};
//Init.
var session = Y.Cookie.get('session');
//If cookie doesn't exist, user's not logged in and/or is not registered, so ...
if ('true' === session) {
Y.on('io:complete',
handleIoComplete,
this
);
Y.io(proxyUri);
} else {
//... reveal log in link and ...
Y.get('#auth').setStyle('display', 'block').on("click", handleClick);
}
});
</script>