-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refs #109860 add auth with github, add component for auth page,…
… add dev mongo model
- Loading branch information
1 parent
923fb87
commit 91d5a79
Showing
32 changed files
with
4,371 additions
and
119 deletions.
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
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 |
---|---|---|
|
@@ -3,4 +3,5 @@ node_modules/ | |
yarn-error.log | ||
.history | ||
.env | ||
logs/ | ||
logs/ | ||
.DS_Store |
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,12 @@ | ||
module.exports = function(api) { | ||
const presets = [ | ||
['@babel/preset-env', { useBuiltIns: 'entry', corejs: '3.0.0' }], | ||
'@babel/preset-react', | ||
]; | ||
|
||
const plugins = ['@babel/plugin-proposal-class-properties']; | ||
|
||
api.cache.never(); | ||
|
||
return { presets, plugins }; | ||
}; |
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 |
---|---|---|
@@ -1,7 +1,23 @@ | ||
mongo -- "$MONGO_INITDB_DATABASE" <<EOF | ||
mongo -- "$MONGO_INITDB_PARSE_DATABASE" <<EOF | ||
var user = '$MONGO_INITDB_ROOT_USERNAME'; | ||
var passwd = '$MONGO_INITDB_ROOT_PASSWORD'; | ||
var admin = db.getSiblingDB('admin'); | ||
admin.auth(user, passwd); | ||
var table = db.getSiblingDB('$MONGO_INITDB_PARSE_DATABASE'); | ||
table.auth(user, passwd); | ||
db.createUser({user: user, pwd: passwd, roles: ["readWrite"]}); | ||
EOF | ||
|
||
mongo -- "$MONGO_INITDB_API_DATABASE" <<EOF | ||
var user = '$MONGO_INITDB_ROOT_USERNAME'; | ||
var passwd = '$MONGO_INITDB_ROOT_PASSWORD'; | ||
var table = db.getSiblingDB('$MONGO_INITDB_API_DATABASE'); | ||
table.auth(user, passwd); | ||
db.createUser({user: user, pwd: passwd, roles: ["readWrite"]}); | ||
EOF | ||
|
||
mongo -- "$MONGO_INITDB_SANDBOX_DATABASE" <<EOF | ||
var user = '$MONGO_INITDB_ROOT_USERNAME'; | ||
var passwd = '$MONGO_INITDB_ROOT_PASSWORD'; | ||
var table = db.getSiblingDB('$MONGO_INITDB_SANDBOX_DATABASE'); | ||
table.auth(user, passwd); | ||
db.createUser({user: user, pwd: passwd, roles: ["readWrite"]}); | ||
EOF |
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,15 @@ | ||
const auth = require('./services/auth'); | ||
const logger = require('./../logger'); | ||
|
||
module.exports = srv => { | ||
srv.post('/api/auth', async (req, res) => { | ||
try { | ||
const jwt = await auth.connectUser(req.body.code); | ||
|
||
res.send(jwt); | ||
} catch (err) { | ||
logger(err); | ||
res.sendStatus(500); | ||
} | ||
}); | ||
}; |
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,50 @@ | ||
const jwt = require('jsonwebtoken'); | ||
const moment = require('moment'); | ||
const { AUTH_SECRET } = require('../../config'); | ||
const github = require('./github'); | ||
const Developer = require('./../../db/model/developer'); | ||
|
||
class Auth { | ||
// Generate a token and send connect link by email | ||
async createUserIfNotExist(userGithub) { | ||
let developer = await Developer.findOne({ githubId: userGithub.id }).exec(); | ||
|
||
if (!developer) { | ||
developer = new Developer({ | ||
login: userGithub.login, | ||
githubId: userGithub.id, | ||
companyName: userGithub.company, | ||
email: userGithub.email, | ||
created_at: new Date(), | ||
updated_at: new Date(), | ||
}); | ||
|
||
developer = await developer.save(); | ||
} | ||
|
||
return developer; | ||
} | ||
|
||
// Valid a token and return the jwt session | ||
async connectUser(githubCode) { | ||
const githubToken = await github.getAccessToken(githubCode); | ||
const userGithub = await github.getUser(githubToken); | ||
const developer = await this.createUserIfNotExist(userGithub); | ||
|
||
const jwtToken = jwt.sign( | ||
{ | ||
login: developer.login, | ||
name: userGithub.name, | ||
iat: moment().valueOf(), | ||
exp: moment() | ||
.add(1, 'days') | ||
.valueOf(), | ||
}, | ||
AUTH_SECRET, | ||
); | ||
|
||
return jwtToken; | ||
} | ||
} | ||
|
||
module.exports = new Auth(); |
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,33 @@ | ||
const axios = require('axios'); | ||
const { GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET } = require('./../../config'); | ||
|
||
class Github { | ||
async getAccessToken(code) { | ||
const response = await axios({ | ||
method: 'post', | ||
url: 'https://github.com/login/oauth/access_token', | ||
data: { | ||
client_id: GITHUB_CLIENT_ID, | ||
client_secret: GITHUB_CLIENT_SECRET, | ||
code, | ||
}, | ||
headers: { | ||
Accept: 'application/json', | ||
}, | ||
}); | ||
|
||
return response.data.access_token; | ||
} | ||
|
||
async getUser(token) { | ||
const response = await axios({ | ||
method: 'get', | ||
url: 'https://api.github.com/user', | ||
headers: { Authorization: `token ${token}`, Accept: 'application/json' }, | ||
}); | ||
|
||
return response.data; | ||
} | ||
} | ||
|
||
module.exports = new Github(); |
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,26 @@ | ||
const mongoose = require('mongoose'); | ||
const logger = require('../logger'); | ||
const { | ||
MONGO_DB_NAME, | ||
MONGO_HOST, | ||
MONGO_USERNAME, | ||
MONGO_PASSWORD, | ||
MONGO_PORT, | ||
} = require('../config'); | ||
|
||
mongoose.connect( | ||
`mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_HOST}:${MONGO_PORT}/${MONGO_DB_NAME}-api`, | ||
{ useNewUrlParser: true }, | ||
); | ||
|
||
const db = mongoose.connection; | ||
|
||
db.on('error', err => { | ||
logger(`mongo connect error : ${err}`); | ||
}); | ||
|
||
db.once('open', () => { | ||
logger(`mongo connected on ${MONGO_DB_NAME}-api`); | ||
}); | ||
|
||
module.exports = mongoose; |
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,35 @@ | ||
const Developer = require('./model/developer'); | ||
const Application = require('./model/application'); | ||
const mongoose = require('mongoose'); | ||
|
||
class MongoInstall { | ||
async init() {} | ||
|
||
async exec() { | ||
let developer = new Developer({ | ||
login: 'sample', | ||
githubId: 12345, | ||
companyName: 'company', | ||
email: '[email protected]', | ||
created_at: new Date(), | ||
updated_at: new Date(), | ||
}); | ||
|
||
developer = await developer.save(); | ||
|
||
let application = new Application({ | ||
developerId: developer._id, | ||
name: 'sampleApp', | ||
token: 'superToken', | ||
created_at: new Date(), | ||
updated_at: new Date(), | ||
}); | ||
|
||
await application.save(); | ||
|
||
return true; | ||
} | ||
} | ||
|
||
const install = new MongoInstall(); | ||
install.init().then(async () => await install.exec()); |
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,11 @@ | ||
const mongoose = require('../apiClient'); | ||
|
||
const applicationSchema = new mongoose.Schema({ | ||
developerId: mongoose.Schema.Types.ObjectId, | ||
name: String, | ||
token: String, | ||
create_at: { type: Date, default: Date.now }, | ||
updated_at: { type: Date, default: Date.now }, | ||
}); | ||
|
||
module.exports = mongoose.model('Application', applicationSchema); |
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,12 @@ | ||
const mongoose = require('../apiClient'); | ||
|
||
const developerSchema = new mongoose.Schema({ | ||
login: String, | ||
githubId: { type: Number, unique: true }, | ||
companyName: String, | ||
email: String, | ||
create_at: { type: Date, default: Date.now }, | ||
updated_at: { type: Date, default: Date.now }, | ||
}); | ||
|
||
module.exports = mongoose.model('Developer', developerSchema); |
Oops, something went wrong.