Last active
July 9, 2018 14:47
-
-
Save lunhg/11ec9d080395a6e845230470f5a5c8e8 to your computer and use it in GitHub Desktop.
O problema atual de, ao adicionar um usuário, procurar se ele existe na base de dados. Se não, criar um. Este usuário será administrador se estiver na whitelist de configuração em `config/<config>.json`. Após isso, procuraremos seus dados no id.org, com um cliente OpenId
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
// users-model.js - A mongoose model | |
// | |
// See http://mongoosejs.com/docs/models.html | |
// for more of what you can do here. | |
const uuid = require('uuid'); | |
const { Issuer } = require('openid-client'); | |
module.exports = function (app) { | |
const mongooseClient = app.get('mongooseClient'); | |
let table = { | |
telegramId: { type: String, required: true }, | |
isAdmin: { type: Boolean } | |
} | |
let users = new mongooseClient.Schema(table, { | |
timestamps: true | |
}) | |
let Users = mongooseClient.model('users', users); | |
let whitelist = [ | |
app.get('authentication').telegram.username, | |
...app.get('authentication').telegram.whitelist | |
] | |
// Pre-save some data | |
users.pre('save', function(next) { | |
let self = this | |
Users.find({telegramId: self.telegramId}).then(function(users){ | |
if(users.length > 0) { | |
self.invalidate("telegramId", "telegramId must be unique") | |
return new Error("telegramId must be unique") | |
} | |
else { | |
self.isAdmin = false | |
for (let i in whitelist) { | |
if (telegramId === whitelist[i]){ | |
self.isAdmin = true | |
break; | |
} | |
} | |
return Issuer.discover('https://id.org.br') | |
} | |
}).then(function (idIssuer) { | |
console.log('Discovered issuer %s', idIssuer); | |
return new idIssuer.Client({ | |
client_id: '<client_token>', | |
client_secret: '<client_secret>' | |
}) | |
}).then(function(client){ | |
return client.authorizationCallback('https://localhost:3000/authentication') | |
}).then(function(tokenSet){ | |
l// TODO here... | |
next() | |
}).catch(next) | |
}) | |
// Drop database and reset all data if you are in development mode | |
if (process.env.NODE_ENV === 'development') { | |
Users.find({}).remove() | |
for (let i in whitelist) { | |
Users.create({ | |
telegramID: whitelist[i] | |
}) | |
} | |
} | |
return Users | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment