1 Star 0 Fork 0

Wang Ningkai / OLINDEX-Node

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
oauth2.js 1.76 KB
一键复制 编辑 原始数据 按行查看 历史
Wang Ningkai 提交于 2019-02-22 21:41 . fix
'use strict';
require('dotenv').config()
const createApplication = require('./');
const simpleOauthModule = require('simple-oauth2');
createApplication(({
app,
callbackUrl
}) => {
const oauth2 = simpleOauthModule.create({
client: {
id: process.env.CLIENT_ID,
secret: process.env.CLIENT_SECRET,
},
auth: {
tokenHost: process.env.OAUTH_AUTHORITY,
tokenPath: process.env.OAUTH_TOKEN_ENDPOINT,
authorizePath: process.env.OAUTH_AUTHORIZE_ENDPOINT,
},
options: {
authorizationMethod: 'body',
}
});
// Authorization uri definition
const authorizationUri = oauth2.authorizationCode.authorizeURL({
redirect_uri: callbackUrl,
scope: process.env.OAUTH_SCOPES,
});
// Initial page redirecting to Github
app.get('/auth', (req, res) => {
console.log(authorizationUri);
res.redirect(authorizationUri);
});
// Callback service parsing the authorization token and asking for the access token
app.get('/callback', async (req, res) => {
const code = req.query.code;
const options = {
code,
redirect_uri: callbackUrl,
};
try {
const result = await oauth2.authorizationCode.getToken(options);
console.log('The resulting token: ', result);
const token = oauth2.accessToken.create(result);
return res.status(200).json(token)
} catch (error) {
console.error('Access Token Error', error.message);
return res.status(500).json('Authentication failed');
}
});
app.get('/', (req, res) => {
res.send('Hello<br><a href="/auth">Log in with Microsoft</a>');
});
});
1
https://gitee.com/wangningkai/OLINDEX-Node.git
git@gitee.com:wangningkai/OLINDEX-Node.git
wangningkai
OLINDEX-Node
OLINDEX-Node
master

搜索帮助