|
| 1 | +/** |
| 2 | + * @Service |
| 3 | + * @Module auth |
| 4 | + * --------------------------------------------- |
| 5 | + * Author : IndexXuan(https://github.com/IndexXuan) |
| 6 | + * Mail : indexxuan@gmail.com |
| 7 | + * Date : Tue 14 Mar 2017 03:19:07 PM CST |
| 8 | + */ |
| 9 | + |
| 10 | +const setCookieParser = require('set-cookie-parser') |
| 11 | + |
| 12 | +module.exports = app => { |
| 13 | + return class AuthService extends app.Service { |
| 14 | + |
| 15 | + constructor (ctx) { |
| 16 | + super(ctx) |
| 17 | + this.url = 'https://www.v2ex.com/signin' |
| 18 | + this.setCookiestr = '' // 供放在headers里使用 |
| 19 | + this.cookies = '' // get到的cookie暂存,供合并给最终的cookies |
| 20 | + this.userKey = '' |
| 21 | + this.passKey = '' |
| 22 | + this.once = '' |
| 23 | + } |
| 24 | + |
| 25 | + async request (url, opts) { |
| 26 | + opts = Object.assign({ |
| 27 | + timeout: [ '30s', '30s' ], |
| 28 | + }, opts) |
| 29 | + |
| 30 | + return await this.ctx.curl(url, opts) |
| 31 | + } |
| 32 | + |
| 33 | + getKeys (content) { |
| 34 | + const re = /class="sl"\s*name=\"(\w*)/g |
| 35 | + const onceRe = /type="hidden"\s*value="(\w*)\s*/ |
| 36 | + const matches = content && content.match(re) |
| 37 | + this.userKey = matches[0].match(/name="(\w*)/)[1] |
| 38 | + this.passKey = matches[1].match(/name="(\w*)/)[1] |
| 39 | + this.once = content.match(onceRe)[1] |
| 40 | + } |
| 41 | + |
| 42 | + async getAuthPrepare () { |
| 43 | + const result = await this.request(this.url, { |
| 44 | + method: 'GET', |
| 45 | + dataType: 'text', |
| 46 | + headers: { |
| 47 | + "accept": "text/html,application/xhtml+xml,application/xml", |
| 48 | + "origin": "https://www.v2ex.com", |
| 49 | + "referer": "https://www.v2ex.com/signin", |
| 50 | + "user-agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36" |
| 51 | + } |
| 52 | + }) |
| 53 | + this.setCookiestr = result.headers['set-cookie'][0] |
| 54 | + const cs = setCookieParser(result) |
| 55 | + this.cookies = cs |
| 56 | + |
| 57 | + // set cookies for client |
| 58 | + cs.forEach(c => { |
| 59 | + this.ctx.cookies.set(c.name, c.value, { |
| 60 | + domain: '', |
| 61 | + expires: c.expires, |
| 62 | + path: c.path, |
| 63 | + httpOnly: c.httpOnly |
| 64 | + }) |
| 65 | + }) |
| 66 | + return this.getKeys(result.data) |
| 67 | + } |
| 68 | + |
| 69 | + async signin (ctx) { |
| 70 | + |
| 71 | + await this.getAuthPrepare() |
| 72 | + |
| 73 | + const opts = { |
| 74 | + method: 'POST', |
| 75 | + headers: { |
| 76 | + "method": "POST", |
| 77 | + "accept": "text/html,application/xhtml+xml,application/xml", |
| 78 | + "origin": "https://www.v2ex.com", |
| 79 | + "referer": "https://www.v2ex.com/signin", |
| 80 | + "Cookie": this.setCookiestr, |
| 81 | + "user-agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36" |
| 82 | + }, |
| 83 | + data: { |
| 84 | + [this.userKey]: "your username", |
| 85 | + [this.passKey]: "your password", |
| 86 | + "once": this.once, |
| 87 | + "next": "/" |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + const result = await this.request(this.url, opts) |
| 92 | + |
| 93 | + const cs = setCookieParser(result) |
| 94 | + // set cookies for client |
| 95 | + cs.forEach(c => { |
| 96 | + this.ctx.cookies.set(c.name, c.value, { |
| 97 | + domain: '', |
| 98 | + path: c.path, |
| 99 | + expires: c.expires, |
| 100 | + httpOnly: c.httpOnly |
| 101 | + }) |
| 102 | + }) |
| 103 | + |
| 104 | + // return all cookies to client |
| 105 | + return cs.concat(this.cookies.filter(item => item.name === 'PB3_SESSION')) |
| 106 | + } |
| 107 | + |
| 108 | + } // /.class=>AuthService |
| 109 | +} |
0 commit comments