-
Notifications
You must be signed in to change notification settings - Fork 414
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
202 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = class extends think.Controller { | ||
async githubAction() { | ||
const instance = this.service('auth/github', this); | ||
const socialInfo = await instance.getUserInfo(); | ||
return this.success(socialInfo); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module.exports = { | ||
success(...args) { | ||
this.ctx.success(...args); | ||
return think.prevent(); | ||
}, | ||
fail(...args) { | ||
this.ctx.fail(...args); | ||
return think.prevent(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const preventMessage = 'PREVENT_NEXT_PROCESS'; | ||
module.exports = { | ||
prevent() { | ||
throw new Error(preventMessage); | ||
}, | ||
isPrevent(err) { | ||
return think.isError(err) && err.message === preventMessage; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
const Base = require('./base'); | ||
|
||
module.exports = class extends Base { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const { parse } = require('url'); | ||
|
||
module.exports = class extends think.Service { | ||
constructor(app) { | ||
super(app); | ||
this.app = app; | ||
} | ||
|
||
getCompleteUrl(url = '') { | ||
if (url.slice(0, 4) === 'http') { | ||
try { | ||
const { host } = parse(url); | ||
if (this.app.host.toLowerCase() !== host.toLowerCase()) { | ||
throw new Error(403); | ||
} | ||
return url; | ||
} catch (e) { | ||
// ignore error | ||
} | ||
} | ||
const protocol = this.app.header('x-forwarded-proto') || 'http'; | ||
if (!/^\//.test(url)) { | ||
url = '/' + url; | ||
} | ||
return protocol + '://' + this.app.ctx.host + url; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
const qs = require('querystring'); | ||
const request = require('request-promise-native'); | ||
const Base = require('./base'); | ||
|
||
const OAUTH_URL = 'https://github.com/login/oauth/authorize'; | ||
const ACCESS_TOKEN_URL = 'https://github.com/login/oauth/access_token'; | ||
const USER_INFO_URL = 'https://api.github.com/user'; | ||
const {GITHUB_ID, GITHUB_SECRET} = process.env; | ||
module.exports = class extends Base { | ||
getAuthUrl(opts) { | ||
const params = { | ||
client_id: GITHUB_ID, | ||
redirect_uri: opts.rdUrl, | ||
scope: 'user' | ||
}; | ||
return OAUTH_URL + '?' + qs.stringify(params); | ||
} | ||
|
||
async getAccessToken(opts) { | ||
const params = { | ||
client_id: GITHUB_ID, | ||
client_secret: GITHUB_SECRET, | ||
code: opts.code | ||
}; | ||
|
||
const body = await request.post({ | ||
url: ACCESS_TOKEN_URL, | ||
headers: {'Accept': 'application/json'}, | ||
form: params, | ||
json: true | ||
}); | ||
return body; | ||
} | ||
|
||
async getUserInfoByToken(opts) { | ||
const userInfo = await request.get({ | ||
url: USER_INFO_URL, | ||
headers: { | ||
'User-Agent': '@waline', | ||
'Authorization': 'token ' + opts.access_token | ||
}, | ||
json: true | ||
}); | ||
|
||
return { | ||
id: userInfo.login, | ||
name: userInfo.name, | ||
desc: userInfo.bio, | ||
avatar: userInfo.avatar_url | ||
}; | ||
} | ||
|
||
async redirect() { | ||
const {app} = this; | ||
const {type, rdurl} = app.get(); | ||
const rdUrlAfterLogin = this.getCompleteUrl(rdurl); | ||
|
||
const params = { rdurl: rdUrlAfterLogin, type }; | ||
const signinUrl = this.getCompleteUrl('/oauth') + '?' + qs.stringify(params); | ||
const AUTH_URL = this.getAuthUrl({rdUrl: signinUrl}); | ||
app.redirect(AUTH_URL); | ||
return app.success(); | ||
} | ||
|
||
async getUserInfo() { | ||
const {code} = this.app.get(); | ||
if (!code) { | ||
return this.redirect(); | ||
} | ||
|
||
const accessTokenInfo = await this.getAccessToken({code}); | ||
return this.getUserInfoByToken(accessTokenInfo); | ||
} | ||
}; |