+ routers

This commit is contained in:
tmshv committed 2016-03-23 02:11:53 +03:00
1 parent d3ab0713b4
commit d002bd74a7
5 files changed
+224

No files matched your search

+63
View File
@@ -0,0 +1,63 @@
import {get, url} from './';
import {authorize, createAuthorizationUrl} from '../instagram';
import {redirectUri, authScope} from '../config';
import {authorizeUser} from '../core/auth';
export function auth() {
return get('/auth', async(ctx) => {
const state = url('/');
const i = createAuthorizationUrl(redirectUri, authScope, state);
ctx.redirect(i, state);
});
}
export function authCallback() {
const root = url('/');
return get('/auth/callback', async(ctx) => {
let state = ctx.query.state;
let code = ctx.query.code;
if(!ctx.session.accessToken) {
try{
var client = await authorize(code, redirectUri);
}catch (e){
console.log(e);
}
if (client){
var user = await authorizeUser(client.access_token, client.user);
var accessToken = client.access_token;
}
ctx.status = user ? 200 : 401;
ctx.session.user = user;
ctx.session.accessToken = accessToken;
}
ctx.redirect(state, root);
});
}
export function authOut(redirectUrl = '/') {
const redirectTo = url(redirectUrl);
const root = url('/');
return get('/auth/out', async(ctx) => {
ctx.session = null;
ctx.redirect(redirectTo, root);
});
}
export function ensureAuth(failRedirect = '/') {
const redirectTo = url(failRedirect);
const root = url('/');
return async (ctx, next) => {
if(!ctx.session.user) {
ctx.redirect(redirectTo, root)
}else{
await next();
}
}
}
@@ -0,0 +1,42 @@
import {get, post} from './';
import update from '../core/update';
export function callbackGet() {
return get('/callback/:id', async (ctx, id) => {
if(ctx.query['hub.mode'] == 'subscribe'){
ctx.body = ctx.query['hub.challenge'];
}else{
ctx.status = 400;
}
});
}
export function callbackPost() {
return post('/callback/:id', async (ctx, id) => {
let body = ctx.request.body;
update(id, body);
//body example
// {
// "changed_aspect": "media",
// "object": "tag",
// "object_id": "love",
// "time": 1458675620,
// "subscription_id": 22487204,
// "data": {}
// }
// let sign = ctx.header['x-hub-signature'];
//var hmac = crypto.createHmac("sha1", instagram.client_secret);
//hmac.update(body);
//var shasum = hmac.digest("hex");
//console.log("X-Hub-Signature", sign);
//console.log("Hmac", shasum);
//console.log();
//if(shasum.digest("hex") == sign) {
// instagram.update(body);
//}
});
}
+56
View File
@@ -0,0 +1,56 @@
import fs from 'mz/fs';
import compose from 'koa-compose';
import route from 'koa-route';
import {indexPath, pathPrefix as root} from '../config';
import main from './main';
import {auth, authCallback, authOut} from './auth';
import {list, addTag, addUser} from './subscription';
import {ensureAuth} from './auth';
import {callbackGet, callbackPost} from './callback';
export function statelessRouters() {
return compose([
callbackGet(),
callbackPost()
]);
}
export function routers() {
return compose([
main(),
auth(),
authCallback(),
authOut(),
user(),
list(),
addTag(),
addUser()
]);
}
export const url = path => `${root}${path}`;
export const get = (path, fn) => route['get'](url(path), fn);
export const post = (path, fn) => route['post'](url(path), fn);
export function index(fn){
return async function (ctx){
ctx.type = 'text/html';
ctx.body = await fs.createReadStream(indexPath);
}
}
function user(){
return compose([
ensureAuth(),
get('/user', async (ctx) => {
const user = ctx.session.user.id;
const instagram = ctx.instagram;
let data = await instagram.user.mediaRecent(user);
console.log(data);
ctx.body = data;
})
]);
}
+7
View File
@@ -0,0 +1,7 @@
import {index, get} from './';
export default function () {
return get('/', async (ctx) => {
await index()(ctx);
});
}
@@ -0,0 +1,56 @@
import compose from 'koa-compose';
import {get} from './';
import {ensureAuth} from './auth';
export function list() {
return compose([
ensureAuth(),
get('/subscriptions', async(ctx) => {
const instagram = ctx.instagram;
ctx.set('Content-Type', 'application/json');
ctx.body = await instagram.subscriptions.list();
})
]);
}
export function addTag() {
return compose([
ensureAuth(),
get('/subscriptions/add-tag/:tag', async(ctx, tag) => {
let callbackUrl = ctx.query.url;
let result = await ctx.instagram.subscriptions.subscribeTag(tag, callbackUrl);
if (result) {
ctx.body = result;
} else {
ctx.status = 500;
}
})
]);
}
export function addUser() {
return compose([
ensureAuth(),
get('/subscriptions/add-user', async (ctx) => {
let callbackUrl = ctx.query.url;
let result = await ctx.instagram.subscriptions.subscribeUser(callbackUrl);
if (result) {
ctx.body = result;
} else {
ctx.status = 500;
}
})
])
}
// export function remove() {
// return get('/subscriptions/remove/:id', async (ctx, id) => {
// let result = yield instagram.api.unsubscribe(id);
// if(result) {
// ctx.body = result;
// }else{
// ctx.status = 500;
// }
// });
// }