Skip to content

Commit b797116

Browse files
committed
working with Authprovider
1 parent 2bb840b commit b797116

16 files changed

+218
-393
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "AuthProvider"]
2+
path = AuthProvider
3+
url = [email protected]:formula1/AuthProvider.git

AuthProvider

Submodule AuthProvider added at 169ddc7

dist/api.min.js

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/article.html

Lines changed: 63 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -126,26 +126,28 @@ <h3 class="footer-title">Share</h3>
126126
<script type="text/javascript" src="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js" ></script>
127127
<!-- START: BIND DATA TO VIEW -->
128128
<script type="text/javascript">
129-
130129
var NodeOsBlog = angular.module('NodeOsBlog', []);
131-
NodeOsBlog.filter('to_trusted', ['$sce', function($sce){
132-
return function(text) {
133-
return $sce.trustAsHtml(text);
134-
};
135-
}]);
136-
var errors = [];
130+
NodeOsBlog.filter('to_trusted', ['$sce', function($sce){
131+
return function(text) {
132+
return $sce.trustAsHtml(text);
133+
};
134+
}]);
135+
var errors = [];
136+
var has403 = false;
137137

138138
function add403(){
139+
if(has403) return;
140+
has403 = true;
139141
var topush = {
140142
class:"four-zero-three",
141143
name:"You've hit max data"
142144
};
143-
if(is_authed === 1){
145+
if(user.isLoggedIn){
144146
topush.message = "Apparently, people have been using our app too much..."+
145-
"<button onclick='login()'>Log in</button>";
147+
"<button onclick='user.login()'>Log in</button>";
146148
}else{
147149
topush.message = "If you'd like to continue, please "+
148-
"<button onclick='login()'>Log in</button>";
150+
"<button onclick='user.login()'>Log in</button>";
149151
}
150152
errors.push(topush);
151153
}
@@ -167,7 +169,7 @@ <h3 class="footer-title">Share</h3>
167169
});
168170
/* */
169171
function parseMarkdown(item,next){
170-
uriAsAuthority("https://api.github.com/markdown/raw",function(uri){
172+
user.asAuthority("https://api.github.com/markdown/raw",function(uri){
171173
jQuery.ajax({
172174
url:uri,
173175
type:'POST',
@@ -177,7 +179,7 @@ <h3 class="footer-title">Share</h3>
177179
data:item.body
178180
}).done(function(data){
179181
item.bodyHTML = data;
180-
}).fail(function(response, type, title, config) {
182+
}).fail(function(response, textStatus,data) {
181183
if(response.status === 403){
182184
add403();
183185
}else{
@@ -208,102 +210,6 @@ <h3 class="footer-title">Share</h3>
208210
setTimeout(next.bind(next,item),1);
209211
}
210212
/* */
211-
function CookieStore(id){
212-
this.id = id;
213-
var cook = document.cookie.split(";");
214-
var current;
215-
var i;
216-
for(i=0;i<cook.length;i++){
217-
if(cook[i].indexOf("=") != -1){
218-
current = cook[i].split("=");
219-
if(current[0].replace(/s/, "") == id){
220-
this.cookie = JSON.parse(decodeURIComponent(current[1]));
221-
return;
222-
}
223-
}
224-
}
225-
this.cookie = {};
226-
this.save();
227-
}
228-
229-
CookieStore.prototype.get = function(key){
230-
return this.cookie[key];
231-
};
232-
CookieStore.prototype.set = function(key, value){
233-
this.cookie[key] = value;
234-
this.save();
235-
return this;
236-
};
237-
CookieStore.prototype.delete = function(key){
238-
delete this.cookie[key];
239-
this.save();
240-
return this;
241-
};
242-
243-
CookieStore.prototype.save = function(){
244-
document.cookie = this.id+"="+encodeURIComponent(JSON.stringify(this.cookie))+";";
245-
};
246-
247-
//==========Auth Start===================
248-
249-
250-
var auth = new CookieStore("auth");
251-
var is_authed = -1;
252-
var auth_queue = [];
253-
var url = require("url");
254-
var querystring = require("querystring");
255-
var docuri = url.parse(window.location.href);
256-
docuri.query = querystring.parse(docuri.query);
257-
var config = {
258-
cid: "dafb27cb88db35267e75",
259-
yqluri: "store://gdDAnJkTXAuVgzAQ8wboA2"
260-
};
261-
if(docuri.query && docuri.query.code){
262-
if(docuri.query.state != auth.get("state")){
263-
errors.push(new Error("Improper State"));
264-
}else{
265-
getAccess(docuri.query.code);
266-
}
267-
}else if(auth.get("access_token")){
268-
console.log(auth.get("access_token"));
269-
is_authed = 1;
270-
}
271-
272-
function login(){
273-
var state = Date.now()+"_"+Math.random();
274-
auth.set("state", state);
275-
window.location.href = "https://github.com/login/oauth/authorize" +
276-
"?client_id="+config.cid +
277-
"&state="+state;
278-
}
279-
function getAccess(code){
280-
is_authed = 0;
281-
auth.delete("state");
282-
jQuery.get(compileYQL([
283-
"env \""+config.yqluri+"\"",
284-
"select * from github where CODE=\""+code+"\""
285-
])).done(function(results){
286-
console.log("auth success");
287-
console.log(arguments);
288-
auth.set("access_token",results.query.results.OAuth.access_token);
289-
is_authed = 1;
290-
jQuery(".four-zero-three .content").text("You've authenticated!");
291-
}).fail(function(e){
292-
console.error(arguments);
293-
is_authed = -1;
294-
errors.push({
295-
class:"four-zero-three",
296-
name:"You've failed authorization",
297-
message: "You can always try again"
298-
});
299-
}).always(function(){
300-
console.log("always");
301-
while(auth_queue.length){
302-
uriAsAuthority.apply(void(0),auth_queue.pop());
303-
}
304-
});
305-
}
306-
307213
function compileYQL(query){
308214
if(Array.isArray(query)){
309215
query = query.join(";");
@@ -315,30 +221,63 @@ <h3 class="footer-title">Share</h3>
315221
encodeURIComponent(query) +
316222
"&diagnostics=true&format=json";
317223
}
224+
function yql_access(yql_uri){
225+
return function(code,next){
226+
var url;
227+
try{
228+
url = compileYQL([
229+
"env \""+yql_uri+"\"",
230+
"select * from github where CODE=\""+code+"\""
231+
]);
232+
}catch(e){
233+
return setTimeout(next.bind(next,e),1);
234+
}
318235

319-
function uriAsAuthority(uri,next){
320-
if(is_authed === -1) return next(uri);
321-
if(is_authed === 0) return auth_queue.push([uri,next]);
322-
uri = url.parse(uri);
323-
uri.query = querystring.parse(uri.query);
324-
uri.query.access_token = auth.get("access_token");
325-
uri.search = "?"+querystring.stringify(uri.query);
326-
next(url.format(uri));
236+
jQuery.ajax(url).done(function(results){
237+
next(void(0),results.query.results.OAuth.access_token);
238+
}).fail(function(x,status,e){
239+
next(e);
240+
});
241+
};
327242
}
328243

329-
function logout(){
330-
auth.delete("access_token");
331-
is_authed = -1;
332-
}
244+
var config = {
245+
cid: "dafb27cb88db35267e75",
246+
yqluri: "store://gdDAnJkTXAuVgzAQ8wboA2"
247+
};
248+
249+
var AuthProvider = require("AuthProvider");
250+
251+
window.user = new AuthProvider(
252+
config.cid,
253+
yql_access(config.yqluri)
254+
);
255+
user.on("parse-error",function(e){
256+
errors.push({
257+
class:"four-zero-three",
258+
name:"You've failed authorization",
259+
message: e
260+
});
261+
});
262+
user.on("access-error",function(e){
263+
errors.push({
264+
class:"four-zero-three",
265+
name:"You've failed authorization",
266+
message: "You can always try again"
267+
});
268+
});
269+
user.on("access-success",function(token){
270+
jQuery(".four-zero-three .content").text("You've authenticated!");
271+
});
333272
var hash = document.location.toString().split("#")[1];
334273
NodeOsBlog.controller('BlogSingleCtrl', function ($scope, $http) {
335274
$scope.uriPath = "/NodeOS/NodeOS/issues/"+hash;
275+
$scope.user = user;
336276
$scope.blog = [];
337-
$scope.uriAsAuthority = uriAsAuthority;
338277
$scope.parseMarkdown = parseMarkdown;
339278

340279
var markdown = require("markdown").markdown;
341-
$scope.uriAsAuthority('https://api.github.com/repos'+$scope.uriPath,function(uri){
280+
$scope.user.asAuthority('https://api.github.com/repos'+$scope.uriPath,function(uri){
342281
$http.get(uri).success(function(data,status,headers) {
343282
var l = data.labels.length;
344283
while(l--){
@@ -368,15 +307,15 @@ <h3 class="footer-title">Share</h3>
368307
});
369308
NodeOsBlog.controller('CommentListCtrl', function ($scope, $http) {
370309
$scope.uriPath = "/NodeOS/NodeOS/issues/"+hash+"/comments";
310+
$scope.user = user;
371311
$scope.blog = [];
372312
$scope.last = void(0);
373-
$scope.uriAsAuthority = uriAsAuthority;
374313
$scope.parseMarkdown = parseMarkdown;
375314
$scope.loadMore = function(page){
376315
if($scope.last && $scope.last < page) return;
377316
var i=0;
378317
var l=-1;
379-
$scope.uriAsAuthority(
318+
$scope.user.asAuthority(
380319
'https://api.github.com/repos'+$scope.uriPath+'?labels=blog&sort=created&page='+page,
381320
function(uri){
382321
$http.get(uri).success(function(data,status,headers) {

0 commit comments

Comments
 (0)